Control Structures Flashcards
What are some examples of statements?
An assignment, a function call, a loop, a conditional statement, a statement that does nothing (an empty statement), or a statement-group.
How many if statements can be nested?
They can be nested infinitely.
What is an easy way to execute conditional HTML/Javascript/CSS?
html code to run if condition is true
html code to run if condition is false
What is the alternate spelling of elseif?
else if
When will the “else if” spelling of elseif fail?
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;
?>
What is the alternative syntax for these control structures: if, while, for, foreach, 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;.
Can you mix alternative and standard control structure syntax in the same block?
No, it’s not supported.
Any output (including whitespace, but not a trailing newline) between a ‘switch’ statement and the first ‘case’ will result in…
…a syntax error.
Do ‘do while’ loops have an alternative syntax?
No.
When is the first expression in a for loop not evaluated?
Never. It’s evaluated once unconditionally at the beginning of the loop.
Can expressions in a for loop be empty?
Yes, they can all be empty. This is perfectly valid:
for( ; ;) {
}
Can expressions in a for loop contain multiple expressions?
Yes, separated by commas. This is valid:
for ($i = 1, $j = 0; $i
Foreach works only on…
…arrays and objects.
What happens when you try to use foreach on an uninitialized variable, or a non-array non-object type?
It issues an error.
When do you need to call reset() before a foreach loop?
Never. The internal array pointer is automatically reset to the first element of the array when foreach starts executing.
How can you directly modify array elements within a foreach loop?
Precede $value with &.
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
Will this work?
foreach (array(1, 2, 3, 4) as &$value) {
$value = $value * 2;
}
No. Referencing &$value is only possible if the iterated array can be referenced (i.e. if it is a variable).
When should you use unset() with a foreach loop?
After the foreach loop, if you’re referencing $value.
Can error messages be suppressed using the error suppression operator in a foreach loop?
No.
What happens when there are not enough array elements to fill list(), like here:
$array = (1,2,3);
list($a,$b,$c,$d);
A notice will be generated.
What array iteration ability was added in PHP 5.5?
The ability to iterate over an array of arrays and unpack the nested array into loop variables by providing list() as the value.
What does break end execution of?
The current for, foreach, while, do-while, or switch structure.
Is ‘break 0’ valid?
Not any longer; previously it was interpreted as ‘break 1’.
Can variables be passed to ‘break’ as the numerical argument?
Not any longer.
If a break statement is not in a loop, what will happen?
It will end the script.
Can ‘break’ be used in an included file?
No. It results in a fatal error.
Is the switch statement considered a looping structure?
For the purposes of ‘continue’, yes it is.
What is the default value for ‘continue’?
1
Is ‘continue 0’ valid?
No.
Can variables be passed in as the argument to continue?
No.