Table Of Contents
- 1 PHP Programming code and output often asked in Interviews & Exams 2022
- 1.1 PHP Solution For Finding The Sum of all Digits of any Number
- 1.2 PHP Program to Print Table of a Number
- 1.3 PHP solution to determine if a number is Even or Odd
- 1.4 PHP Solution for finding Prime Numbers
- 1.5 PHP Solution to check if a number is a prime number
- 1.6 PHP Solution for reversing words in a string
- 1.7 PHP Solution to check if a string is a palindrome
Interviews are stressful at the best of times, and if you have to solve PHP interview questions or take a PHP exam and answer other questions your brain can refuse to play nice. The best way to be prepared is to practice answering questions, brush up on your skills and a good candidate will prepare by reviewing common PHP interview questions 2022.
So if you are preparing for PHP Interview Questions in 2022 don’t miss this list of PHP program quizzes asked in interviews and exams. We have also added the output at the end of each program so you are thoroughly prepared for your upcoming interviews.
PHP Programming code and output often asked in Interviews & Exams 2022
The first thing to brush up on, is the history of PHP which stands for Hypertext Preprocessor. This language was created in 1994 by Rasmus Lerdorf, a Danish-Canadian programmer. Most frequently It is used for web development.
Generally embedded in HTML, PHP is a server-side scripted language which runs on the back-end of web pages. It is a simple and easy to learn language that provides powerful interactions with the server, databases and other functions.
PHP Solution For Finding The Sum of all Digits of any 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 25341, then we need to add each of those digits up individually.
Sum of 25341 = 2 + 5 + 3 + 4 + 1 = 15
Therefore, 15 is the sum of 25341. To create a PHP program, you need to consider every combination, so solve the equation by using string length, the power of a loop and mod to resolve remainder.
PHP Solution
<?php
$numb = 25341;
// number = 25341
$s=0;
// Initialize Sum = 0
$r=0;
// Initialize remainder = 0
for ($i=0; $i<=strlen($numb); $i++)
{ $r=$numb % 10;
//remainder = number mod 10
$s = $s + $r;
// Sum = Sum + Remainder
$numb=$numb / 10;
// number = number/10
}
echo "The sum of digits 25341 is $s";
?>
Output
The sum of digits 25341 is 15
PHP Program to Print Table of a Number
If you are asked to print the table of number, for example ‘9’, use for loop. Step 1 define the number. Step 2 run the ‘for loop’ from 1 to 10. And step 3 multiply the number with the loop and print for your output.
Solution
<?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 solution to determine if a number is Even or Odd
Even numbers are those which are divisible by 2 without remainder. Odd numbers are the others, which are not divisible by 2. Therefore the program must check if the number is even by dividing it by 2. If the remainder comes out to be 0, print “Even” otherwise print the number is “ODD”.
Solution
<?php
$numb=42;
// Check if 42 is even or odd
if($numb%2==0)
// if remainder from the number is zero, then number is even else odd
{
echo "$numb is EVEN";
}
else
{
echo "$numb is ODD";
}
?>
Output
42 is EVEN
PHP Solution for finding Prime Numbers
Sometimes they ask you to find the first 10 non prime numbers, in which case this example plus the following one should prepare you anyway.
A prime number is a natural number which is only divisible by itself and the number 1. Note, 0 and 1 are not prime numbers because 0 is not a natural number and 1 is not a composite number. Here are the first 10 we are looking for: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29.
Solution
<?php
$count = 0;
$numb = 2;
// Start at the first prime number
while ($count < 10)
// Print the first 10 prime numbers
{
$div_count=0;
// Zero the counter
for ( $i=1; $i<=$numb; $i++)
{
if (($numb%$i)==0)
{
$div_count++;
}
}
if ($div_count<3)
{
echo $numb " , ";
$count=$count+1;
}
$n=$n+1;
}
?>
Output
2,3,5,7,11,13,17,19,23,29
PHP Solution to check if a number is a prime number
In this example let’s validate a field from a form, to check if user entry is a prime number or not.
Solution
// First the form itself in HTML
<form method="post">
Please enter a number to confirm if it is a prime number: <input type="text" name="input"></br></br>
<input type="submit" name="submit" value="Submit">
</form>
// Now the PHP processing of the user entry
<?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 a prime number.";
}
else
{
echo "The number ". $input . " is a prime number.";
}
}
?>
Output
The number 95 is not a prime number. The number 19 is a prime number.
PHP Solution for reversing words in a string
There are many useful and powerful inbuilt functions in PHP, however sometimes you will be asked to recreate these functions by finding your own solution. Here’s a PHP Solution to reverse the order of words in a string.
Program
<?php
$string="Hello World";
$reverse="";
// Here is a step some people forget. Emptying the string first.
$i=0;
while(!empty($string[$i]))
{
$reverse=$string[$i].$reverse;
$i++;
}
echo $reverse;
?>
World Hello
PHP Solution to check if a string is a palindrome
Palindrome means a word which is spelt the same from either end, it can be reversed and will read as the same. Some simple examples are “dad”, “mum” and “pip”. A simple solution is to check if the string is equal to its reversed string. This time we will use an inbuilt function strrev().
Program
<?php
$string="PHP";
if ($string == strrev($string))
{
echo $string." is a palindrome.";
}
else
{
echo $string." is not a palindrome.";
}
?>
PHP is a palindrome. C++ is not a palindrome.
Hopefully you get asked one of these questions or you can use some of the above PHP code to solve whatever PHP Interview questions 2022 come up in your interview or exam. Thanks for taking the time to read our article and good luck with your PHP Interview Questions & Exams 2022 and we really hope you get the job!