PHP (if, then - form value help)

Greetings Guys,

I am working on learning some things in PHP, I am however not a No0b I am just more of a winidows application developer VB.Net, C++, C# etc…

Anyways I am trying to build in an after form post function that checks for a form feild value, then executes a script depending on the result for example, if the form looks like this…

Name: ___________
Email: ___________
Donation Type (multi select option box)
Options: Donate Money
Donate Products
Donate Time

the php function I need would run after the form is submitted but check to see which option the user selected for donation type, if the user selected Donate Money, then the function would then send the user to paypal donations page… Hope you understand what I am asking, how would I create a php function to check for that value then redirect the user based on the result.

Thanks in advance, and if someone can help me you can all feel free to ask me anything you might want to know about the windows applications coding world, I would be a big help there.

It’s not much different then you will do in ASP or .net

PHP functions start with <?php and end with ?>

Think of it as switching out the inline server takes for asp <% %>. You can do the same in .NET on the HTML side.

Once you have your code block form… You just get the form fields…

in .NET you probably do something like

[php][/php]

Then in the code behind you do this…

[php]Dim sName as string = txt_name.value[/php]

Well it’s pretty much the same thing in PHP… You just need to change it a little.

The HTML you can remove the runat=“server”

Then in the code block

[php]<?php

$sName = $_POST[‘txt_name’];

?>[/php]

Variable in PHP start with $, you don’t have to DIM them or Declare them and to get an HTML element that’s been posted back with a submit button use $_POST and if it’s on the query screen from a link use $_GET

Just rinse and repeat it for the other fields you are looking to retrieve.

It would be better practice not to convert it to a variable though, there’s no point, just another strain on resources. There’s no reason you can’t just use $_POST[‘txt_name’] in whatever you do. That does the exact same thing a variable does so you’re doubling the bandwidth by converting it to a variable.

Just saying :stuck_out_tongue:

@scottlpool2003

I’m going to have to disagree with you, but I’m backing it up with proof…

[php]<?php

$test = $_GET[‘name’];
$time_start = microtime(true);

for ($x=0; $x<=10000; $x++)
{
$Assign = $test;
}
$time_end = microtime(true);

$time = $time_end - $time_start;

echo 'Assigned to a variable: ’ . $time;

echo “
”;
$time_start = microtime(true);
for ($x=0; $x<=10000; $x++)
{
$Assign = $_GET[‘name’];
}

$time_end = microtime(true);

$time = $time_end - $time_start;

echo 'Using the $_GET Directly: ’ . $time;
?>[/php]

Results…

Test 1

Assigned to a variable: 0.00078988075256348
Using the $_GET Directly: 0.0022192001342773

Test 2

Assigned to a variable: 0.00078892707824707
Using the $_GET Directly: 0.0021238327026367

Test 3

Assigned to a variable: 0.00079011917114258
Using the $_GET Directly: 0.0021100044250488

As you can see, it’s a lot more faster and efficient to assign it to a variable and use that variable throughout your code. Now granted, if you are just using it 1 time in your code. Go with the $_GET Directly.

Thank you Henry, I’ve also read somewhere that Super Globals are a bit slower than the regular variables and also agree with your point to use GET directly if it is being used one time only.

Of course you’ll get that output if you assign it outside of the actual speed test but what happens when using a real database query and assign the variable within the test?

[php]while($row = $sth->fetch(PDO::FETCH_ASSOC)) {

$time_start = microtime(true);

for ($x=0; $x<=10000; $x++)
{
$test = $row[‘Expr2’];
$Assign = $test;
}
$time_end = microtime(true);

$time = $time_end - $time_start;

echo 'Assigned to a variable: ’ . $time;

echo “
”;
$time_start = microtime(true);
for ($x=0; $x<=10000; $x++)
{
$Assign = $row[‘Expr2’];
}

$time_end = microtime(true);

$time = $time_end - $time_start;

echo 'Using the $_POST Directly: ’ . $time;

}[/php]

Result:
Assigned to a variable: 0.0033669471740723
Using the $_POST Directly: 0.0025389194488525

It can’t possibly be quicker (in 1 instance) to pull out the data, convert it to a variable and output it which is adding 1 step than pulling out the data and outputting it, that’s beyond belief, simply impossible.

The only case in which converting it to a variable would be quicker would be if you were going to use that variable multiple times as the variable stores in the memory whereas the $_POST[’’] will pull that data again.

@scottlpool2003

You didn’t use $_POST once in your example, I’m not sure what you’re getting at. All I see is an extra assignment of a variable in 1 loop verses the other.

I the example I gave, I was trying to get across the point that “Arrays” ($_POST, $_GET) are slower then strings. The larger the array the slower they are.

It’s more efficient to assign a value in the array to a string then use that string throughout your code, then repeatedly accessing a slower array object. That’s what I was showing.

My impression is that you think you should never assign an array to a string and just use the array directly throughout the code. This is where I disagree, but perhaps my impression was wrong.

It’s also faster to assign a database column to a string, then to use that database column directly throughout your code, because a database column is simply an array.

The speed difference is nominal at best, for a function call, it wouldn’t make a difference which one you use. But for troubleshooting purposes, its better to assign it a variable - less typing involved too.

To answer the question, you could do something like
[php]function findDonation($item) {
$needed = array(‘Donate Money’, ‘Donate Time’, ‘Donate Products’);
if(in_array($item, $needed)) {
// do something
return $something;
} else {
// not there, do something else
}
}[/php]
Then in your processing code, you’d do findDonation($_POST[‘options’]);

Its been a while since i used in_array, so the syntax might be wrong.

Sponsor our Newsletter | Privacy Policy | Terms of Service