douwen7905 2013-12-13 18:42
浏览 38

陷入困境的复合IF声明

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!

  • 写回答

1条回答 默认 最新

  • donglvlao8367 2013-12-13 18:49
    关注

    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;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应