LWal
LWal

Reputation: 1

Multiple if statements in PHP

I know that this is not the way to do it but I'm having some trouble trying to set a variable using conditional statements.

For some context, $foo, $foo_1 and $foo_2 are checking to see if a form field is set, if it is, to set $bar equal to the calculation that accompanies each condition.

if(isset($foo)) {
    $bar = ($baz * 52) * ($qux);
} 

if(isset($foo_1)) {
    $bar = ($baz_1 * 12) * ($qux);
}

if(isset($foo_2)) {
    $bar = ($baz_2 * $qux);
}

This is because the existing code uses $bar in a calculation

$a = ($b / $bar)

and $a is passed on further down in a variety of cases.

As it is, only the first $foo condition is passing $bar properly, all other conditions $bar outputs as NULL.

EDIT: To clarify the code above (which was incorrect) and update my example below:

$foo = isset($form['first'] ? $form['first'] : '';
$foo_1 = isset($form['second'] ? $form['second'] : '';
$foo_2 = isset($form['third'] ? $form['third'] : '';
if(isset($foo)) {
    $bar = ($foo * 52) * $qux;
} 

if(isset($foo_1)) {
    $bar = ($foo_1 * 12) * $qux;
}

if(isset($foo_2)) {
    $bar = $foo_2 * $qux;
}

Revent, Oswald and RoyalBG's suggestions have pointed out that my code isn't setting variables properly, which is the case. $baz, $baz_1, $baz_2 were deprecated and non-existent and replaced with $foo, $foo_1, $foo_2 to accompany their conditionals for the desired inputs. Once I clean this up I will use a switch statement instead of what I've written above. All of the comments below have been very helpful Thanks for the help.

Upvotes: 0

Views: 244

Answers (4)

Revent
Revent

Reputation: 2109

Oswald and Royal Bg are correct that your variables are likely never set. I would write the code this way (assuming you allow a zero value for the $baz and $qux variables):

$bar = 0;   // initialize the variable so it won't be null
if(isset($foo) && isset($baz) && isset($qux)) {
  $bar = ($baz * 52) * ($qux);
} 
else if(isset($foo_1) && isset($ba_1z) && isset($qux)) {
  $bar = ($baz_1 * 12) * ($qux);
}
else if(isset($foo_2) && isset($baz_2) && isset($qux)) {
  $bar = ($baz_2 * $qux);
}

You can then check the value of $bar using the empty() function, which returns true if the value is null, zero, or an empty string.

Upvotes: 0

Ivan Yonkov
Ivan Yonkov

Reputation: 7034

If the code in your condition is not passed to the PHP engine, when you ask the variable to be set, then it's most likely not set... you could also try with switch/case block

Upvotes: 0

Last Breath
Last Breath

Reputation: 530

Please clarify your code much more because i don't see where your $baz_1 variable states and where it is initialized ?

As the logic say you must multiply the $foo_1 by the $qux i don't know what you mean by the $baz_1,$baz_2 variables

Then if you are testing the $foo_1,$foo_2 you must make your calculation base on the tested variable not on a new variable that may it is not set

More explaining what if the $foo,$foo_1,$foo_2 are set but the $baz,$baz_1,$baz_2 not set i don't see any manipulation for the $baz variables please clarify the situation .

Upvotes: 0

Oswald
Oswald

Reputation: 31685

$foo_1 and $foo_2 are never set. That's why if $foo is not set, $bar won't be set either.

Upvotes: 1

Related Questions