DEV Community

Cover image for Best PHP Programs with Output – Practice for Interviews
suraj kumar
suraj kumar

Posted on

Best PHP Programs with Output – Practice for Interviews

PHP (Hypertext Preprocessor) is one of the most popular server-side scripting languages used to build dynamic and interactive web applications. Whether you are preparing for a coding interview or brushing up on your web development skills, practicing PHP programs with real examples can boost your confidence and understanding. In this blog, we’ll explore some of the best PHP programs with outputs to help you get ready for interviews and practical coding tests.

*1. Basic PHP Program: Print “Hello, World!”
*

This is the first program every beginner learns. It helps you understand PHP syntax and structure.

Code:

<?php
echo "Hello, World!";
?>
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, World!
Enter fullscreen mode Exit fullscreen mode

Explanation:
The echo statement is used to print text or variables to the browser. Every PHP script starts with <?php and ends with ?>.

** 2. PHP Program to Add Two Numbers
**
This simple arithmetic operation is a common interview question.

Code:

<?php
$num1 = 10;
$num2 = 20;
$sum = $num1 + $num2;
echo "The sum is: $sum";
?>
Enter fullscreen mode Exit fullscreen mode

Output:

The sum is: 30
Enter fullscreen mode Exit fullscreen mode

Explanation:
Variables in PHP start with a $ symbol. You can perform arithmetic operations easily and display results using echo.

** 3. PHP Program to Check Even or Odd Number
**
Conditional statements are essential for logic building.

Code:

<?php
$number = 7;
if($number % 2 == 0) {
    echo "$number is Even";
} else {
    echo "$number is Odd";
}
?>
Enter fullscreen mode Exit fullscreen mode

Output:

7 is Odd
Enter fullscreen mode Exit fullscreen mode

Explanation:
The modulus operator % checks the remainder when dividing by 2. If it’s 0, the number is even; otherwise, it’s odd.

** 4. PHP Program to Find Factorial of a Number
**
This program tests your knowledge of loops.

Code:

<?php
$num = 5;
$fact = 1;
for($i = 1; $i <= $num; $i++) {
    $fact *= $i;
}
echo "Factorial of $num is $fact";
?>
Enter fullscreen mode Exit fullscreen mode

Output:

Factorial of 5 is 120
Enter fullscreen mode Exit fullscreen mode

Explanation:
The for loop multiplies all integers from 1 to $num, giving the factorial value.

** 5. PHP Program to Reverse a String
**
String manipulation is a frequent interview topic.

Code:

<?php
$str = "Thesoka";
$rev = strrev($str);
echo "Reversed string: $rev";
?>
Enter fullscreen mode Exit fullscreen mode

Output:

Reversed string: akosehT
Enter fullscreen mode Exit fullscreen mode

Explanation:
The built-in PHP function strrev() reverses a string. It's useful in programs related to palindrome checks and text manipulation.

** 6. PHP Program to Check Palindrome String
**
A palindrome reads the same forward and backward.

Code:

<?php
$string = "level";
if($string == strrev($string)) {
    echo "$string is a Palindrome";
} else {
    echo "$string is not a Palindrome";
}
?>
Enter fullscreen mode Exit fullscreen mode

Output:

The level is a Palindrome
Enter fullscreen mode Exit fullscreen mode

Explanation:
The program compares the original string with its reversed version using the equality operator ==.

** 7. PHP Program to Find the Largest Number Among Three
**
Common logic-based interview question.

Code:

<?php
$a = 12;
$b = 25;
$c = 9;
if($a > $b && $a > $c)
    echo "$a is the largest number";
elseif($b > $a && $b > $c)
    echo "$b is the largest number";
else
    echo "$c is the largest number";
?>
Enter fullscreen mode Exit fullscreen mode

Output:

25 is the largest number
Enter fullscreen mode Exit fullscreen mode

Explanation:
The if-elseif-else structure helps compare multiple conditions to find the maximum number.

*8. PHP Program to Swap Two Numbers Without Using Third Variable
*

A logical test of understanding operators.

Code:

<?php
$x = 5;
$y = 10;
$x = $x + $y;
$y = $x - $y;
$x = $x - $y;
echo "After swapping: x = $x, y = $y";
?>
Enter fullscreen mode Exit fullscreen mode

Output:

After swapping: x = 10, y = 5
Enter fullscreen mode Exit fullscreen mode

Explanation:
This program swaps values using arithmetic operations instead of a temporary variable.

** 9. PHP Program to Check Prime Number
**
Important for loops and conditional logic practice.

Code:

<?php
$num = 13;
$flag = 0;
for($i = 2; $i <= $num/2; $i++) {
    if($num % $i == 0) {
        $flag = 1;
        break;
    }
}
if($flag == 0)
    echo "$num is a Prime number";
else
    echo "$num is not a Prime number";
?>
Enter fullscreen mode Exit fullscreen mode

Output:

13 is a Prime number
Enter fullscreen mode Exit fullscreen mode

Explanation:
A number is prime if it has no divisors other than 1 and itself.

** Final Thoughts**

Practicing PHP programs with real examples is one of the best ways to prepare for PHP coding interviews. Focus on understanding loops, conditionals, arrays, strings, and functions, as these are the most commonly asked topics. The examples above cover both beginner and intermediate concepts that will strengthen your foundation in PHP.

Whether you’re building a career in web development or appearing for technical interviews, consistent coding practice will make you confident and ready to solve any challenge that comes your way.

Top comments (0)