Skip to content

Best 20+ Interview Questions on PHP for Freshers and Experienced

Here we have provided Interview Questions on PHP for both freshers and experienced. These are some important questions that will ask in every interview of PHP.

Interview Questions on PHP for Freshers


1. How does one access standard input/output and error streams in PHP.

Ans:

STDIN An already opened stream to stdin. This saves opening it with $stdin = fopen(‘php://stdin’, ‘r’);

STDOUT An already opened stream to stdout. This saves opening it with $stdout = fopen (‘php://stdout’, ‘w’);

STDERR An already opened stream to stderr. This saves opening it with $stderr = fopen(‘php://stderr’, ‘w’);


2. What are the distance between the access modifier public/private/protected.

Ans:

  • Public scope to make that property/ method available from anywhere, other classes, and instances of the object.
  • Private scope when you want your property/method to be visible in its own class only.
  • Protected scope when you want to make your property/method visible in all classes that extend the current class including the parent class.

3. How can you ensure that a given object has a particular set of methods?

Ans: Implement an interface that defines a list of those methods.


4. Which types of loops exist in PHP?

Ans: for, while, do-while, for each.


5. What is the $_REQUEST variable?

Ans” An associative array that by default contains the contents of $_GET, $_POST, and $_COOKIE.


6. How can we remove duplicate values from an array?

Ans: The function array_unique function can be used for this purpose.


7. What is the difference between sessions and cookies?

Ans: The main difference between session and cookie is that cookies are stored on the user’s computer in the text file format while sessions are stored on the server-side.

Cookies can’t hold multiple variables, on the other hand, Session can hold multiple variables.

You can manually set an expiry for a cookie, while the session only remains active as long as the browser is open.


8. How can we get the error when there is a problem uploading a file?

Ans: $_FILES[‘userfile’][‘error’] contains the error code associated with the uploaded file.


9. What does the array operator ‘===’ means?

Ans: $a === #b TRUE if $a and $b have the same key/value pairs in the same order and of the same types.


10. How can we determine whether a PHP variable is an instantiated object of a certain class?

Ans: To be able to verify whether a PHP variable is an instantiated object of a certain class we use instanceof.

 

Interview Questions on PHP

Interview Questions on PHP

11. What are the three classes of errors that can occur in PHP?

Ans: The three basic classes of errors are notices (non-critical), warning (serious errors), and fatal errors (critical errors).


12. Which functions are used in PHP to search a particular value in an array?

Ans: in_array() function is used to search a particular value in an array.


13. What is the purpose of the break and continue statement?

Ans:

Break: It terminates the for loop or switch statement and transfers execution to the statement immediately following the for loop or switch.

Continue: It causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.


Interview Questions on PHP for Experienced

Interview Questions on PHP

Interview Questions on PHP

1. What is the difference between the ‘include’ and ‘require’ language constructs?

Ans: Require constructs cause a data error if the file can’t be read. Include won’t.


2. What is the best measure one can take to prevent a cross-site request forgery (CSRF)?

Ans: Add a secret token to all form submissions.


3. What is the problem with the following and how to solve it:

$date = new\datetime();

$date = $date1;

$date2> modify(=1 day);

Ans: The $date2 is a reference to date1 and changing one of them will change the other. The correct way to do this is to use a clone operator.


4. What is the primary difference between a method declared as static and a normal method?

Ans:

  • The static method can call without an object while the instance method can not be called without an object.
  • Static methods do not provide a reference to $this.

5. How can you convert an object into a string and customize the returned string when casted as a string: echo((string) $myObject).

Ans: Implemented the magic function _toString inside the object class. Being a loosely typed language, the same variable in PHP can be referred to or used as a number, object, or string. Now, the method _toString() can be called when an object needs to be treated like a string.


6. What is the use of the function HTML entities?

Ans: Convert all applicable characters to HTML entities, The function is identical to htmlspecialchars(), in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.


7. What is the difference between the function unlink and unset?

Ans: In PHP unlink() is a function for file system handling, unlink() is used to delete files. unset() is a function for variable management. It will make a variable undefined.


8. How can you pass a variable by reference?

Ans: To be able to pass a variable by reference, we use an ampersand in front of it, as follow $var1 = &$var2


9. What are Traits?

Ans: Traits are a mechanism that allows you to create reusable code in languages like PHP where multiple inheritances are not supported. A Trait cannot be instantiated on its own and enable developers to reuse combinations of methods from different class hierarchies.


10. Can you explain the difference between an abstract class and an interface?

Ans:

Abstract Classes: 

  • An abstract class can provide some functionality and leave the rest for the derived class.
  • The derived class may or may not override the concrete functions defined in the base class.
  • A child class extended from an abstract class should logically be related.

Interface:

  • An interface cannot certain of any functionality. It only contains definitions of the methods.
  • The derived class must provide code for all the methods defined in the interface.
  • Completely different and non-related classes can be logically grouped together using an interface.

These are some of the Interview questions on PHP for freshers and experienced. If you have any doubt related to these PHP questions ask me in the comment section.

Source: Nafaa AZAIEZ

 

Leave a Reply

Your email address will not be published.

%d bloggers like this: