2

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? I've tried adding different echos to both If/Else statements and the latter (Else) will not show. 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";
    }
6
  • there is a spelling mistake in else key word , It should be 'else' , not 'Else' Commented Dec 9, 2012 at 2:09
  • PHP is case-insensitive in regards to that. Commented Dec 9, 2012 at 2:09
  • Yeah , I agree , with you , but why am I getting a fatel error if I execute this on that line number as PHP depricated : some thing like that Commented Dec 9, 2012 at 2:14
  • and also you answer here is correct , but still I felt , even this might be a reason Commented Dec 9, 2012 at 2:14
  • I'm not getting such an error. Can you post the exact output of the error? Commented Dec 9, 2012 at 2:19

1 Answer 1

6
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'. That assignment will otherwise equal to true, reason the first part runs and the ELSE does not.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.