<?php /* Copyright Date --------------------------*/ function copyright_date($creation_year) { $current_year = date('Y'); if ($creation_year == $current_year || $creation_year == '') { echo $current_year; } else { echo $creation_year . '-' . $current_year; } } ?>
If someone forgets to add the argument (the year the website was created), e.g.
<?php copyright_date(); ?>
instead of:
<?php copyright_date(2009); ?>
how would I test to see if the argument was left blank? In my if statement, that’s what $creation_year == ” is for, but since the argument isn’t a string, it doesn’t work.
Make
$creation_yearan optional argument with a default value ofNULL.Now inside the function just test if
$creation_yearis equal toNULL, which will happen when the function is called with no arguments.