Skip to main content

PHP Fresher Technical Interview Questions

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;

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

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

    ReplyDelete
  4. Nice 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

Post a Comment

Popular posts from this blog

A Guide to UTF-8 for PHP and MySQL

Data Encoding: A Guide to UTF-8 for PHP and MySQL As a MySQL or PHP developer, once you step beyond the comfortable confines of English-only character sets, you quickly find yourself entangled in the wonderfully wacky world of UTF-8. On a previous job, we began running into data encoding issues when displaying bios of artists from all over the world. It soon became apparent that there were problems with the stored data, as sometimes the data was correctly encoded and sometimes it was not. This led programmers to implement a hodge-podge of patches, sometimes with JavaScript, sometimes with HTML charset meta tags, sometimes with PHP, and soon. Soon, we ended up with a list of 600,000 artist bios with double- or triple encoded information, with data being stored in different ways depending on who programmed the feature or implemented the patch. A classical technical rat’s nest.Indeed, navigating through UTF-8 related data encoding issues can be a frustrating and hair-pul...

How To Create Shortcodes In WordPress

We can create own shortcode by using its predified hooks add_shortcode( 'hello-world', 'techsudhir_hello_world_shortcode' ); 1. Write the Shortcode Function Write a function with a unique name, which will execute the code you’d like the shortcode to trigger: function techsudhir_hello_world_shortcode() {    return 'Hello world!'; } Example: [hello-world] If we were to use this function normally, it would return Hello world! as a string 2. Shortcode function with parameters function techsudhir_hello_world_shortcode( $atts ) {    $a = shortcode_atts( array(       'name' => 'world'    ), $atts );    return 'Hello ' . $a['name'] . !'; } Example: [hello-world name="Sudhir"] You can also call shortcode function in PHP using do_shortcode function Example: do_shortcode('[hello-world]');

Integrating Kafka with Node.js

Integrating Kafka with Node.js Apache Kafka is a popular open-source distributed event streaming platform that uses publish & subscribe mechanism to stream the records(data). Kafka Terminologies Distributed system: Distributed system is a computing environment where various software components located on different machines (over multiple locations). All components coordinate together to get stuff done as one unit.   Kafka Broker: Brokers are cluster of multiple servers. Message of each topic are split among the various brokers. Brokers handle all requests from clients to write and read events. A Kafka cluster is simply a collection of one or more Kafka brokers. Topics: A topic is a stream of "related" messages. Its unique throughout application. Kafka producers write messages to topics. Producer: Producer publishes data on the topics. A producer sends a message to a broker and the broker receives and stores messages. Consumers: Consumers read data from topics. A consu...