Functions Flashcards
Can other functions and class definitions appear inside of a function?
Yes.
What are the rules for function names?
Same as other labels in PHP: starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
When must functions be defined before they are referenced?
1. When a function is defined in a conditional manner: $a = true; foo(); //will not work yet if($a == true) { function foo() { print "foo"; } } foo(); //now it works
- When a function is defined inside of another function which has not yet been called.
function foo() { function bar() { echo "I don't exist until foo() is called.\n"; } }
/* We can't call bar() yet since it doesn't exist. */
foo();
/* Now we can call bar(),
foo()’s processing has
made it accessible. */
bar();
All functions and classes in PHP have what scope?
Global.
If a function or class was defined inside of a function, can it be called outside of it?
Yes, and vice-versa.
Are function names case sensitive?
No.
Can you call recursive functions in PHP?
Yes, but avoid over 100-200 recursion levels, as it can cause a termination of the current script.
By default, function arguments are passed by ____
Value.
To allow a function to modify its arguments…
…the arguments must be passed by reference.
To have an argument to a function ALWAYS be passed by reference…
…prepend an ampersand to the argument name in the function definition.
What are the limits on a default value for a function?
It must be a constant expression, not (for example) a variable, a class member, or a function call.
When using default arguments in a function, any defaults should be…
…on the right side of any non-default arguments.
Can arguments to a function that are passed by reference have a default value?
Yes.
How does PHP support variable-length argument list for functions?
With the func_num_args(), func_get_arg(), and func_get_args() functions for PHP 5.5 and earlier.
What types can be returned from a function?
Any type.