Functions Flashcards
Can other functions and class definitions appear inside a function?
Yes.
What is a valid function name?
A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
Can functions be referenced before they are defined?
Yes, except when a function is defined conditionally.
What is the scope for functions and classes?
All functions and classes in PHP have the global scope.
Can you undefine or redefine previously-declared functions?
No.
Are function names case-sensitive?
No.
Are function arguments passed by value or reference?
Value.
How can you allow a function to modify its arguments?
Pass the arguments by reference. To have an argument to a function always passed by reference, prepend an ampersand (&) to the argument name in the function definition.
What types are allowed for the default value of functions?
scalars (boolean, integer, float, string), arrays, and null.
It can’t be a variable, a class member, or a function call.
When using default arguments, any defaults should be on the ___ side of any non-default arguments.
Right.
How do variable-length argument lists in user-defined functions work?
They are implemented using the … token in PHP 5.6 and later, and using the func_num_args(), func_get_arg(), and func_get_args() functions in PHP 5.5 and earlier. It is also possible to add a type hint before the … token. If this is present, then all arguments captured by … must be objects of the hinted class. You may also pass variable arguments by reference by prefixing the … with an ampersand (&).
How do variable-length argument lists in user-defined functions work?
They are implemented using the … token in PHP 5.6 and later, and using the func_num_args(), func_get_arg(), and func_get_args() functions in PHP 5.5 and earlier. It is also possible to add a type hint before the … token. If this is present, then all arguments captured by … must be objects of the hinted class. You may also pass variable arguments by reference by prefixing the … with an ampersand (&).
What types may be returned in a function with a return statement?
Any type, including arrays and objects.
Can a function return multiple values?
No, but it can return an array.
How can you return a reference from a function?
Use the reference operator & in both the function declaration and when assigning the returned value to a variable.