Jump to content

[SOLVED] Comparison Operators experiment, need help


.evo.

Recommended Posts

This is more of an experiment than anything, but I was making a function so that you enter a year as the variable, and if the year you entered was the current year, it would just echo the current year. If not it would echo the year chosen - current year. eg. "cpyrht(2003);" would show up as "2003 - 2008" but if you entered "cpyrht(2008);" it would show up as "2008" because it is the current year. Anyway the code I have at the moment, is not working, it's not a syntax error it's just not functioning. Wherever I enter the function the div that it's placed in just disappears.

 

$curyr = date(Y);

function cpyrht($yr) {
$cpyrht = ($yr==$curyr)?"$yr":"$yr - $curyr";
echo $cpyrht;
}

 

Thanks. :)

$curyr has to be defined within function. Read on variable scope in manual

 

function cpyrht($yr) {
   $curyr = date(Y);
   $cpyrht = ($yr==$curyr)?"$yr":"$yr - $curyr";
   return $cpyrht;
}

echo cpyrht("2008");
echo cpyrht("2003");

Ok I just got the function working in an if statement. Now I just gotta try get that into a working comparison operator.

 

function cpyrht($yr) {
$sep = " - ";
$curyr = date(Y);
if ($yr==$curyr) {
	echo $curyr;
} else {
	echo $yr, $sep, $curyr; 
}
}

cpyrht("2003");
cpyrht("2008");

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.