# PHP Interview Question

## Write the code to calculate sum of each digit in PHP.

``````<?php
\$num = 12345;
\$sum=0; \$rem=0;
for (\$i =0; \$i<=strlen(\$num);\$i++)
{
\$rem=\$num%10;
\$sum = \$sum + \$rem;
\$num=\$num/10;
}
echo "Sum of digits 12345 is \$sum";
?>  ``````

Output:

``Sum of digits 12345 is 15``

## Write the Program to calculate Even Odd Program in PHP

``````<?php
\$number=28462;
if(\$number%2==0)
{
echo "\$number is Even Number";
}
else
{
echo "\$number is Odd Number";
}
?>  ``````

Output:

``28462 is Even Number``

## Write the Program in PHP to calculate the given number is Prime number or not ?

Here, We’ll print first 15 prime numbers.

``````<?php
\$count = 0;
\$num = 2;
while (\$count < 15 )
{
\$div_count=0;
for ( \$i=1; \$i<=\$num; \$i++)
{
if ((\$num%\$i)==0)
{
\$div_count++;
}
}
if (\$div_count<3)
{
echo \$num." , ";
\$count=\$count+1;
}
\$num=\$num+1;
}
?>  ``````

Output:

``2,3,5,7,11,13,17,19,23,29,31,37,41,43,47``

## Write the code to Print Table of a Number

Here , We’ll print the table of 3.

``````<?php
define('a', 3);
for(\$i=1; \$i<=10; \$i++)
{
echo \$i*a;
echo '<br>';
}
?>  ``````

Output:

``````3
6
9
12
15
18
21
24
27
30``````

## Write the code to Factorial in PHP

``````<?php
\$num = 4;
\$factorial = 1;
for (\$x=\$num; \$x>=1; \$x--)
{
\$factorial = \$factorial * \$x;
}
echo "Factorial of \$num is \$factorial";
?>  ``````

Output:

``Factorial of 4 is 24``

Subscribe
Notify of