Linear search algorithm in PHP
Notes:
Linear search with & without break statement in PHP
When to use break statement?
- We can use break statement for writing efficient codes.
Linear search without break statement:
Ex:
$numbers = array(10,20,30,40,50);
$item= 20;
$found = false;
for($i=0; $i<count($numbers); $i++)
{
if($numbers[$i]==$item)
{
$found= true;
}
}
echo ($found==true) ? "item found" : "item not found";
Linear search with break statement:
Ex:
$numbers = array(10,20,30,40,50);
$item= 20;
$found = false;
for($i=0; $i<count($numbers); $i++)
{
if($numbers[$i]==$item)
{
$found= true;
break;
}
}
echo ($found==true) ? "item found" : "item not found";
Interview Questions:
1. PHP stands for ______________
a. Hypertext Preprocessor
b. Preprocessor Hypertext
c. Personal Home Processor
d. Personal Hypertext processor
Ans: a