PHP Interview Questions And Answers for Experienced

PHP Interview Questions And Answers for Experienced

PHP Interview Questions And Answers for Experienced

Question: What’s the difference between the include() and require() functions?

They both include a specific file but on require the process exits with a fatal error if the file can’t be included, while include statement may still pass and jump to the next step in the execution

Question: 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.

Question: 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.

Question: 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.

Question: What is the difference between GET and POST?

GET displays the submitted data as part of the URL, during POST this information is not shown as it’s encoded in the request.
GET can handle a maximum of 2048 characters, POST has no such restrictions.
GET allows only ASCII data, POST has no restrictions, binary data are also allowed.
Normally GET is used to retrieve data while POST to insert and update.

Question: 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 one can instantly get the exact line that is producing the error and can see also if the script in general is behaving correctly.

Also Read:- How to add Days, Hours, Minutes, and Seconds to Datetime in PHP

Question: What are Traits?

Traits are a mechanism that allows you to create reusable code in languages like PHP where multiple inheritance is not supported. A Trait cannot be instantiated on its own.

It’s important that a developer knows the powerful features of the language (s)he is working on, and Trait is one of such features.

Question: Can the value of a constant change during the script’s execution?

No, the value of a constant cannot be changed once it’s declared during the PHP execution.

Question: 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.

Question: What are the __construct() and __destruct() methods in a PHP class?

All objects in PHP have Constructor and Destructor methods built-in. The Constructor method is called immediately after a new instance of the class is being created, and it’s used to initialize class properties. The Destructor method takes no parameters.

Question: How we can get the number of elements in an array?

The count() function is used to return the number of elements in an array.

Also Read:- PHP developers functophobic

Question: How would you declare a function that receives one parameter name hello?

If hello is true, then the function must print hello, but if the function doesn’t receive hello or hello is false the function must print bye.

<?php
function showMessage($hello=false){
 echo ($hello)?’hello’:’bye’;
}
?>

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

Private — Visible only in its own class
Public — Visible to any other code accessing the class
Protected — Visible only to classes parent(s) and classes that extend the current class

Question: What are getters and setters and why are they important?

Getters and setters are methods used to declare or obtain the values of variables, usually private ones. They are important because it allows for a central location that is able to handle data prior to declaring it or returning it to the developer. Within a getter or setter one is able to consistently handle data that will eventually be passed into a variable or additional functions. An example of this would be a user’s name. If a setter is not being used and the developer is just declaring the $userName variable by hand, you could end up with results as such: “kevin”, “KEVIN”, “KeViN”, “”, etc. With a setter, the developer can not only adjust the value, for example, ucfirst($userName), but can also handle situations where the data is not valid such as the example where “” is passed. The same applies to a getter?—?when the data is being returned, it can be modifyed the results to include strtoupper($userName) for proper formatting further up the chain.

Question: 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.

Also Read:- Create a Simple CRUD Database App - Connecting to MySQL with PHP

Question: What are the different types of errors in php? 

E_ERROR: A fatal error that causes script termination
E_WARNING: Run-time warning that does not cause script termination
E_PARSE: Compile time parse error.
E_NOTICE: Run time notice caused due to error in code
E_CORE_ERROR: Fatal errors that occur during PHP’s initial startup (installation)
E_CORE_WARNING: Warnings that occur during PHP initial startup
E_COMPILE_ERROR: Fatal compile-time errors indication problem with script.
E_USER_ERROR: User-generated error message.
E_USER_WARNING: User-generated warning message.
E_USER_NOTICE: User-generated notice message.
E_STRICT: Run-time notices.
E_RECOVERABLE_ERROR: Catchable fatal error indicating a dangerous error
E_ALL: Catches all errors and warnings

Question: What is meant by urlencode and urldocode?

Urlencode() returns the URL encoded version of the given string. URL coding converts special characters into % signs followed by two hex digits. For example: urlencode(“10.00%”) will return “10%2E00%25?. URL encoded strings are safe to be used as part of URLs.
urldecode() returns the URL decoded version of the given string.

Author Biography.

CrowdforThink
CrowdforThink

CrowdforThink is the leading Indian media platform, known for its end-to-end coverage of the Indian startups through news, reports, technology and inspiring stories of startup founders, entrepreneurs, investors, influencers and analysis of the startup eco-system, mobile app developers and more dedicated to promote the startup ecosystem.

News & Blogs

6cea77c21ed8326243beef09eb717236.png

Latest Top 20 Team Foundation Server (TFS) Inte...

1. Mention What Is Team Foundation Server? Team foundation server is used for inter-communication...

37fed12d5b45592ae61b500bce26d1ee.jpg

Latest Top 20 Ruby On Rails Interview Questions...

1. What Is Rails? Rails is a extremely productive web-application framework written in Ruby langu...

be4a44b5df74b5568ff2f8049ec2cf16.png

Top Benefits of Doing SAS Certification

SAS software has established itself as the market leader in delivering a new generation of busine...