PHP Interview Questions Part 1

PHPInterviewQuestions-1

Hi guys, here today i came with some PHP interview questions which is submitted by some of our blog readers. And i requesting you guys that if you have any interview questions which can help others please share with me.

1- What are the main error types in PHP and how do they differ?

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.

2- How can you enable error reporting in PHP?

Check if “display_errors” is equal “on” in the php.ini or declare “ini_set(‘display_errors’, 1)” in your script. Then, include “error_reporting(E_ALL)” in your code to display all types of error messages during the script execution.

Enabling error messages is very important especially during the debugging process as you can instantly get the exact line that is producing the error and you can see also if the script in general is behaving correctly.

3- What is MIME?

Full form of MIME is “Multi-purpose Internet Mail Extensions”. It is extension of e-mail protocol helps to exchanges the different kids of data files over the internet. Data files may be audio, video, images, application programs and ASCII etc.

4- What is use of header() function in php?

Header is used to redirect from current page to another page.

header(“Location: anotherpage.php”);

Header is used to send HTTP status code.

header(“HTTP/1.0 404 Not Found”);

Header is used to send Send a raw HTTP header.

header(‘Content-Type: application/pdf’);

5- How to scrape the data from website using CURL?

To scrap the data from website, Website must be public and open for scrapable. By using below code we can scrap website:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “http://www.web-technology-experts-notes.in/”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

6- Can you extend a Final defined class?

No, you cannot extend a Final defined class. A Final class or method declaration prevents child class or method overriding.

7- What does MVC stand for and what does each component do?

MVC stands for Model View Controller. The controller handles data passed to it by the view and also passes data to the view. It’s responsible for interpretation of the data sent by the view and dispersing that data to the appropriate models awaiting results to pass back to the view. Very little, if any business logic should be occurring in the controller.

The model’s job is to handle specific tasks related to a specific area of the application or functionality. Models will communicate directly with your database or other storage system and will handle business logic related to the results.

The view is passed data by the controller and is displayed to the user.

Overall, this question is worth knowing as the MVC design pattern has been used a lot in the last few years and is a very good design pattern to know. Even with more advanced flows that go down to repositories and entities, they still are following the same basic idea for the Controller and View. The Model is typically just split out into multiple components to handle specific tasks related to database data, business logic etc. The MVC design pattern helps draw a better understanding of what is being used, as a whole, in the industry.

8- Why would you use === instead of ==?

If you would want to check for a certain type, like an integer or boolean, the === will do that exactly like one would expect from a strongly typed language, while == would convert the data temporarily and try to match both operand’s types. The identity operator (===) also performs better as a result of not having to deal with type conversion. Especially when checking variables for true/false you want to avoid using == as this would also take into account 0/1 or other similar representation.

9- What are different type of sorting functions in PHP?

sort() – sort arrays in ascending order. asort() – sort associative arrays in ascending order, according to the value.
ksort() – sort associative arrays in ascending order, according to the key.
arsort() – sort associative arrays in descending order, according to the value.
rsort() – sort arrays in descending order.
krsort() – sort associative arrays in descending order, according to the key.
array_multisort() – sort the multi dimension array.
usort()- Sort the array using user defined function.

10- How does one prevent the following Warning ‘Warning: Cannot modify header information – headers already sent’?

Do not output anything to the browser before using code that modifies the HTTP headers. Once you call echo or any other code that clears the buffer you can no longer set cookies or headers. That is also true for error messages, so if an error happens before you use the header command and the INI directive display_errors is set then that will also cause that error to show.