Press "Enter" to skip to content

Top 50 PHP Interview Questions and Answers Could Help You

Here are the top 50 PHP Interview questions and answers could help you in the interview. All questions are program based, and you need to find out the output.

Find The Output?

1.

<html>
    <body>
        <?php
$a=10;
$b=++$a;
echo $b;
        ?>
    </body>
</html>
ans:11

2.

<html>
    <body>
        <?php
$a=10;
$b=$a++;
echo $b;
        ?>
    </body>
</html>
ans:10

3.

<html>
    <body>
        <?php
$a=10;
$b=++$a + ++$a;
echo $b."<br>";
echo $a."<br>";
        ?>
    </body>
</html>
ans:

23
12

4.

<html>
    <body>
        <?php
$a=10;
$b=++$a + $a++;
echo $b."<br>";
echo $a."<br>";
        ?>
    </body>
</html>
ans:
22
12

5.

<html>
    <body>
        <?php
$a=10;
$b=--$a + $a--;
echo $b."<br>";
echo $a."<br>";
        ?>
    </body>
</html>
ans:
18
8

6.

<html>
    <body>
        <?php
$a=10;
$c=10;
$d=$a*$c+$c++;
echo $c.$d;
        ?>
    </body>
</html>
ans:11110

7.

<html>
    <body>
        <?php
$a=10;
$c=10;
$d=100;
$d1=$a+$c*$c/$d;
echo $d1;
        ?>
    </body>
</html>
ans:
11

8.

<html>
    <body>
        <?php
$a="shibu";
$b==90;
$c=$a+$b;
echo c;
        ?>
    </body>
</html>
ans: error

9.

<html>
    <body>
        <?php
$a="shibu";
$b=30;
$c=$a+$b;
echo $c;
        ?>
    </body>
</html>
ans:30

10.

<html>
    <body>
        <?php
$a=10;
$b=30;
$c=$a=$b;
echo $c;
        ?>
    </body>
</html>
ans:
30

10.

<html>
    <body>
        <?php
$a="shibu";
$b="tarak";
$c=$a+$b;
echo $c;
        ?>
    </body>
</html>
ans:0

11.

<html>
    <body>
        <?php
$a="shibu";
$b="tarak";
$c=$a*$b;
echo $c;
        ?>
    </body>
</html>
ans:0

12.

<html>
    <body>
        <?php
$a=True;
echo $a;
        ?>
    </body>
</html>
ans:1

13.

<html>
    <body>
        <?php
$a='well come';
$b=$a{1};
echo $b;
        ?>
    </body>
</html>
ans:e

14.

<html>
    <body>
        <?php
$a='red is a color';
$b=$a{strlen($a)-1};
echo $b;
        ?>
    </body>
</html>
ans:r

15.

<html>
    <body>
        <?php
$a='amit is in kolkata';
$b=strtoupper(strlen($a)).'well';
echo $b;
        ?>
    </body>
</html>
ans:18well

16.

<html>
    <body>
        <?php
$a="amit";
$b="AMIT";
$c=strcmp($a,$b);
echo $c;
        ?>
    </body>
</html>
ans:1

17.

<html>
    <body>
        <?php
$a="amit";
$b="AMIT";
$c=strcmp($a,$b)+strcasecmp($a,$b);
echo $c;
        ?>
    </body>
</html>
ans:1

18.

<html>
    <body>
        <?php
$a="This is sunday";
$b="We know it";
$c=$a{1}+$b;
echo $c;
        ?>
    </body>
</html>
ans:0

19.

<html>
    <body>
        <?php
$a="This is sunday";
$b=strpos($a,"sunday");
echo $b;
        ?>
    </body>
</html>
ans:8

20.

<html>
    <body>
        <?php
$a="This is sunday";
$b=substr($a,strlen($a)-1,1);
echo $b;
        ?>
    </body>
</html>
ans:y

21.

<html>
    <body>
        <?php
$a=90,80,60;
echo $a;
        ?>
    </body>
</html>
ans:error

22.

<html>
    <body>
        <?php
$a=90;
if($a>10);
echo "ok I am in India";
        ?>
    </body>
</html>
ans:ok I am in India

23.

<<html>
    <body>
        <?php
$a=90;
if($a>10);
echo "ok I am in India";
else
    echo "Hello";
        ?>
    </body>
</html>
ans:error 
Parse error: syntax error, unexpected 'else'

24.

<html>
    <body>
        <?php
$a=90;
if($a<10)
    echo "ok I am in India";
else;
echo "Hello";
        ?>
    </body>
</html>
ans:Hello

25.

<html>
    <body>
        <?php
$a=90;
$b=80;
$c=40;
if($a>$b>$c)
    echo "well come";
        ?>
    </body>
</html>
ans:error
Parse error: syntax error, unexpected '>'

26.

<html>
    <body>
        <?php
$a=90;
if($a=80)
    echo "well come";
else
    echo "India";
        ?>
    </body>
</html>
ans:well come

27.

<html>
    <body>
        <?php
$a=90;
if(1)
    echo "well come";
else
    echo "India";
        ?>
    </body>
</html>
ans:well come

28.

<html>
    <body>
        <?php
$a=90;
if(0)
    echo "well come";
else
    echo "India";
        ?>
    </body>
</html>
ans:India

29.

<html>
    <body>
        <?php
$a=90.0/90;
if(0)
    echo "well come";
else
    echo "India";
        ?>
    </body>
</html>
ans:India

30.

<html>
    <body>
        <?php
$a=-90.0%90;
if($a>-0)
    echo "well come";
else
    echo "India";
        ?>
    </body>
</html>
ans:India

31.

<html>
    <body>
        <?php
$a=9;
if($a>1)
    echo "well";
elseif($a>1)
    echo "come";
        ?>
    </body>
</html>
ans:well

32.

<html>
    <body>
        <?php
$a=9;
if($a>1)
    echo "well";
elseif($a>8)
    echo "come";
else($a>20)
    echo "India";
        ?>
    </body>
</html>
ans:error

33.

<html>
    <body>
        <?php
$a=3;
switch($a)
{
    case 1:
    echo "well";
    break;
    case 2:
    echo "come";
    break;
    case 3:
    echo "Delhi";
    break;
    case 1+2:
    echo "Kolkata";
    break;
    default:
    echo "Hello kolkata";
}
        ?>
    </body>
</html>
ans:Delhi

34.

<html>
    <body>
        <?php
$a=3;
switch($a)
{
    case 1:
    echo "well";
    break;
    case 2:
    echo "come";
    break;
    case 3:
    echo "Delhi";
    case 1+2:
    echo "Kolkata";
    default:
    echo "Hello kolkata";
}
        ?>
    </body>
</html>
ans:DelhiKolkataHello kolkata

35.

<html>
    <body>
        <?php
$a=30;
switch($a)
{
    default:
    echo "Red";
    case 1:
    echo "Green";
    break;
    case 2:
    echo "blue";
    break;
    case 3:
    echo "Yellow";
    break;
    case 4:
    echo "Magenta";
    break;
}
        ?>
    </body>
</html>
ans:RedGreen

36.

<html>
    <body>
        <?php
$a=30;
switch(1)
{
    default:
    echo "Red";
    case 1:
    echo "Green";
    break;
    case 2:
    echo "blue";
    break;
    case 3:
    echo "Yellow";
    break;
    case 4:
    echo "Magenta";
    break;
}
        ?>
    </body>
</html>
ans:Green

37.

<html>
    <body>
        <?php
$a=1;
switch($a)
{
    case "1":
    echo "Gita";
    break;
    case "2":
    echo "Mita";
    break;
    case "3":
    echo "Tita";
    break;
    case "4":
    echo "Mona";
    break;
}
        ?>
    </body>
</html>
ans:Gita

38.

<html>
    <body>
        <?php
$a=2;
switch($a)
{
    case "1":
    echo "Goutam";
    case "2":
    echo "Mou";
    case "3":
    echo "Tata";
    case "4":
    echo "Nilu";
}
        ?>
    </body>
</html>
ans:MouTataNilu

39.

<html>
    <body>
        <?php
$a=1.1;
switch($a)
{
    case 1.1:
    echo "one";
    break;
    case 2.1:
    echo "two";
    break;
    default:
    echo "Same number";
}
        ?>
    </body>
</html>
ans:one

40.

<html>
    <body>
        <?php
for($i=1;$i<=10;$i=$i+1);
echo $i;
        ?>
    </body>
</html>

41.

<html>
    <body>
        <?php
for($i=1;$i=10;$i=$i+1)
    echo "well come";
        ?>
    </body>
</html>
ans:well come prints infinitely

42.

<html>
    <body>
        <?php
for($i=1,$i=10,$i=$i+1)
    echo "India";
        ?>
    </body>
</html>
ans:Syntax error

43.

<html>
    <body>
        <?php
for($i=1;$i%2==0;$i=$i+1)
    echo "India is a country";
        ?>
    </body>
</html>
ans:no output

44.

<html>
    <body>
        <?php
for(;$i;$i=$i+1)
    echo "well come";
        ?>
    </body>
</html>
ans:error,undefined $i

45.

<html>
    <body>
        <?php
for($i=1;$i<=10;$i=$i+1)
{
    if($i)
    {
        echo "Happy New year";
        break;
    }
}
        ?>
    </body>
</html>
ans:Happy New year

46.

<html>
    <body>
        <?php
for($i=1;$i<=10;$i=$i+1)
{
    if($i)
    {
        continue;
        echo "Happy New year 2019";
    }
}
        ?>
    </body>
</html>
ans:no output

48.

<html>
    <body>
        <?php
while(True)
{
    echo "Sunday";
}
        ?>
    </body>
</html>
ans:sunday is printing infinitely

49.

<html>
    <body>
        <?php
$a=True;
do
{
    echo "This is Monday";
}
while($a!=False);
        ?>
    </body>
</html>
ans:This is Monday is printing infinitely

50.

<html>
    <body>
        <?php
for($i=1;$i<=3;$i=$i+1)
{
    for($j=1;$j<=3;$j=$j+1)
    {
        if($i+$j!=2)
            echo "Apple";
        else
            echo "Orange";
    }
}
        ?>
    </body>
</html>
ans:OrangeAppleAppleAppleAppleAppleAppleAppleApple