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 are the main error types in PHP and how do they differ?
In PHP there are three main type of errors:
- Notices – Simple, non-critical errors that are occurred during the script execution. An example of a Notice would be accessing an undefined variable.
- Warnings – more important errors than Notices, however the scripts continue the execution. An example would be
include()a file that does not exist. - Fatal – this type of error causes a termination of the script execution when it occurs. An example of a Fatal error would be accessing a property of a non-existent object or
require()a non-existent file.
Understanding the error types is very important as they help developers understand what is going on during the development, and what to look out for during debugging.
.What are constructor and destructor in PHP ?
PHP constructor and destructor are special type functions that are automatically called when a PHP class object is created and destroyed.
Generally, Constructor is used to initializing the private variables for class and Destructors to free the resources created /used by the class.
Here is a sample class with a constructor and destructor in PHP.
.<?php class Foo { private $name; private $link; public function __construct($name) { $this->name = $name; } public function setLink(Foo $link){ $this->link = $link; } public function __destruct() { echo 'Destroying: '. $this->name; } } ?>
What is the use of count() function in PHP?
The PHP count() function is used to count total elements in the array, or something an object..
Which function is used in PHP to check the data type of any variable?
gettype() function is used to check the data type of any variable.
.echogettype(true).'';//booleanechogettype(10).'';//integerechogettype('Web Programming').'';//stringechogettype(null).'';//NULL
What’s the difference between unset() and unlink()
unset() sets a variable to “undefined” while unlink() deletes a file we pass to it from the file system..
Is PHP a case sensitive language?
Not fully. PHP is partly a case sensitive language where the variable names are case-sensitive but function names are not. Also, user-defined functions aren’t case sensitive but the rest of the language is..
How can we get the IP address of the client?
This question might show you how playful and creative the candidate is because there are many options.
$_SERVER["REMOTE_ADDR"];
is the easiest solution, but the candidate can write x line scripts for this question.
.Where sessions stored in PHP ?
PHP sessions are stored on the server generally in text files in a temp directory of the server. That file is not accessible from the outside world. When we create a session PHP create a unique session id that is shared by the client by creating a cookie on the client’s browser. That session id is sent by the client browser to the server each time when a request is made and the session is identified. The default session name is “PHPSESSID”..
Which function can be used to exit from the script after displaying the error message?
You can use exit() or die() function to exit from the current script after displaying the error message.
if(!fopen('t.txt','r'))exit(" Unable to open the file");
.if(!mysqli_connect('localhost','user','password'))die(" Unable to connect with the database");
What are the different loops in PHP?
For, while, do-while and for each..