1. What keyword is used to exit a switch statement in PHP?
The break statement stops execution of the current case in a switch block. Without it, PHP continues to the next case.
switch($day){ case 'Mon': echo 'Work'; break; }Get the Preplance app for a seamless learning experience. Practice offline, get daily streaks, and stay ahead with real-time interview updates.
Get it on
Google Play
4.9/5 Rating on Store
PHP · Question Set
Control Structures & Functions interview questions for placements and exams.
Questions
14
Included in this set
Subject
PHP
Explore more sets
Difficulty
Mixed
Level of this set
Go through each question and its explanation. Use this set as a focused practice pack for PHP.
The break statement stops execution of the current case in a switch block. Without it, PHP continues to the next case.
switch($day){ case 'Mon': echo 'Work'; break; }For complete preparation, combine this set with full subject-wise practice for PHP. You can also explore other subjects and sets from the links below.
Variables inside a function are local by default. To access global variables inside a function, you use the global keyword or the $GLOBALS array.
function show(){ global $x; echo $x; }PHP supports two syntaxes for if statements. The standard uses braces, and the alternate syntax uses a colon with endif for template files.
if ($x > 10) { echo 'Big'; }
// or
if ($x > 10): echo 'Big'; endif;A for loop includes three expressions: initialization, condition, and increment or decrement.
for($i=0; $i<5; $i++){ echo $i; }A while loop checks the condition first, then executes the body. A do-while loop executes once before checking.
while($x < 5){ echo $x; $x++; }In do-while, the body runs once before checking the condition. In while, the condition is checked first.
do { echo $x; $x++; } while ($x < 5);The foreach loop is made for iterating arrays. It can loop through both keys and values easily.
foreach($arr as $val){ echo $val; }Functions are declared using the keyword function, followed by a name and parentheses.
function greet(){ echo 'Hello'; }If a function has no return statement, PHP automatically returns null. This is common for functions that only print or modify data.
function test(){} $x = test(); // nullYou can assign a default value using an equal sign in the parameter list. Example: function add($a=10).
function greet($name='User'){ echo $name; }Nested loops are loops inside another loop. The inner loop runs completely for each iteration of the outer loop. They are often used for multidimensional arrays or patterns.
for($i=0;$i<3;$i++){ for($j=0;$j<3;$j++){ echo $i.$j; } }Recursion means a function calls itself until a condition is met. It is used in problems like factorial or tree traversal. Care must be taken to avoid infinite loops.
function fact($n){ if($n==1) return 1; return $n*fact($n-1); }Anonymous functions are functions without a name. They are used as arguments to other functions or stored in variables. Closures can access variables from their parent scope using the use keyword.
$add = function($a,$b){ return $a+$b; }; echo $add(2,3);When a variable is passed by reference, the function gets the original variable, not a copy. Changes inside the function affect the original. Use an ampersand before the parameter name.
function addOne(&$num){ $num++; } $x=5; addOne($x);