...

View Full Version : Question about arguments to functions



zingzangzung
08-10-2007, 11:43 PM
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?


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;
}
}

argothian
08-11-2007, 12:03 AM
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.

PhaZZed
08-11-2007, 12:41 AM
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/language.operators.logical.php

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

Good luck!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum