Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php question

Options
  • 07-09-2006 4:20pm
    #1
    Registered Users Posts: 1,086 ✭✭✭


    I've never quite understood how these if statements work but could someone please explain what going on here.

    I dont actually understand what the single "=" (equals) means in an if statment.

    Does it mean

    if $file1 is not false, then $lines1 = file($file1) therefore "Worked".
    else
    $lines1 = false; therefore ERROR.


    [PHP]
    if ($lines1 = file($file1))
    {
    echo "Worked";
    } else {
    //failed
    echo "ERROR";
    }
    [/PHP]


Comments

  • Registered Users Posts: 8,488 ✭✭✭Goodshape


    Fairly straightforward isn't it?

    if $lines1 is the same as file($file1) then echo "worked". If it isn't then eco "ERROR".

    What's the problem?


  • Registered Users Posts: 1,086 ✭✭✭Peter B


    I thought that was the case with 2 "=" (equals signs) but not with 1.

    I would understand that to be the case if I had written

    [PHP]
    if ($lines1 == file($file1))
    {
    echo "Worked";
    } else {
    //failed
    echo "ERROR";
    } [/PHP]

    Maybe it is the same in PHP, I'm not sure


  • Registered Users Posts: 8,488 ✭✭✭Goodshape


    Hmm, right you are acutally. Two equels are better than one.

    I'm not actually sure what the single "=" does.. prehaps nothing. It could be that the two equel signs are used so as to keep in line with "=<" (equel to or less than) and so forth.


  • Registered Users Posts: 180 ✭✭marcphisto


    = is an assignment operator
    == is a comparison operator

    Think of it this way

    X = Y is placing the contents of Y into X

    whereas

    X == Y is making a comparison between X and Y

    If you put X = Y into and if statement it will always return true.


  • Closed Accounts Posts: 169 ✭✭akari no ryu


    It depends on what you're trying to do.
    Let's look at an example.

    [php]
    class test{
    var $prop;
    function test($prop=null){
    $this->prop=$prop;
    }

    function setProp($prop){
    $this->prop=$prop;
    }

    function getProp(){
    return $this->prop;
    }
    }

    $test1=new test();
    $test2=new test("blah");

    if($test1Prop=$test1->getProp()){
    echo "got test 1 prop: $test1Prop";
    }

    if($test2Prop=$test2->getProp(){
    echo "got test 2 prop: $test2Prop";
    }
    [/php]

    Essentially, when an assignation fails ($a=doSomething()) the if statement returns false.


  • Advertisement
  • Closed Accounts Posts: 169 ✭✭akari no ryu



    no it won't
    [php]if($result=mysql_query($someSQL)){
    echo "query succeeded";
    }else{
    echo "query failed";
    }[/php]


  • Registered Users Posts: 1,086 ✭✭✭Peter B


    Hmm think I have found the solution.

    Since you are assigning x = y
    x will always be assigned value of y, therefore both values will be the same
    So if y is equal to "", or y is equal to 0, or y is equal to FALSE, then it will be the same as

    [PHP]if (FALSE)
    {

    } else{

    }[/PHP]

    I think by using the single "=" in an if statment just allows you to assign at the same time as checking if it is true or not.

    I understand exactly the same functioning code would be

    [PHP]
    $lines1 = file($file1);
    if ($lines1)
    {
    echo "Worked";
    } else {
    //failed
    echo "ERROR";
    } [/PHP]


  • Closed Accounts Posts: 169 ✭✭akari no ryu


    No.
    With assigning, if the assigning happened, it's true. If, for some reason, the assigning didn't happen, then the assignation didn't happen so it's false.

    Take for example the mysql_query.

    If there's something wrong with the query, the assignation $result=mysql_query($someSQL) will not have happened.

    The if will return false.

    If is just
    if(whatever's in here is true){
      block
    }else
      block
    }
    

    Assignations do not always return true.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    No.
    With assigning, if the assigning happened, it's true. If, for some reason, the assigning didn't happen, then the assignation didn't happen so it's false.
    Well, yes and no.

    Basically, when you insert an assignment operator into an if() statement, the preprocessor evaluates the statement inside the if() conditional, and then tests the value of the item which is being assigned. If the value of the item assigned = true, then the condition is true.

    That is, imagine you have

    if($x = someFunction())

    PHP will carry out someFunction(), assign the return value into the $x variable, and then test the value of $x for true or false.


  • Registered Users Posts: 1,086 ✭✭✭Peter B


    No.
    With assigning, if the assigning happened, it's true. If, for some reason, the assigning didn't happen, then the assignation didn't happen so it's false.

    Thanks for the help akari no ryu

    However since I am the OP and was asking the question in the first place I should not really be disagreeing with your comment but if I run the code below the assigning does happen, $hmmm turnes to 0, however the if statment does read it as FALSE.

    [PHP]
    $hmmm=0;
    if ($lines1 = $hmmm)
    {
    echo "Worked";
    } else {
    //failed
    echo "ERROR";
    }
    echo "<br> Lines1 ->".$lines1."<-";
    if (!$lines)
    {
    echo "<br>FALSE";
    }
    [/PHP]

    This code echos
    [PHP]ERROR
    Lines1 ->0<-
    FALSE[/PHP]


  • Advertisement
  • Registered Users Posts: 4,188 ✭✭✭pH


    [php]$hmmm = 0;[/php]

    [php]if ($lines1 = $hmmm)[/php] Will equate to FALSE
    if (0) is always false.

    [PHP]if (!$lines)[/PHP] Will equate to TRUE

    see here:
    http://www.zend.com/manual/types.comparisons.php
    (Last column of the first table on that page)


  • Registered Users Posts: 5,335 ✭✭✭Cake Fiend


    Take for example the mysql_query.

    If there's something wrong with the query, the assignation $result=mysql_query($someSQL) will not have happened.

    Not quite - mysql_query() returns FALSE on error, so the assignment will still take place, assigning $result the value FALSE. $result is then evaluated as Seamus says, and the if statement fails.


  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    And while we're at it there's '===' which also compares type, eg
    [PHP]'1' == 1 //returns true though one is a string type and the other is numeric
    '1' === 1 //returns false
    [/PHP]


Advertisement