PHP Control Structures and Loops Flashcards
(25 cards)
What is a control structure in PHP?
A control structure directs the flow of a program using conditionals or loops.
Control structures like if or for decide which code runs or how often. In WordPress, they’re used in templates (e.g., checking if a post exists). Freelancers use them for dynamic features, while enterprise architects optimize control flow for scalable systems.
What is an if statement in PHP?
An if statement executes code if a condition is true, like if ($x > 0) { echo “Positive”; }.
Common in WordPress for conditional logic (e.g., if (is_single())). Freelancers use if to tailor client output, while enterprise architects ensure efficient conditionals for high-traffic applications.
What is an else statement in PHP?
An else statement runs code if an if condition is false, like else { echo “Non-positive”; }.
In WordPress, else handles fallback content (e.g., “No posts found”). Freelancers use it for alternative logic, while enterprise architects maintain clear, maintainable code paths.
What is an elseif statement in PHP?
An elseif checks additional conditions, like elseif ($x == 0) { echo “Zero”; }.
Used in WordPress for multiple conditions (e.g., checking user roles). Freelancers implement elseif for complex logic, while enterprise architects streamline it for readability and performance.
What is a switch statement in PHP?
A switch statement selects code blocks based on a variable’s value, like switch ($day) { case “Monday”: echo “Start”; break; }.
In WordPress, switch handles multiple cases (e.g., post types). Freelancers use it for structured logic, while enterprise architects prefer it over nested if for clarity.
What is the break keyword in a switch statement?
break exits a switch case, preventing fall-through, like case “Monday”: echo “Start”; break;.
Ensures only the matched case runs. In WordPress, it’s used in plugins for clean case handling. Freelancers apply break for control, while enterprise architects ensure no unintended execution.
What is the default case in a switch statement?
default runs if no case matches, like default: echo “Unknown”;.
In WordPress, default handles unexpected values (e.g., invalid settings). Freelancers use it for robust code, while enterprise architects ensure fallback logic for edge cases.
What is a for loop in PHP?
A for loop repeats code a set number of times, like for ($i = 0; $i < 5; $i++) { echo $i; }.
Used in WordPress to iterate over arrays (e.g., post lists). Freelancers use for for precise iteration, while enterprise architects optimize loops for performance in large datasets.
What are the parts of a for loop?
Initialization, condition, and increment, like $i = 0, $i < 5, $i++.
These control the loop’s behavior. In WordPress, for loops process fixed-size data (e.g., pagination). Freelancers structure loops for clarity, while enterprise architects minimize overhead.
What is a while loop in PHP?
A while loop runs as long as a condition is true, like while ($x < 5) { echo $x; $x++; }.
In WordPress, while is used in the WordPress Loop (while (have_posts())). Freelancers use it for dynamic iteration, while enterprise architects ensure termination to prevent infinite loops.
What is a do-while loop in PHP?
A do-while loop runs at least once, then continues if a condition is true, like do { echo $x; $x++; } while ($x < 5);.
Less common in WordPress but useful for guaranteed execution. Freelancers use it for specific client needs, while enterprise architects avoid it unless necessary for efficiency.
What is a foreach loop in PHP?
A foreach loop iterates over array elements, like foreach ($array as $value) { echo $value; }.
In WordPress, foreach processes arrays (e.g., $posts in a query). As you’ve worked with arrays, freelancers use it for client data, while enterprise architects optimize it for large arrays.
How do you use foreach with key-value pairs?
Use foreach ($array as $key => $value), like foreach ($options as $id => $name) { echo “$id: $name”; }.
Common in WordPress for associative arrays (e.g., plugin settings). Freelancers use it for structured output, while enterprise architects ensure efficient key handling.
What is the break keyword in a loop?
break exits a loop immediately, like if ($x > 10) { break; } in a for loop.
In WordPress, break stops loops early (e.g., limiting posts). Freelancers use it for control, while enterprise architects ensure it’s used judiciously to avoid logic errors.
What is the continue keyword in a loop?
continue skips the current loop iteration, like if ($x % 2 == 0) { continue; }.
In WordPress, continue skips posts in a loop (e.g., excluding categories). Freelancers use it for filtering, while enterprise architects ensure it doesn’t disrupt loop flow.
What is a nested loop in PHP?
A nested loop is a loop inside another, like for ($i = 0; $i < 3; $i++) { for ($j = 0; $j < 2; $j++) { echo “$i,$j”; } }.
Used in WordPress for multi-dimensional arrays (e.g., menu items). Freelancers use nested loops for complex data, while enterprise architects optimize them to avoid performance issues.
What is an infinite loop in PHP?
An infinite loop runs indefinitely, like while (true) { echo “Loop”; }.
Rare in WordPress but can occur in debugging. Freelancers must avoid infinite loops, while enterprise architects implement safeguards to prevent server crashes.
How do you exit an infinite loop?
Use break or a condition, like if ($x > 100) { break; } in a while (true) loop. In WordPress, conditions ensure loop termination (e.g., have_posts()). Freelancers test loop exits, while enterprise architects enforce safe loop design.
What is the goto statement in PHP?
goto jumps to a labeled point in code, like goto my_label; my_label: echo “Here”;.
Rarely used in WordPress due to poor readability. Freelancers avoid goto, while enterprise architects prohibit it for maintainable, structured code.
What is a ternary operator in PHP?
A ternary operator is a shorthand if-else, like $result = ($x > 0) ? “Positive” : “Non-positive”;.
In WordPress, it’s used for concise conditions (e.g., inline styles). Freelancers use it for compact code, while enterprise architects ensure clarity over brevity.
What is the null coalescing operator in PHP?
The null coalescing operator (??) returns the first non-null value, like $value = $_GET[‘key’] ?? “default”;.
In WordPress, it handles missing data (e.g., post meta). Freelancers use it for safe defaults, while enterprise architects leverage it for robust input handling.
What is the spaceship operator in PHP?
The spaceship operator (<=>) compares values, returning -1, 0, or 1, like $x <=> $y.
Used in WordPress for sorting (e.g., custom post order). Freelancers apply it in plugins, while enterprise architects use it for efficient comparisons.
What is alternative syntax for control structures in PHP?
Alternative syntax uses : and end keywords, like if ($x > 0): echo “Positive”; endif;.
Common in WordPress templates for readability. Freelancers use it in themes, while enterprise architects ensure consistent syntax in collaborative projects.
What is the match expression in PHP?
A match expression is a concise switch, like $result = match($x) { 1 => “One”, default => “Other”; };.
Introduced in PHP 8, it’s cleaner than switch. Freelancers use it in modern WordPress plugins, while enterprise architects adopt it for readable, type-safe code.