Functions Flashcards

1
Q

Can other functions and class definitions appear inside of a function?

A

Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the rules for function names?

A

Same as other labels in PHP: starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When must functions be defined before they are referenced?

A
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
  1. 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();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

All functions and classes in PHP have what scope?

A

Global.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If a function or class was defined inside of a function, can it be called outside of it?

A

Yes, and vice-versa.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Are function names case sensitive?

A

No.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can you call recursive functions in PHP?

A

Yes, but avoid over 100-200 recursion levels, as it can cause a termination of the current script.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

By default, function arguments are passed by ____

A

Value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

To allow a function to modify its arguments…

A

…the arguments must be passed by reference.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

To have an argument to a function ALWAYS be passed by reference…

A

…prepend an ampersand to the argument name in the function definition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the limits on a default value for a function?

A

It must be a constant expression, not (for example) a variable, a class member, or a function call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When using default arguments in a function, any defaults should be…

A

…on the right side of any non-default arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can arguments to a function that are passed by reference have a default value?

A

Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does PHP support variable-length argument list for functions?

A

With the func_num_args(), func_get_arg(), and func_get_args() functions for PHP 5.5 and earlier.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What types can be returned from a function?

A

Any type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If ‘return’ is omitted from a function, what is returned?

A

NULL

17
Q

How do you return a reference from a function?

A

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();

18
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:

$func = ‘foo’;
$func(); // This calls foo()

19
Q

Variable functions won’t work with…

A

…language constructs such as echo, print, unset(), isset(), empty(), include, require, and the like.

20
Q

Can object methods be called with the variable functions syntax?

A

Yes:

$foo = new Foo();
$funcname = "Variable";
$foo->$funcname();  // This calls $foo->Variable()
21
Q

What will show which functions are loaded into PHP?

A

A call to phpinfo() or get_loaded_extensions().

22
Q

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

A

The return value of the function is undefined (usually it will return NULL).

23
Q

What are closures?

A

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.

24
Q

Can closures be used as the value of variables?

A

Yes. PHP automatically converts such expressions into instances of the Closure internal class.

25
Q

How do you assign a closure to a variable?

A

With the same syntax as any other assignment, including the trailing semicolon.

26
Q

Can closures also inherit variables from the parent scope?

A

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

27
Q

What are the two applications of the ‘use’ keyword?

A

It applies to namespace constructs and to closure constructs.

28
Q

When the ‘use’ keyword is used in a closure, to inherit a variable, can that variable later be changed, outside of the closure?

A

No. An inherited variable’s value is from when the function is defined, not when called. You can change this behavior by inheriting by reference.

29
Q

Can closures accept regular arguments?

A

Yes:

// Closures can also accept regular arguments
$example = function ($arg) use ($message) {
    var_dump($arg . ' ' . $message);
};
$example("hello");
30
Q

What is the difference between inheriting variables from the parent scope and using global variables?

A

Global variables exist in the global scope, which is the same no matter what function is executing. The parent scope of a closure is the function in which the closure was declared (not necessarily the function it was called from).

31
Q

Is it possible to use func_num_args(), func_get_arg(), and func_get_args() from within a closure.

A

Yes.