0

I am experienced programmer, but am not a PHP developer. I have been asked to troubleshoot the following code, but can't see what the problem is. This IF statement works:

<?php
    if ($ice_cream_social == 'yes') {
        $registration_price = "58.00";
    }
?>

However, the page in question requires some compound IF statements to test if form elements have been checked and adjust the price accordingly. This code does NOT work, it doesn't give an error -- but the IF doesn't execute:

<?php
    if ($ice_cream_social == 'yes' AND $registration_type == 'Banquet and Conference') {
        $registration_price = "78.00";
?>

Using developer tools I have verified the form fields are being passed from the HTML, the code has the variable name and value spelled correctly, and I can test for any ONE variable's value successfully. It seems that when a second or third test is added using the syntax I showed above, the test fails and the IF doesn't run. Based on my limited understanding of PHP's syntax and some Googling, the code looks correct to me (and does not produce any errors), but I am thinking I am missing something about why this isn't working.

Can someone tell me if I'm missing something obvious in my code sample that would cause the IF not to run? I didn't include more code as this is one piece of a messy set of includes :)

Thanks!

red4d
  • 277
  • 2
  • 4
  • 10
  • 1
    **1.** `var_dump()` the values of `$ice_cream_social` and `$registration_type` and see what it outputs. **2.** It's a good idea to [use `===` instead of `==`](http://stackoverflow.com/a/2063500/1438393). **3.** I suggest using [`&&` instead of `AND`](http://stackoverflow.com/a/11861068/1438393). – Amal Murali Dec 13 '13 at 18:47
  • I would check the values and ensure there is no whitespace causing the logic to fail. You can use var_dump as mentioned to get the encapsulated value along with a string length and compare. If whitespace is an issue you can use trim() to get rid of it. – phpisuber01 Dec 13 '13 at 18:49
  • Hi Amal -- I tried all 3 of your suggestions. The dump of the vars shows they should match correctly and the checks should pass. I also switched to && and the triple equals which I know tests for value and type matching. However I am still not getting the IF to execute. Any other ideas? – red4d Dec 13 '13 at 19:54

1 Answers1

1

It looks like to me on the elseif you don't have a logical check, so you either need to change it to else or add a check(if that is your intention) elseif($registration_price>0)

I used this code to test:

<?php
$registration_price = '';
$ice_cream_social = 'yes';
//$ice_cream_social = 'no';
$registration_type = 'Banquet and Conference';
//$registration_type = 'Something else';

if ($ice_cream_social == 'yes') {
    $registration_price = "58.00";
} else {
    $registration_price = "not defined";
}        
echo $registration_price;

if ($ice_cream_social == 'yes' && $registration_type == 'Banquet and Conference') {
    $registration_price = "78.00";
} elseif( 1 > 0) {
    $registration_price = "1 million dolars!";
} else {
    $registration_price = "not defined";
}

echo $registration_price;
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
DrCord
  • 3,917
  • 3
  • 34
  • 47
  • ... or just use `else` – hek2mgl Dec 13 '13 at 18:50
  • That's too fragile, you need to initialize `$registration_price` with a proper default value if you are just using `elseif` without an unconditional `else`. Otherwise you would get `undefined symbol` errors if the condition(s) doesn't match – hek2mgl Dec 13 '13 at 18:52
  • @hek2mgl is that fine now? – DrCord Dec 13 '13 at 18:55
  • @hek2mgl: how is that any different than what I just posted? – DrCord Dec 13 '13 at 19:01
  • You had `else { echo "...." ; } ` .. this will not set `$registration_type`.. But after that you accessed it -> `undefined symbol` – hek2mgl Dec 13 '13 at 19:04