Functions Flashcards Preview

PHP Certification > Functions > Flashcards

Flashcards in Functions Deck (22)
Loading flashcards...
1
Q

Can other functions and class definitions appear inside a function?

A

Yes.

2
Q

What is a valid function name?

A

A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

3
Q

Can functions be referenced before they are defined?

A

Yes, except when a function is defined conditionally.

4
Q

What is the scope for functions and classes?

A

All functions and classes in PHP have the global scope.

5
Q

Can you undefine or redefine previously-declared functions?

A

No.

6
Q

Are function names case-sensitive?

A

No.

7
Q

Are function arguments passed by value or reference?

A

Value.

8
Q

How can you allow a function to modify its arguments?

A

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.

9
Q

What types are allowed for the default value of functions?

A

scalars (boolean, integer, float, string), arrays, and null.

It can’t be a variable, a class member, or a function call.

10
Q

When using default arguments, any defaults should be on the ___ side of any non-default arguments.

A

Right.

11
Q

How do variable-length argument lists in user-defined functions work?

A

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 (&).

12
Q

How do variable-length argument lists in user-defined functions work?

A

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 (&).

13
Q

What types may be returned in a function with a return statement?

A

Any type, including arrays and objects.

14
Q

Can a function return multiple values?

A

No, but it can return an array.

15
Q

How can you return a reference from a function?

A

Use the reference operator & in both the function declaration and when assigning the returned value to a variable.

16
Q

What are variable functions?

A

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. Among other things, this can be used to implement callbacks, function tables, and so forth. Object methods can also be called with the variable functions syntax.

17
Q

How can you display which extensions are loaded into PHP?

A

Call phpinfo() or get_loaded_extensions().

18
Q

What happens if the parameters given to a function are not what it expects?

A

The return value of the function is undefined. It’s often NULL, but that’s just a convention, and cannot be relied upon.

19
Q

What are anonymous functions?

A

Also known as “closures”, they allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses.

20
Q

Can closures be used as the values of variables?

A

Yes. PHP automatically converts such expressions into instances of the Closure internal class. Assigning a closure to a variable uses the same syntax as any other assignment, including the trailing semicolon.

21
Q

Can closures inherit variables from the parent scope?

A

Yes. Any such variables must be passed to the use language construct.

22
Q

What is the parent scope of a closure?

A

The function in which the closure was declared (not necessarily the function it was called from).