tags:

views:

98

answers:

2
<?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.

+1  A: 

You could use the isset function

if ($creation_year == $current_year || !isset($creation_year)){
 //...
}
CMS
That is not a variable definition but an argument with default value.
Milen A. Radev
Unfortunately, if you have warnings turned on, leaving off function arguments generates warnings. As long as we have armies of programmers willing to write warning/notice generating code due to laziness, it will continue to call into question PHP's suitability as a serious language.
Sean McSomething
+2  A: 

Make $creation_year an optional argument with a default value of NULL.

function copyright_date($creation_year=NULL) {

Now inside the function just test if $creation_year is equal to NULL, which will happen when the function is called with no arguments.

yjerem