Is this legal?
<?php
function ftw($foo = 'pwnage', $nub = MENU_DEFAULT_VALUE, $odp = ODP_DEFAULT_VALUE) {
//lots_of_awesome_code
}
?>
where MENU_DEFAULT_VALUE
and ODP_DEFAULT_VALUE
are constants defined previously in the file.
The constant() function returns the value of a constant. Note: This function also works with class constants.
PHP - Class ConstantsClass constants can be useful if you need to define some constant data within a class. A class constant is declared inside a class with the const keyword. Class constants are case-sensitive. However, it is recommended to name the constants in all uppercase letters.
By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference. $str = 'This is a string, '; add_some_extra($str);
Basic Usage of the const Keyword to Declare Constants For this example, we will declare a new constant using PHP's const keyword. We will name this constant “ WEBSITE ” and assign it the value “ pimylifeup.com “. Once declared, we will use the echo function to print the value of the constant to the screen.
Yes, that is legal.
From the manual:
The default value must be a constant expression, not (for example) a variable, a class member or a function call.
Constants fit that bill perfectly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With