PHP Programming interview questions for freshers
Today we discuss the PHP Programming interview questions for fresher’s previous we discuss the Top 60 asp.net core interview questions and Answers
List of questions –
- Write a program in Php to check whether a number is prime or not?
- Write a program to find no of days between two dates in Php?
- Write a program in PHP to find the occurrence of a word in a string?
- Write a program to upload a file in PHP?
- Write a program to find a string is its palindrome or not?
- Write a program to find the factorial of a number in PHP?
- Php Code to find whether a number Armstrong or not?
- Php program to generate Fibonacci series?
- Write a program in PHP to print a table of a number?
- Write a program in PHP to reverse a number?
- Write a PHP function to convert all null values to blank?
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?
//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?
<?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?
<?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 its palindrome or not?
<?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?
<?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?
<?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?
<?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?
<?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?
<?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?
function convertNullToBlank($array){
foreach ($array as $key => $value) {
if (is_null($value)) {
$array[$key] = "";
}
}
return $array;
}