Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What does this syntax mean? 2

Status
Not open for further replies.

DaveC426913

Programmer
Jul 28, 2003
274
CA
What does this syntax mean:

function foo($bar=0) {
...
}

I don't understand the parameter. Is it equivalent to an assignment? as in:

function foo() {
$bar=0;
...
}

or perhaps, an optional param?:

function foo($bar) {
if (is_null($bar)){
bar = 0;
}
...
}


Here it is in context. The actual variable is $shift (in case that's a reserved variable or something):


$newCal->displayMonth($mes);

...

function displayMonth($shift=0) {
...
$firstday_month_ts = mktime(0,0,0,date("n")+$shift,1,date("Y")); // first day of the month
$lastday_month_ts = mktime(0,0,0,date("n")+$shift+1,0,date("Y")); // last day of the month
...
if ($shift==0 && $today_ts==$day_i_ts) {
}

#$mes would be an integer representing a month


 
If you call the function without specifying the parameter, in that example it is set to 0. In that case the parameter is not required to call the function.
 
That means that if no variable is passed to the function the default value of bar will be 0.

if you do this:
Code:
function foo($bar=0){
echo $bar;
}


and thne call foo:

foo();
$var="Somevalue";
foo($var);

this will output:

0
Somevalue

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
OK thanks. Then there must be some other reason why I'm seeing "" instead of 0.
 
Post the code and some test results then, I'm sure someone will spot it.
 
Actually, when I added this bit:

if ($shift==0){ echo "True"; }

I got "True".
 
I have noticed that sometimes when you print or echo variables that have the integer value of zero (or a boolean false) it doesnt print anything. Its kind of annoying if you ask me.

If I come across something that looks as if it might be doing something like that I usually turn the print statement into something like this:

print "|" . $shift . "|!" . (!$shift) . "|";

that way it will print out something no matter what.
 
Yes. I truncated it. It's actually:

if ($shift==0){ echo "True"; }
else{ echo "False"; }

The other thing I do is:

echo "[".$i."]";

That way, I can be sure I'm not just missing it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top