# PHP Program Asked in Interviews & Exams 2018 (With Output)

Preparing for Interviews 2019? Don’t miss this list of PHP program asked in Interviews and Exams 2018. We have also added output side by side with each program to help you prepare thoroughly for your interviews. Check out our list of programs which are asked frequently in 2018 interviews and exams.

## PHP Program Asked in Interviews & Exams 2018 (With Output)

PHP ( Hypertext Preprocessor ) was created by Rasmus Lerdorf, a Danish-Canadian programmer in 1994. It is used in various web development applications and has a great demand nowadays. PHP is a server-side scripted language which can be embedded into HTML.  Nowadays, PHP is widely used and PHP program is asked frequently in various interviews and exams.

It is very important to know what was asked in interviews 2018 to get prepared for the upcoming interviews 2019. To help you out, we have curated a list of PHP program frequently asked in Interviews and exams 2018. Prepare thoroughly for the interviews and all the best! Also, read java program, python and C programs asked in interviews 2018. Don’t miss out our post on Interview Preparation 2018 (Frequently Asked Questions to Programmer). ### PHP program to find Sum of all Digits of a Number

Here, you need to find the sum of all the digits of the given number using the PHP program. For example, let’s suppose the given number is 15342. The sum of all digits of 15342 is mathematically calculated by adding all the digits of 15342.

Sum of 15342 = 1 + 5 + 3 + 4 + 2 = 15

Thus, the sum of 15342 is 15. In the PHP program, we will find out the sum of the given number by using for loop. We will first find the remainder by taking mod of the number by 10. Then, add the remainder to the sum variable and divide the number by 10. Now, repeat the process until the remainder is 0.

Program

```<?php

\$n = 15342;

// number = 15342

\$s=0;

// Initialize Sum = 0

\$r=0;

// Initialize remainder = 0

for (\$i=0; \$i<=strlen(\$n); \$i++)

{     \$r=\$n % 10;

//remainder = number mod 10

\$s = \$s + \$r;

// Sum = Sum + Remainder

\$n=\$n / 10;

// number = number/10

}

echo "Sum of digits 15342 is \$s";

?>```

Output

`Sum of digits 15342 is 15`

### PHP Program to Print Table of a Number

Let’s suppose you need to print the table of number ‘9’. Using for loop in the program, you can print the table of any number. First, you need to define the number. Then, you need to run ‘for loop’ from 1 to 10. Now, at the last multiply the number with the loop and print to get the required output.

Program

```<?php

define("j", 9);

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

{

echo \$i*j;

echo "<br>";

}

?>

```

Output

`9,18,27,36,45,54,63,72,81,90`

### PHP program to check for Even or Odd number

Even numbers are those which are divisible by 2, for example – 2,4,6,8,10,12 etc. are even numbers. Whereas Odd numbers are those which are not divisible by 2, for example – 1, 3, 5, 7, 9, 11,13 etc. are odd numbers. To check whether the number is odd or even, divide the number by 2. If the remainder comes out to be 0, print number is even else print the number is odd.

Program

```<?php

\$n=62;

// Check 62 is odd or even number

if(\$n%2==0)

// if remainder is zero, number is even else odd

{

echo "\$n is Even Number";

}

else

{

echo "\$n is Odd Number";

}

?>```

Output

`62 is Even Number`

### PHP Program to Print Prime Numbers

A prime number is a natural number which is only divisible by 1 and itself, for example, 2, 3, 5, 7, 11, 13, 17, 19 etc. are prime numbers. 0 and 1 are not prime numbers because 0 is not a natural number and 1 is neither prime nor composite number.

Program

```<?php

\$count = 0;

\$n = 2;

while (\$count < 10)

// Print first 10 prime numbers

{

\$div_count=0;

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

{

if ((\$n%\$i)==0)

{

\$div_count++;

}

}

if (\$div_count<3)

{

echo \$n " , ";

\$count=\$count+1;

}

\$n=\$n+1;

}

?>```

Output

`2,3,5,7,11,13,17,19,23,29`

### Check whether a number is Prime or not (Form method).

By using the form method, we will check whether the number filled in the form is prime or not.

Program

```<form method="post">

Enter a Number to check for prime: <input type="text" name="input"><br><br>

<input type="submit" name="submit" value="Submit">

</form>

<?php

if(\$_POST)

{

\$input=\$_POST["input"];

for (\$i = 2; \$i <= \$input-1; \$i++)

{

if (\$input % \$i == 0)

{

\$value= True;

}

}

if (isset(\$value) && \$value)

{

echo "The Number ". \$input . " is not prime";

}

else

{

echo "The Number ". \$input . " is prime";

}

}

?>```

Output

```The Number 95 is not prime

The Number 19 is prime```

### PHP Program for reverse a string

You have to reverse a string without using the inbuilt function of PHP.

Program

```<?php

\$string="Hello World";

\$reverse="";

\$i=0;

while(!empty(\$string[\$i]))

{
\$reverse=\$string[\$i].\$reverse;

\$i++;

}

echo \$reverse;

?>```
Output
`World Hello`

### Program to find out the string is palindrome or not.

The palindrome means the word whose reverse is exactly the same as unreversed. So, we have to check the string is palindrome or not. If the string is equal to its reversed string, then it is palindrome otherwise it is not. In this program, only if-else loop is required.

Program

```<?php

\$string="PHP";

if (\$string == strrev(\$string))

{

echo \$string." is palindrome";

}

else

{

echo \$string." is not palindrome";

}

?>```
Output
`PHP is palindrome`

We hope that you liked the post on PHP Program Asked in Interviews & Exams 2018. Spend a minute to subscribe to the blog and receive posts like this directly to your email — Subscription links are provided at the end of every page/post. Also, don’t forget to share your views with us — Comment section provided at the end of this post. Further, share this post on social media and also connect with us via Facebook, Twitter, Instagram, Pinterest, LinkedIn, etc.

You may also like>