PHP Interview Questions

There is given PHP interview questions and answers that have been asked in many companies. Let's see the list of top PHP interview questions.

What is the difference between characters \023 and \x23?

The first one is octal 23, the second is hex 23..

What’s the special meaning of __sleep and __wakeup?

__sleep returns the array of all the variables than need to be saved, while __wakeup retrieves them..

What is the difference between ereg_replace() and eregi_replace()?

eregi_replace() function is identical to ereg_replace() except that it ignores case distinction when matching alphabetic characters..

How can we encrypt the username and password using PHP?

You can encrypt a password with the following Mysql>SET PASSWORD=PASSWORD(“Password”);

Or

You can use the MySQL PASSWORD() function to encrypt username and password.

Example: INSERT into user (password, …) VALUES (PASSWORD($password”)), …);

.

What is garbage collection?

It is an automated feature of PHP. When it runs, it removes all session data which are not accessed for a long time. It runs on /tmp directory which is the default session directory. PHP directives which are used for garbage collection include:

  • session.gc_maxlifetime (default value, 1440)
  • session.gc_probability (default value, 1)
  • session.gc_divisor (default value, 100)
.

What is difference between Method overriding and overloading in PHP?

Overriding and Overloading both are oops concepts.

In Overriding, a method of the parent class is defined in the child or derived class with the same name and parameters. Overriding comes in inheritance.

An example of Overriding in PHP.

<?php

class A {
   function showName() {
      return "Ajay";
   }
}

class B extends A {
   function showName() {
      return "Anil";
   }
}

$foo = new A;
$bar = new B;
echo($foo->myFoo()); //"Ajay"
echo($bar->myFoo()); //"Anil"
?>

In Overloading, there are multiple methods with the same name with different signatures or parameters.

Overloading is done within the same class. Overloading is also called early binding or compile time polymorphism or static binding.

An example of Overloading in PHP

<?php 
class A{

function sum($a,$b){
	return $a+$b;

}

function sum($a,$b,$c){
	return $a+$b+$c;

}

}

$obj= new A;
$obj->sum(1,3); // 4
$obj->sum(1,3,5); // 9
?>
.

What is difference between md5 and SHA256?

Both MD5 and SHA256 are used as hashing algorithms. They take an input file and generate an output which can be of 256/128-bit size. This output represents a checksum or hash value. As, collisions are very rare between hash values, so no encryption takes place.

  • The difference between MD5 and SHA256 is that the former takes less time to calculate than later one.
  • SHA256 is difficult to handle than MD5 because of its size.
  • SHA256 is less secure than MD5
  • MD5 result in an output of 128 bits whereas SHA256 result output of 256 bits.
Concluding all points, it will be better to use MDA5 if you want to secure your files otherwise you can use SHA256..

How can we convert the time zones using PHP?

date_default_timezone_set() funtion is used to convert/change the time zones in PHP.

Example

date_default_timezone_set('Asia/Kolkata');
.

What is the difference between abstract class and interface?

  • Abstract classes are used for closely related objects and interfaces are used for unrelated objects.
  • PHP class can implement multiple interfaces but can’t inherit multiple abstract classes.
  • Common behavior can be implemented in the abstract class but not an interface.
.

How can image properties be retrieved in PHP?

  • getimagesize() – It is used to get the image size.
  • exif_imagetype() – It is used to get the image type.
  • imagesx() – It is used to get the image width.
  • imagesy() – It is used to get the image height.
.