Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question about arguments to functions

    I am learning php using PHP and MySQL Web Development 2nd Ed by Welling/Thomson, and tried out some from page 160 (very slightly amended). It is part of a class definition. It works ok and I understand most of it but a couple of bits of syntax confuse me. In the snippet below there are three functions. In the first I call function displayMenuItem, with three arguments. Why does the third argument have the exclamation mark before it? Does that mean NOT the return from that function? (ie switching false to true or vice versa)
    Then in the actual function displayMenuItem, the arguments it accepts has $active=true, but then the function begins with if ($active). Doesn't that mean "if $active is true"?
    I am getting on fine with php on the whole, but the logic of this is really confusing me. Can anyone help?
    PHP Code:
    function displayMenu($links){
        while(list(
    $name$url) = each($links))
        {
            
    $this->displayMenuItem($name$url, !$this->IsURLCurrentPage($url));
        }
    }

    function 
    IsURLCurrentPage($url){
        if(
    strpos$GLOBALS['SCRIPT_NAME'], $url)==false){
            return 
    false;
        }
        else{
            return 
    true;
        }
    }

    function 
    displayMenuItem($name$url$active=true){
        if (
    $active){
            echo 
    "<a href='$url'>$name</a><br/>";
        }
        else{
            echo 
    $name;
        }


  • #2
    New Coder
    Join Date
    Apr 2007
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You are correct the ! means NOT so a true value gets turned false and a false get turned true.

    The $active=true means that active will be set true if no value is set for it when the function is called, and the if statement always checks for a true value of whatever is in the (). So yes it checks to see if active is true.

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Logical operators are so important in programming. I would suggest taking a look around for some good information on: and, or, not logical operators.

    http://www.phpbuilder.com/manual/lan...rs.logical.php

    ^ Not the best resource, but it's a decent summary.

    Good luck!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •