PHP Logical & Programming Interview Questions & Answers (2025)

PHP logical and programming refers to the use of logical and programming concepts in the development of PHP-based applications. In PHP, logical concepts refer to the use of conditional statements, loops, and logical operators to control the flow of program execution. on the other hand, programming concepts refer to the use of programming constructs such as functions, arrays, and objects to organize code and data

11
Questions
6 min
Avg Read Time
95%
Success Rate
2023
Updated

PHP Logical & Programming Interview Questions Interview Preparation Guide

Practice PHP Logical & Programming Interview Questions Practice here the list of top 10 PHP Programs Asked to write you in your next PHP Interviews for 1 year Php Experience or PHP Fresher.

Interview Tip

In PHP Logical & Programming Interview Questions interviews, it's important to clearly explain key concepts and demonstrate your coding skills in real-time. Practice articulating your thought process while solving problems, as interviewers value both your technical ability and how you approach challenges.

Our team has carefully curated a comprehensive collection of the top PHP Logical & Programming Interview Questions to help you confidently prepare, impress your interviewers, and land your dream job.

PHP Logical & Programming Interview Questions for Freshers

1 Write a program in Php to check whether a number is prime or not?

The program in Php to check whether a number is prime or not is as follows :

<?php

function primeCheck($number){

    if ($number == 1)

    return 0;

    for ($i = 2; $i <= $number/2; $i++){

        if ($number % $i == 0)

            return 0;

    }

    return 1;

}

$number = 31;

$flag = primeCheck($number);

if ($flag == 1)

    echo "Prime";

else

    echo "Not Prime"

?>

 

2 Write a program to find no of days between two dates in Php?

The program to find no of days between two dates in PHP is as follows:

Let the dates are 25-09-2020 and 31-01-2021

<?php  

function dateDiffInDays($date1, $date2) 

{

    $diff = strtotime($date2) - strtotime($date1);

     return abs(round($diff / 86400));

}

$date1 = "25-09-2020";

$date2 = "31-01-2021";

$dateDiff = dateDiffInDays($date1, $date2);

printf("Difference between two dates: ". $dateDiff . " Days ");

?>

 

3 Write a program in PHP to find the occurrence of a word in a string?

The program in PHP to find the occurrence of a word in a string is as follows:

<?php

$s1 = "interviewmocks.com is a interview prep site";

$s2 = "interview";

$res = substr_count($s1, $s2);

echo($res);

?>

 

4 Write a program to upload a file in PHP?

The program to upload a file in PHP is as follows:

<?php

$target_dir = "uploads/";

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$uploadOk = 1;

$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

if(isset($_POST["submit"])) {

  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

  if($check !== false) {

    echo "File is an image - " . $check["mime"] . ".";

    $uploadOk = 1;

  } else {

    echo "File is not an image.";

    $uploadOk = 0;

  }

}

?>

 

5 Write a program to find a string is palindrome or not?

The program to find a string is palindrome or not is :

<?php

function Palindrome($number){ 

    $temp = $number; 

    $new = 0; 

    while (floor($temp)) { 

        $d = $temp % 10; 

        $new = $new * 10 + $d; 

        $temp = $temp/10; 

    } 

    if ($new == $number){ 

        return 1; 

    }

    else{

        return 0;

    }

$original = 257752;

if (Palindrome($original)){ 

    echo "Palindrome"; 

}

else { 

echo "Not a Palindrome"; 

}

?> 

 

6 Write a program to find the factorial of a number in PHP?

The program to find the factorial of a number in PHP is:

<?php

function Factorial($number){

    if($number <= 1){  

        return 1;  

    }  

    else{  

        return $number * Factorial($number - 1);  

    }  

}

$number = 20;

$fact = Factorial($number);

echo "Factorial = $fact";

?>

 

7 Php Code to find whether a number Armstrong or not?

The Php Code to find whether a number Armstrong or not is :

<?php

function armstrongCheck($number){

    $sum = 0; 

    $x = $number; 

    while($x != 0) 

    { 

        $rem = $x % 10; 

        $sum = $sum + $rem*$rem*$rem; 

        $x = $x / 10; 

    } 

    if ($number == $sum)

        return 1;

    return 0;   

}

$number = 153;

$flag = armstrongCheck($number);

if ($flag == 1)

    echo "Yes";

else

    echo "No"

?>

 

8 Php Program to generate Fibonacci series?

The Php Program to generate the Fibonacci series is :

<?php

function Fibonacci($n){

    $num1 = 0;

    $num2 = 1;

    $counter = 0;

    while ($counter < $n){

        echo ' '.$num1;

        $num3 = $num2 + $num1;

        $num1 = $num2;

        $num2 = $num3;

        $counter = $counter + 1;

    }

}

$n = 10;

Fibonacci($n);

?>

 

9 Write a program in PHP to print a table of a number?

The program in PHP to print a table of a number:

<?php    

define('a', 9);   

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

{   

  echo $i*a;   

  echo '<br>';     

}  

?>  

 

10 Write a program in PHP to reverse a number?

The program in PHP to reverse a number is :

<?php  

$num = 79658;  

$revnum = 0;  

while ($num > 1)  

{  

$rem = $num % 10;  

$revnum = ($revnum * 10) + $rem;  

$num = ($num / 10);   

}  

echo "Reverse number of 79658 is: $revnum";  

?>  

 

11  Write a PHP function to convert all null values to blank?

When we are outputting our results into JSON for purpose of web services then It is a good practice to convert all null value to an empty string, below is simple PHP function to convert all null values to blank.


function convertNullToBlank($array){

foreach ($array as $key => $value) {
if (is_null($value)) {
$array[$key] = "";
}
}
return $array;

}

Above function takes an array as an argument and converts it all null values to blank.

Ready to Master JavaScript Interviews?

Practice with our interactive coding challenges and MCQ tests to boost your confidence and land your dream JavaScript developer job.