Top 100 PHP Interview Questions and Answers (2021 Update)


Q1. Create a script that displays 1-2-3-4-5-6-7-8-9-10 on one line. There will be no

hyphen(-) at starting and ending position.


=>

<?php

for($i=1;$i<=10;$i++)

{

if($i<=9)

{

echo $i."-";

}

else

{

echo $i;

}

}

?>



Output:-




Q2. Create a script to make the sum of all the integers between 0 and 30 and display

the total.

=>


<?php

$total=0;

for($i=1;$i<=30;$i++)

{

$total=$total+$i;

//echo $total."<br>";

}

echo “sum of 0 to 30 is $total”;

?>

output:-

Q3. Create a script to construct the following pattern, using a nested for loop.


=>

<?php

for($i=0;$i<4;$i++)

{

for ($j=0; $j <=$i ; $j++) { 

echo "*";

}

echo"<br>";

}

?>


Output:--

Q4. Create a script to construct the following pattern, using a nested for loop.

=>


Output:-

<?php

for($i=0;$i<=5;$i++)

{

for($j=0;$j<$i;$j++)

{

echo"*";


}

echo"<br>";

}

for($i=4;$i>0;$i--)

{

for($j=$i;$j>0;$j--)

{

echo"*";

}

echo"<br>";

}

?>


Q5. Write a PHP script that creates the following table using for loops. Add

cellpadding=”3px” and cellspacing=”0px” to the table tag.

<?php

echo"<table border=1 cellpadding=3px cellspacing=0>";

for ($i=1; $i<=6; $i++) { 

echo"<tr>";

 for($j=1;$j<=5;$j++)

 {


echo"<td> $i * $j= ".$i*$j." </td>";

}

echo"</tr>";

}

echo"</table>";

?>

Output:-

Q6. Write a PHP script using nested for loop that creates a chess board as shown

below.

Use table width=&quot;270px&quot; and take 30px as cell height and width.

=>

<?php

echo"<table width='270px' border=1>";

for($i=1;$i<9;$i++)

{

echo"<tr >";

for($j=$i;$j<9+$i;$j++)

{

if($j%2==0)

{

echo "<td bgcolor='white'text-color='white''height='30'width='30'></td>";

}

else{

echo "<td bgcolor='black' height='30'width='30'></td>";


}

echo"</tr>";

}

echo"</table>";

?>

Output:-


Q7. Write a PHP script that creates the following table (use for loops).

<?php

echo"<table border=1>";

for($i=1;$i<=10;$i++)

{

echo"<tr>";

for($j=1;$j<=10;$j++)

{

echo"<td> ".$i*$j." </td>";

}

echo"</tr>";

}

echo"</table>";

?>

Output:-


Q8. Write a program to find the factorial of a number (non-negative integer).


=>

<?php

echo factorial(5);

function factorial($f)

{

$total=1;


for($i=1;$i<=$f;$i++)

{


$total=$total*$i;



}

return "factorial of $f is $total";

}

?>

Output:-


Q9. Write a PHP program to generate and display the first n lines of a Floyd triangle.

(use n=5 and n=11 rows).

=>


<?php

$php=1;

$n=5;

echo "<br><b>n=5 rows</b><br><br>";

for($i=1;$i<=$n;$i++)

{


for($j=0;$j<$i;$j++)

echo $php."  ";

$php++;



}


echo"<br>";

}


echo "<br><br><b>n=11 rows</b><br><br>";

$php=1;

$n=11;

for($i=1;$i<=$n;$i++)

{


for($j=0;$j<$i;$j++)

echo $php."  ";

$php++;



}


echo"<br>";

}

?>

Output :-


Q10. 

=>

<?php

for($i=1;$i<=50;$i++)

{   

 

  if( $i%15 == 0)

{

echo"$i=>fizzbuzz<br>";

}


else if($i%3==0)

{

     echo" $i=>fizz<br>";


}

else if($i%5==0)

{

     echo"$i=>buzz<br>";

}

}

?>

Output:-


Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post