Week 8 - PHP Part 2 Flashcards
What’s the syntax for defining and invoking functions?
function "functionname"() { what the function does }
functionname();
What are the 3 types of scopes? How are they different?
Variable, local and global.
Variable is where a variable can be used. It can be either global (in all functions throughout the code) or local (within a function where it was declared)
Superglobals
A global variable which is automatically declared by PHP.
How do we validate forms with PHP?
Forms are validated in PHP using regular expressions, isset and superglobals.
How do we implement regular expressions in PHP?
By using preg_match.
preg_match(string$pattern, string$subject)
The first parameter determines the pattern, similar to HTML pattern attribute, then the second parameter tells us which string we are going to match the pattern against.
isset()
Used to check if a variable is assigned before using it.
What are examples of superglobals?
$_GET and $_POST
while Statement
An infinite loop that will execute while a condition is true.
$count = 1;
while ($count <= 10) {
echo “The number is $count”;
}
do…while Statement
A loop statement that will run at least once. Similar to an until statement, where the code will continue running until a certain condition is met.
$count = 2; do { echo "<p>The count is equal to" . $count . "</p>"; $count++; } while ($count < 2);
for Statement
A loop statement which will run for a certain condition evaluating to true, and will end when it becomes false.
$fastFoods = array(“pizza”, “burgers”, “french fries”, “tacos”, “fried chicken”);
for ($count = 0; $count < 5; $count++) {
echo $fastFoods[$count], “<br></br>”;
}
foreach Statement
An iteration statement used to loop through an array.
foreach ($array_name as $variable_name) {
statements;
}
if & if else Statement
A conditional statement using else for any alternatives.
if (conditional expression)
statement;
else
statement;
switch Statement
A conditional statement with multiple cases, and a default statement if none of the conditions match the expression.
switch (expression) { case label: statement(s); break; case label: statement(s); break; ... default: statement(s); }
novalidate
A HTML attribute used in the form tag to indicate that the form is not to be validated on submit to check that PHP validation is working.
Ensures that validation isn’t being taken place on HTML side.