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.
If ‘return’ is omitted from a function, what is returned?
NULL
How do you return a reference from a function?
Use the ampersand reference operator in both the function declaration and when assigning the returned value to a variable:
function &returns_reference() { return $someref; }
$newref =& returns_reference();
What are variable functions?
If a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it:
$func = ‘foo’;
$func(); // This calls foo()
Variable functions won’t work with…
…language constructs such as echo, print, unset(), isset(), empty(), include, require, and the like.
Can object methods be called with the variable functions syntax?
Yes:
$foo = new Foo(); $funcname = "Variable"; $foo->$funcname(); // This calls $foo->Variable()
What will show which functions are loaded into PHP?
A call to phpinfo() or get_loaded_extensions().
What happens if the parameters given to a function are not what it expects?
The return value of the function is undefined (usually it will return NULL).
What are closures?
Also known as anonymous functions, they allow the creation of functions which have no specified name. They are most useful as the value of callback parameters.
Can closures be used as the value of variables?
Yes. PHP automatically converts such expressions into instances of the Closure internal class.