Tue Jul 17 2018
PHP interview questions
Personal Home Page in short PHP is a scripting language. PHP now use as Hypertext Preprocessor, which is powerful and widely-used open source server-side scripting language to write dynamically generated web pages. PHP executes on the server, while a comparable alternative, JavaScript, executes on the client. PHP is an alternative to Microsoft's Active Server Page (ASP) technology. The PHP script is embedded within a Web page along with its HTML. PHP can be integrated with the number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server. According to research PHP has a market share of about 79.23%. And there are a lot of opportunities from many reputed companies in the world. So, You have an opportunity to move ahead in your career in PHP Development. In this article, we are going to highlight some PHP Interview Questions that help you to crack your interview and acquire a dream career as a PHP developer. So, let's see the questions answers -
Q. What is PHP?
Ans. PHP stands for Hypertext Preprocessor. It is an open source server-side scripting language which is widely used for web development. It supports many databases like MySQL, Oracle, Sybase, Solid, PostgreSQL, generic ODBC etc.
Q. What was the old name of PHP?
Ans. Personal Home Page.
Q. What are the main error types in PHP and how do they differ?
Ans. 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.
Q. How can you enable error reporting in PHP?
Ans. 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.
Q. 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.
Q. What is the difference b/w static and dynamic websites?
Ans. In static websites, content can't be changed after running the script. You can't change anything in the site. It is predefined. In dynamic websites, content of script can be changed at the run time. Its content is regenerated every time a user visit or reload. Google, Yahoo, and every search engine is the example of a dynamic website.
Q. What is the name of scripting engine in PHP?
Ans. The scripting engine that powers PHP is called Zend Engine 2.
Q. What is session in PHP. How to remove data from a session?
Ans. As HTTP is state protocol. To maintain states on server and share data across multiple pages PHP session are used.PHP sessions are simple way to store data for individual users/client against a unique session ID. Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data, if session id is not present on server PHP creates a new session, and generate a new session ID.
Q. How to register a variable in PHP session?
Ans. In PHP 5.3 or below we can register a variable session_register() function.It is deprecated now and we can set directly a value in $_SESSION Global.
Q. What is the use of "echo" in PHP?
Ans. It is used to print a data in the web page, Example: , The following code print the text in the webpage
Q. How to include a file to a PHP page?
Ans. We can include a file using "include() " or "require()" function with file path as its parameter.
Q. What's the difference between include and require?
Ans. If the file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue.
Q. How to set cookies in PHP?
Ans. Setcookie("sample", "ram", time()+3600)
Q. How to Retrieve a Cookie Value?
Ans. eg : echo $_COOKIE["user"]
Q. Which programming language does PHP resemble?
Ans. PHP syntax resembles Perl and C
Q. What does PEAR stand for?
Ans. PEAR means “PHP Extension and Application Repository”. It extends PHP and provides a higher level of programming for web developers.
Q. How can we display the output directly to the browser?
Ans. To be able to display the output directly to the browser, we have to use the special tags .
Q. Is multiple inheritance supported in PHP?
Ans. PHP supports only single inheritance; it means that a class can be extended from only one single class using the keyword ‘extended’.
Q. How can PHP and HTML interact?
Ans. It is possible to generate HTML through PHP scripts, and it is possible to pass pieces of information from HTML to PHP.
Q. How can PHP and Javascript interact?
Ans. PHP and Javascript cannot directly interact since PHP is a server side language and Javascript is a client-side language. However, we can exchange variables since PHP can generate Javascript code to be executed by the browser and it is possible to pass specific variables back to PHP via the URL.
Q. How will you define a constant in PHP?
Ans. To define a constant you have to use define() function and to retrieve the value of a constant, you have to simply specify its name. Unlike with variables, you do not need to have a constant with a $.
Q. What are rules for naming a PHP variable?
Ans. Rules for naming a variable are following − Variable names must begin with a letter or underscore character. A variable name can consist of numbers, letters, underscores but you cannot use characters like + , - , % , ( , ) . & , etc.
Q. What are PHP magic constants?
Ans. PHP provides a large number of predefined constants to any script which it runs known as magic constants.
Q. How will you redirect a page using PHP?
Ans. The PHP header() function supplies raw HTTP headers to the browser and can be used to redirect it to another location. The redirection script should be at the very top of the page to prevent any other part of the page from loading. The target is specified by the Location: header as the argument to the header() function. After calling this function the exit() function can be used to halt parsing of rest of the code.
Q. Which function will you use to create an array?
Ans. array() − Creates an array.
Q. How can you sort an array?
Ans. sort() − Sorts an array.
Q. How will you read a file in PHP?
Ans. Once a file is opened using fopen() function it can be read with a function called fread(). This function requires two arguments. These must be the file pointer and the length of the file expressed in bytes.
Q. How will you delete a cookie?
Ans. To delete a cookie you should call set cookie() with the name argument only.
Q. How will you send an email using PHP?
Ans. PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the message and the actual message additionally there are other two optional parameters.
Q. How will you destroy the session?
Ans. A PHP session can be destroyed by session_destroy() function.
Q. How ereg() function works?
Ans. ereg() − The ereg() function searches a string specified by string for a string specified by pattern, returning true if the pattern is found, and false otherwise.
Q. How will you retrieve the error message using Exception class in PHP when error occurred?
Ans. Using getMessage() method of Exception class which returns the message of exception.
Q. What is the purpose of getdate() function?
Ans. The function getdate() optionally accepts a time stamp and returns an associative array containing information about the date. If you omit the time stamp, it works with the current timestamp as returned by time().
Q. How will you parse an XML document using PHP?
Ans. PHP new SimpleXML module makes parsing an XML document, well, simple. It turns an XML document into an object that provides structured access to the XML. To create a SimpleXML object from an XML document stored in a string, pass the string to simplexml_load_string( ). It returns a SimpleXML object.
Q. Can you create a class in PHP? How will you add a constructor function to a PHP class?
Ans. Yes! PHP provides a special function called __construct() to define a constructor. You can pass as many as arguments you like into the constructor function.
Q. Differences between GET and POST methods?
Ans. We can send 1024 bytes using GET method but POST method can transfer large amount of data and POST is the secure method than GET method.
Q. What is the use of final keyword?
Ans. PHP introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
The questions that we mentioned above are not only which will ask in the interviews. You have to practice more questions answers related to PHP that interviewer may ask. Questions only depend on them. Hope this article successfully guided you and helped you to understand the level of interview questions. Best of luck for the future! Thank you!