PHP Logical & Programming Interview Questions

PHP Logical & Programming Interview Questions

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.

Download PHP Logical & Programming Interview Questions PDF

Below are the list of Best PHP Logical & Programming Interview Questions and Answers

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"

?>

 

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 ");

?>

 

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);

?>

 

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;

  }

}

?>

 

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"; 

}

?> 

 

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";

?>

 

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"

?>

 

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);

?>

 

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>';     

}  

?>  

 

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";  

?>  

 

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.