简体   繁体   English

PHP If / Else语句在While循环中不起作用

[英]PHP If / Else statement not working in While loop

I've been working on this bit of code for about an hour and can't seem to figure out why it's not working. 我已经在这一段代码上工作了大约一个小时,似乎无法弄清为什么它不起作用。 Does PHP allow If/Else statements within While loops? PHP是否在While循环中允许If / Else语句? I've tried adding different echos to both If/Else statements and the latter (Else) will not show. 我尝试向If / Else语句添加不同的回显,并且后者(Else)将不会显示。 Is this because I'm trying to use the same variable names? 这是因为我试图使用相同的变量名吗?

while($row = mysql_fetch_array($result))

{

//assign variables
$title = $row['title'];
$file_url = $row['file_location'];
$category = $row['category'];
$layout = $row['layout'];


    If ($layout = "vertical")       
    {
        //Page Layout
        $BODYLAYOUT = "vertical_body";
        $GAMECONTAIN = "vertical_gameContain";
        $GAMEWIDTH = "vertical_game";
    }
    Else
    {
        // Page Layout
        $BODYLAYOUT = "horizontal_body";
        $GAMECONTAIN = "horizontal_gameContain";
        $GAMEWIDTH = "horizontal_game";
    }
if ($layout = "vertical")   

should be: 应该:

if ($layout == "vertical")   

Otherwise you are assinging a value to $layout of 'vertical' opposed to comparing it's value to see if it's equal to 'vertical'. 否则,您将向$ layout的“ vertical”分配一个值,而不是比较它的值以查看其是否等于'vertical'。 That assignment will otherwise equal to true, reason the first part runs and the ELSE does not. 否则,该分配将等于true,原因是第一部分运行而ELSE则不运行。

One method I use to prevent accidents like this is to put the constant first such as: 我用来防止此类事故发生的一种方法是将常量放在第一位,例如:

if ("vertical" == $layout)   

That way if I miss the other = sign PHP will error, rather than assign the value erroneously. 这样,如果我错过了另一个=号,PHP将会出错,而不是错误地分配值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM