Control Structures Flashcards

1
Q

What are some examples of statements?

A

An assignment, a function call, a loop, a conditional statement, a statement that does nothing (an empty statement), or a statement-group.

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

How many if statements can be nested?

A

They can be nested infinitely.

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

What is an easy way to execute conditional HTML/Javascript/CSS?

A

html code to run if condition is true

html code to run if condition is false

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

What is the alternate spelling of elseif?

A

else if

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

When will the “else if” spelling of elseif fail?

A

When using a colon to define your if/else conditions:

 $b):
    echo $a." is greater than ".$b;
else if($a == $b): // Will not compile.
    echo "The above line causes a parse error.";
endif;
/* Correct Method: */
if($a > $b):
    echo $a." is greater than ".$b;
elseif($a == $b): // Note the combination of the words.
    echo $a." equals ".$b;
else:
    echo $a." is neither greater than or equal to ".$b;
endif;

?>

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

What is the alternative syntax for these control structures: if, while, for, foreach, switch.

A

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;.

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

Can you mix alternative and standard control structure syntax in the same block?

A

No, it’s not supported.

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

Any output (including whitespace, but not a trailing newline) between a ‘switch’ statement and the first ‘case’ will result in…

A

…a syntax error.

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

Do ‘do while’ loops have an alternative syntax?

A

No.

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

When is the first expression in a for loop not evaluated?

A

Never. It’s evaluated once unconditionally at the beginning of the loop.

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

Can expressions in a for loop be empty?

A

Yes, they can all be empty. This is perfectly valid:

for( ; ;) {
}

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

Can expressions in a for loop contain multiple expressions?

A

Yes, separated by commas. This is valid:

for ($i = 1, $j = 0; $i

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

Foreach works only on…

A

…arrays and objects.

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

What happens when you try to use foreach on an uninitialized variable, or a non-array non-object type?

A

It issues an error.

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

When do you need to call reset() before a foreach loop?

A

Never. The internal array pointer is automatically reset to the first element of the array when foreach starts executing.

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

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

A

Precede $value with &.

$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}

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

Will this work?

foreach (array(1, 2, 3, 4) as &$value) {
$value = $value * 2;
}

A

No. Referencing &$value is only possible if the iterated array can be referenced (i.e. if it is a variable).

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

When should you use unset() with a foreach loop?

A

After the foreach loop, if you’re referencing $value.

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

Can error messages be suppressed using the error suppression operator in a foreach loop?

A

No.

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

What happens when there are not enough array elements to fill list(), like here:

$array = (1,2,3);
list($a,$b,$c,$d);

A

A notice will be generated.

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

What array iteration ability was added in PHP 5.5?

A

The ability to iterate over an array of arrays and unpack the nested array into loop variables by providing list() as the value.

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

What does break end execution of?

A

The current for, foreach, while, do-while, or switch structure.

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

Is ‘break 0’ valid?

A

Not any longer; previously it was interpreted as ‘break 1’.

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

Can variables be passed to ‘break’ as the numerical argument?

A

Not any longer.

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

If a break statement is not in a loop, what will happen?

A

It will end the script.

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

Can ‘break’ be used in an included file?

A

No. It results in a fatal error.

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

Is the switch statement considered a looping structure?

A

For the purposes of ‘continue’, yes it is.

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

What is the default value for ‘continue’?

A

1

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

Is ‘continue 0’ valid?

A

No.

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

Can variables be passed in as the argument to continue?

A

No.

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

What happens if you include multiple default cases in a switch statement?

A

It raises an E_COMPILE_ERROR error.

32
Q

Why is using ‘break’ so important in a switch statement?

A

When a case statement is found with a value that matches the value of the switch expression, PHP continues to execute statements until the end of the switch block, or the first time it sees a ‘break’ statement.

33
Q

In a switch statement, what happens if the statement list for a case is empty?

A

It simply passes control into the statement list for the next case.

34
Q

How does the default case in a switch statement work?

A

The default case matches anything that wasn’t matched by the other cases.

35
Q

In a switch statement, what can the case expression be?

A

Any expression that evaluates to a simple type: integer, floating-point number, string.

36
Q

In a switch statement, what can be used instead of a colon?

A

A semicolon.

37
Q

What is the difference between a series of if statements and a switch statement?

A

The expression you’re comparing with is evaluated only once in a switch statement.

38
Q

What is the ‘declare’ construct used for?

A

It’s for setting execution directives for a block of code. Here’s the syntax:

declare (directive)
statement

39
Q

What are the two directives accepted in a declare construct?

A

ticks and encoding

40
Q

What can be given as directive values in a declare statement?

A

Only literals.

41
Q

Can the ‘declare’ construct be used in the global scope?

A

Yes.

42
Q

If a file with a ‘declare’ construct is included, does it affect the parent file?

A

No.

43
Q

Is the statement part of a declare block executed?

A

Yes.

44
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.

45
Q

Typically, what’s not tickable?

A

Condition expressions and argument expressions.

46
Q

How are the events that occur on each tick specified?

A

By using the register_tick_function().

47
Q

Can more than one event occur for each tick?

A

Yes.

48
Q

How can a script’s encoding be specified per-script?

A

By using the encoding directive.

49
Q

When combined with namespaces, what is the only legal syntax for declare?

A

declare(encoding=’…’), and NOT declare(encoding=’…’){}, which will result in a parse error when combined with namespaces.

50
Q

What does ‘return’ do?

A

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

51
Q

Besides ending execution of the current function, and returning its argument as the value of the function call, what else can return end?

A

The execution of an eval() statement or script file.

52
Q

What exactly is ‘return’?

A

A language construct. That’s why the parentheses surrounding its arguments are not required.

53
Q

What happens when you call return with parentheses but no arguments?

A

A parse error.

54
Q

Why shouldn’t you use parentheses around your return variable when returning by reference?

A

It won’t work. You can only return variables by reference, and not the result of a statement. If you use return($a), you’re not returning a variable, but the result of the expression ($a), which is of course the value of $a.

55
Q

What happens when return is called in the global scope, from an included or required script file?

A

Control is passed back to the calling file. If the current script file was included, then the value given to ‘return’ will be returned as the value of the include call. Any functions defined in the included file will still be available, even if the file is not executed.

56
Q

What happens if ‘return’ is called from within the main script file?

A

Script execution ends.

57
Q

What happens if ‘return’ is called from within a script file that was named by the auto_prepend_file or auto_append_file configuration options in php.ini?

A

That script file’s execution is ended.

58
Q

What is the difference between ‘return’ and ‘exit’?

A

Return just exits execution of the current script, whereas ‘exit’ exits the whole execution.

59
Q

How is require different from include?

A

Upon failure, require produces a fatal E_COMPILE_ERROR level error, whereas include only emits an E_WARNING, which allows the script to continue.

60
Q

Is require a statement or a function?

A

It’s a statement and/or language construct.

61
Q

When using include or require, where does PHP look for the required or included file?

A

First it looks at the exact file path given. Then in checks the include_path specified. Then it checks in the calling script’s own directory and the working directory.

62
Q

When using include or require, when is the include_path ignored altogether?

A

When the path is defined, whether absolute, or relative to the current directory.

63
Q

What is the scope of a required or included file?

A

When a file is included, the code it contains inherits the variable scope of the line on which the include or require occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward.

64
Q

Functions and classes defined in an included file have what scope?

A

Global.

65
Q

What happens if an include occurs inside a function within the calling file?

A

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. The exception to this rule is magic constants, which are evaluated by the parser before the include occurs.

66
Q

How does PHP/HTML parsing work for included or required files?

A

When a file is included/required, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end.

67
Q

How can you specify a file to be included or required via a URL?

A

If ‘URL include wrappers’ are enabled, you can specify the file to be included using a URL.

68
Q

How can you pass variables to an included or required file when that file is specified as a url? How is this different from including the file locally and having it inherit the parent file’s variable scope.

A

You can pass variables using a URL request string, as with HTTP GET.

It’s different because the script is actually being run on the remote server, and the result is then being included into the local script.

69
Q

Why is readfile() better than including/requiring via HTTP?

A

It’s more secure. If a file from a remote server needs to be processed there and outputted only, readfile is a better function to use.

70
Q

What does include return?

A

FALSE on failure (and raises a warning). 1 on success.

71
Q

Can you return values from included files/

A

Yes, but not when including remote files, unless the output of the remote file has valid PHP start and end tags (as with any local file).

72
Q

Are parentheses needed around the argument to include? or require.

A

No.

73
Q

If functions are defined in an included file, can they be used in the main file independent if they are before return or after?

A

Yes. However, if the file is included twice, PHP 5 issues a fatal error because the functions were already declared.

74
Q

How can you automatically include files within scripts?

A

Use the auto_prepend_file and auto_append_file configuration options in php.ini.

75
Q

How does the ‘goto’ operator work?

A

It’s used to jump to another section in a 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.

76
Q

What are some limitations on the ‘goto’ operator?

A
  • The target label must be within the same file and context (you cannot jump out of a function or method, nor can you jump into one).
  • You cannot jump into any sort of loop or switch structure.
  • You MAY jump OUT of a loop or switch structure.
77
Q

What happens if you try to use the goto operator to jump into a loop or switch structure?

A

A fatal error.