Control Structures Flashcards

1
Q

What is a statement-group?

A

A group of statements encapsulated with curly braces, that itself is also a statement.

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

When can using ‘else if’ instead of ‘elsif’ be problematic?

A

When using a colon to define your if/elseif conditions. It will fail with a parse error.

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

What is the alternative syntax for control structures?

A

For if, while, for, foreach, and switch, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.

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

Any output (including whitespace) between a switch statement and the first case…

A

…will result in a syntax error.

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

Any output (including whitespace) between a switch statement and the first case…

A

…will result in a syntax error.

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

Any output (including whitespace) between a switch statement and the first case…

A

…will result in a syntax error.

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

What happens when an expression in a for loop is empty?

A

It’s skipped. expr2 being empty means the loop should be run indefinitely.

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

What happens when multiple expressions separated by commas are in a for loop?

A

In expr2, all expressions separated by a comma are evaluated but the result is taken from the last part.

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

What happens when multiple expressions separated by commas are in a for loop?

A

In expr2, all expressions separated by a comma are evaluated but the result is taken from the last part.

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

What does foreach work on?

A

Only on arrays and objects. When you try to use it on a variable with a different data type, or on an unitialized variable, it will issue an error.

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

Why don’t you need to call reset() before a foreach loop?

A

When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array.

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

How can you directly modify array elements within a foreach loop?

A

Precede $value with &, and the value will be assigned by reference. Like this:

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

How can you directly modify array elements within a foreach loop?

A

Precede $value with &, and the value will be assigned by reference. Like this:

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

Is ‘break 0’ or ‘continue 0’ valid now?

A

No.

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

Can you pass in variables to break or continue: break $num?

A

No.

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

When can a break statement end the entire script?

A

When it’s not in a control loop.

17
Q

What does continue do?

A

It’s used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

18
Q

What is the default value for continue or break?

A

1

19
Q

What is the default value for continue or break?

A

1

20
Q

In a switch statement, what are the limits on the case expression?

A

It can be any expression that evaluates to an integer or floating-point number or string.

21
Q

What are the directives you can set using declare?

A

Ticks and encoding.

22
Q

What can be given as directive values using declare?

A

Only literals, not variables or constants.

23
Q

What is a tick?

A

e

24
Q

What is a tick?

A

An event that occurs for every N low-level tickable statements executed by the parser within the declare block. The value for N is specified using ticks=N within the declare block’s directive section.

25
Q

How do you specify an event to occur on each tick?

A

Use register_tick_function().

26
Q

What does return do?

A

It’s a language construct that returns program control to the calling module. Execution resumes at the statement following the called module’s invocation.

27
Q

What happens if return is called from within a function?

A

It immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file.

28
Q

What happens if return is called from the global scope?

A

If called from the global scope, then execution of the current script file is ended. If the current script file was included or required, then control is passed back to the calling file. Furthermore, if the current script file was included, then the value given to return will be returned as the value of the include call. If return is called from within the main script file, then script execution ends. If the current script file was named by the auto_prepend_file or auto_append_file configuration options in php.ini, then that script file’s execution is ended.

29
Q

What is the difference between require and include?

A

Upon failure, require produces a fatal error AND a warning, whereas include produces only a warning. Otherwise they are identical.

30
Q

Where does include() check for files?

A

If the file isn’t found in the include_path, include will finally check in the calling script’s own directory and the current working directory before failing.

31
Q

What is the scope of an included file?

A

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

32
Q

What is the scope of an included file?

A

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

33
Q

What happens when a file is included from inside of a function?

A

If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function. An exception to this rule are magic constants which are evaluated by the parser before the include occurs.

34
Q

How is PHP parsing handled in included files?

A

When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

35
Q

What are URL include wrappers?

A

A means for allowing files to be included using a URL instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET.

36
Q

What does include return?

A

Include returns FALSE on failure and raises a warning. Successful includes, unless overridden by the included file, return 1

37
Q

Can you execute a return statement inside an included file in order to terminate processing in that file and return to the script which called it?

A

Yes. Also, it’s possible to return values from included files.

38
Q

Do you need parentheses around the argument to include?

A

No, because it is a special language construct.

39
Q

What is the goto operator used for?

A

The goto operator can be used to jump to another section in the program. The target point is specified by a label followed by a colon, and the instruction is given as goto followed by the desired target label. This is not a full unrestricted goto. The target label must be within the same file and context, meaning that you cannot jump out of a function or method, nor can you jump into one. You also cannot jump into any sort of loop or switch structure. You may jump out of these, and a common use is to use a goto in place of a multi-level break.