3. Functions Flashcards

1
Q

How do you define a function in PHP?

A
function funcName() {
 // Statements here.
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you write a PHP function that’s just short hand for some HTML?

A

Define the function, then exit PHP, write your HTML, then go back into PHP and finish the function.

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

What happens if you nest functions?

A
  • Inner functions do NOT gain visibility to outer function arguments.
  • Inner function also cannot be called until outer function has been called.
  • Inner function cannot be used UNTIL outer function has been called at least once.

Generally, this is a bad idea to do.

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

When is a static variable initialized within a function?

A

Only once when it’s first called.

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

What are the two ways you can pass parameters to functions?

A

Value – Copy is passed in.

Reference – Pointer to the value is passed, modifications are reflected to the caller.

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

How do you pass a parameter to a function by reference?

A

Add an & before the parameter name.

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

How do you define a default parameter for a function?

A

Assign it in the function definition line. The only rule for this is all optionals need to be AFTER all required parameters.

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

How do you make a function with a variable number of parameters?

A

First, create a function with no parameters. Then within your function use the following functions to retrieve arguments.

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

What’s important to note about the variable parameters functions?

A

They can’t be directly passed to another function. See image.

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

What happens if you just DON’T pass some parameters to a function?

A

Those parameters will remain unset, and a warning is generated. It will still call the function though.

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

How can you use type hinting in a function definition?

A

State the type before the name of the parameter.

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

What’s the type of a function in PHP?

How can you use type hinting to get something you can invoke?

A

Set the type as callable.

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

How can you set the return data type of a function?

A

At the end of the function prototype, add a colon and the type

: int

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

When should you use return by reference for functions?

A

ONLY when you have a good technical reason for doing so. The PHP engine will already optimize, so don’t consider optimization.

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

What is a variable function?

A

A function that’s referenced by a variable, maybe passed to another function as a callback, that can then be called.

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

How can you test that a function exists? When is it good to use this?

A

Use the function_exists() function.

This is really useful when you’re using a variable function, and you want to make sure the callback you’ve been given exists before calling it so you don’t throw a function not found error.

17
Q

How can anonymous functions use variables in their enclosing scope?

A

by using the using construct and listing what it will be using from the enclosing scope.