**PHP Fresher Technical Interview Questions**

**Question : How to find out leap year?**

**Answer:**A

**leap year**is a calendar year containing one additional day. It has 366 days instead of the usual 365 days.

**function isLeapYear($year){**

**return ((0 == ($year % 4)) && (0 != ($year % 100)) || (0 == ($year % 400)));**

**}**

**var_dump(isLeapYear(2020));**

**Code Explanation:**Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400.

**Question : How to find out Prime number?**

**Answer:**

**Prime number**is a natural number greater than 1 that has no positive divisors other than 1 and itself.

**Function to check whether prime number or not**

**function isPrimeNumber($num){**

**$flag = 0;**

**for($i=2; $i<=$num/2; ++$i)**

**{**

**// condition for nonprime number**

**if($num%$i==0)**

**{**

**$flag=1;**

**break;**

**}**

**}**

**if ($flag==0)**

**echo "$num is prime number.";**

**else**

**echo "$num is not prime number.";**

**}**

**isPrimeNumber(19);**

**Find prime number under any number**

**function listPrimeNumber($num){**

**for( $j = 2; $j <= $num; $j++ )**

**{**

**for( $k = 2; $k < $j; $k++ )**

**{**

**if( $j % $k == 0 )**

**{**

**break;**

**}**

**}**

**if( $k == $j )**

**echo "Prime Number : ". $j. "<br>";**

**}**

**}**

**listPrimeNumber(20);**

**Question : How to find out Armstrong number?**

**Answer:**An

**Armstrong number**of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.

A positive integer is called an Armstrong number of order n if

**abcd... = a**n + b**n + c**n + d**n + ...**

In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example:

**153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.**

**function to check wheather number is armstrong or not**

**function isArmstrong($num){**

**$sum=0;**

**$temp=$num;**

**while($temp!=0)**

**{**

**$rem=$temp%10;**

**$sum=$sum+($rem*$rem*$rem);**

**$temp=$temp/10;**

**}**

**if($num==$sum)**

**{**

**echo "Armstrong number";**

**}**

**else**

**{**

**echo "Not an armstrong number";**

**}**

**}**

**isArmstrong(155);**

**Question : How to find out Palindrome number?**

**Answer:**A

**palindromic**number is a number that remains the same when its digits are reversed.

**function isPalindrome($number){**

**$reverse = strrev($number);**

**//check if the number and reverse is equal**

**if($number == $reverse){**

**echo " number is Palindrome!!";**

**}**

**}**

**isPalindrome(16461);**

**Or you can do without inbuild function**

**function isPalindrome($num){**

**$sum = 0;**

**$temp = $num;**

**while((int)$num!=0)**

**{**

**$rem=$num%10;**

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

**$num=$num/10;**

**}**

**if ($sum == $temp)**

**echo "$temp is a palindrome number.\n";**

**else**

**echo "$temp is not a palindrome number.\n";**

**}**

**isPalindrome(1551);**

**Question : How to find out Fibonacci number?**

**Answer:**The Fibonacci Sequence is the series of numbers in which next number is found by adding up the two numbers before it.

**The Rule is xn = xn-1 + xn-2**

where:

x(n) is term number "n"

x(n-1) is the previous term (n-1)

x(n-2) is the term before that (n-2)

**function fibonacciSeries($num){**

**$i = $sum = 0; $j=1;**

**print_r("FIBONACCI SERIES: ");**

**echo "$i , $j";**

**for($k=2;$k<$num;$k++){**

**$sum=$i+$j;**

**$i=$j;**

**$j=$sum;**

**echo " , $j";**

**}**

**}**

**fibonacciSeries(10);**

**How to switch PHP version 5.2 to 5.3 with .htaccess!**

**AddHandler application/x-httpd-php53 .php**

**Some string reverse examples:**

String: My Name is Sudhir

Output Need: Sudhir is Name My

Code:

**function stringReverse($str){**

**$i = 0;**

**while ($d = $str[$i]) {**

**if($d == " ") {**

**$out = " ".$temp.$out;**

**$temp = "";**

**}**

**else**

**$temp .= $d;**

**$i++;**

**}**

**echo $temp.$out;**

**}**

**stringReverse("My Name is Sudhir");**

String: My Name is Sudhir

Output Need: rihduS si emaN yM

Code:

**function stringReverse($str){**

**$reverse = '';**

**for($i = strlen($str) -1; $i >= 0; $i--){**

**$reverse .= $str[$i];**

**}**

**echo $reverse;**

**}**

**stringReverse("My Name is Sudhir");**

**Concisely reversing a string by word**

**$reversed_s = implode(' ',array_reverse(explode(' ',$s)));**

Another example of counting how many times an ASCII character

**$str = "PHP is pretty fun!!";**

**$strArray = count_chars($str,1);**

**foreach ($strArray as $key=>$value)**

**{**

**echo "The character <b>'".chr($key)."'</b> was found $value time(s)<br>";**

**}**

**array_merge() vs array_combine()**

**array_merge():**Merge one or more arrays

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one or if the arrays contain numeric keys, then the later value will not overwrite the original value and will be appended.

**$array1 = array("color" => "red", 1, 2,4);**

**$array2 = array("sudhir", "pandey", "color" => "green", "shape" => "trapezoid", 4);**

**$result = array_merge($array1, $array2);**

**array_combine():**Creates an array by using one array for keys and another for its values

In array_combine both array should have an equal number of elements.

**How to swap two number without using 3rd variable**

**$a = $a + $b;**

**$b = $a - $b;**

**$a = $a - $b;**

This comment has been removed by the author.

ReplyDeleteThis comment has been removed by the author.

ReplyDeleteI very much enjoyed this article.Nice article thanks for given this information. i hope it useful to many pepole.more.php jobs in hyderabad.

ReplyDeleteNice and well written post! I stumbled on this post when I was googling Dot Net Job Support. This one is the perfect for Freshers. This will work like magic for them. Thanks!

ReplyDelete