Jump to content

[SOLVED] Comparison Operators experiment, need help


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");

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.