Register Login

PHP Interview Questions

PHP Interview Questions For Freshers

1) What is _FILE_ constant?

A _FILE_ constant contains the filename and the full path of the file. If the constant is used inside an include, the included file name is returned.

2) Why is the break statement used?

The break statement terminates the switch statement or for loop and transfers the execution to the statement just after the switch or the for loop.

3) Explain the use of foreach loop in PHP?

The 'foreach' statement is used to loop through each key pair or value pair in an array. For every pass, the value of the current array element is assigned to $value and the array pointer is moved by one till the last array element is reached.

4) How can you identify a string within a string in PHP?

To search for another string or a character within a string, the function that is used is strpos(). If there is a match, the function returns the position of the match and otherwise, it returns FALSE.

5) How to redirect a page using PHP?

The browser obtains raw HTTP headers from the PHP header() function which is used to redirect it to some other location. After calling the header() function, the exit() function is used to prevent the rest of the code from getting parsed.

6) What is the difference between include() and require() functions?

In case of a problem in loading a file, the require () causes a fatal error stops the script's execution. In case of the same problem, the include() issues a warning but the execution of the script continues.

7) How can you set cookies using PHP?

The setcookie() function is used to set a cooking and the function is called separately to set each cookie. The syntax is: setcookie(name, value, expire, path, domain, security);

8) How is a cookie deleted?

The setcookie() function is called with only the name argument for deleting a cookie.

9) What is the use of final keyword?

The final keyword prefixes the definition of a method with itself and prevents the method from being overriden by a child class. If a class is defined as final, it means that the class cannot be extended.

10) How will you parse an XML document using PHP?

The SimpleXML module of PHP 5 helps to parse an XML document. The module turns the XML document into an object providing structured access to the XML. To create an object from the XML document that is stored inside a string, the string is passed to simplexml_load_string( ). It returns the SimpleXML object.

11) What is the purpose of the unlink() function?

The unlink() function is used to delete any file. It returns TRUE in case of success and FALSE in case of failure.

12) How is the ternary conditional operator used in PHP?

It has three expressions- one condition along with two operands that describe the function to be performed according to whether the said condition is true or false.
Condition? Operand_1 : Operand 2;

13) What is a persistent cookie?

A persistent cookie is one that remains stored in the cookie file unlike normal cookies that get erased when the browser is closed. Persistent cookies are created in the same way that temporary cookies are created; you just have to set an expiry date for the cookie to persist for that time period.

14) How does a session end?

A session ends on its own when the execution of the PHP script finishes. A session can also be ended manually by using the session_write_close() function.

15) What is the difference between for and foreach?

The PHP for loop executes a code for a certain number of times and is used only when it is known beforehand how many times the block of code has to be run.

Syntax:

for ($m=0; $m<=5; $m++); {
echo "The result is : $m"; }

The foreach loop works on arrays only and loops through each of the key/value pairs in an array.

Syntax:

foreach ($m as $value) {
echo "$value"; }

16) Explain whether it is possible to share a single instance of a Memcache between multiple PHP projects?

Yes, it is possible. Memcache is a memory space and can be run on multiple servers. Two independent Memcache processes can be run on the same host. Only if you are using partitioned data, then you need to know the instance in which you have to put the data or the one from which you have to get the data.

17) What does $GLOBALS mean?

$GLOBALS is a super global variable in PHP that can be used to access any global variable in a PHP script.

18)What is the difference between unset() and unlink()?

The unset() function sets the value of a variable to 'undefined' and the unlink() function deletes any file that is passed to it from the system.

19) What are the error types in PHP and how do they differ?

There are three main error types in PHP-

  • Notice: It refers to any simple error, like trying to access any undefined variable, that occurs during the execution of the script.
  • Warning: It is a more serious error than a notice but does not halt the execution of the script. For example, an include() a non-existing file.
  • Fatal: This is a major error that terminates the execution of the script. Examples of such an error would be to require() a file which doesn't exist or trying to access the property of an object that doesn't exist.

20) What is the difference between GET and POST?

The differences between GET and POST are-

  • GET can transfer not more than 2048 characters, POST has no constraints like that and can handle larger amount of data.
  • GET can transfer nothing but ASCII data while POST allows binary data too.
  • GET is for extracting data while POST is to insert and update.

21) What are Traits in PHP?

Trait is a very powerful feature of PHP that lets you create a reusable code which is essential in a language like PHP that does not support multiple inheritance.

22) What are _construct() and _destruct() methods in PHP?

The _construct() method signifies the constructor that is called immediately after the creation of a new instance variable in a class to initialize the properties of the class. The destructor method _destruct() does not take any parameter and is called when an obbject is to be deleted.

23) What are the 3 scope levels available in PHP and how would you define them?

The three scope levels are-

  • Private- The object is visible only in the class in which it is created.
  • Public- The object is visible to any code that accesses the class.
  • Protected- The object is visible to the parent class and child classes of the class.

24) What are getters and setters?

Getter is an object method that lets you 'get' or retrieve a property while Setter is another object method that lets you 'set' the value of a property.

25) What is 'echo' in PHP?

The echo() function is a slightly faster print function that can output multiple strings.


×