Top 20 PHP Interview Questions and Answers
Table of Contents
- 1 What is PHP?
- 2 Explain the differnce between $message vs $$message?
- 3 Explain the different type of errors?
- 4 How to include a file in php page?
- 5 What is the difference between include(), require(), and require_once() ?
- 6 Differentiate between echo Vs. print statement?
- 7 Differentiate between GET and POST methods?
- 8 What is the Difference between Session and Cookie?
- 9 How to set and retrieve cookie in PHP?
- 10 What is urlencode and urldecode?
- 11 What does PEAR stand for in PHP?
- 12 What is the difference between “==” and “===” operator?
- 13 How to submit form without a Submit button?
- 14 Differentiate between unlink Vs unset?
- 15 What is difference between split and explode?
- 16 What is Type Juggling in PHP?
- 17 What is Zend Engine?
- 18 What is CAPTCHA?
- 19 Explain How to Execute a PHP Script from command line?
- 20 What is the main difference between PHP 4 and PHP 5?
- 21 Related
This post covers Top 20 PHP Interview Questions Asked to freshers and experienced. If you are a PHP Programmer, can you answer these questions?
Let’s start with the Basics and then we will move to the tough questions asked in the php interviews.
Sort Answer – PHP (Hypertext Processor) is a server side scripting language commonly used for web applications.
Long Answer – PHP : Hypertext Processor is open source server side scripting language that is widely used for web development. PHP allows writing dynamically generate web pages efficiently and quickly.It is also an object oriented programming language and its syntax is mostly borrowed from C, Java, Perl. PHP is Free to download and ease to use.
Explain the differnce between $message vs $$message?
Answer – $message is a variable with a fixed name while $$message is a variable whose name is stored in $message.
For Example: If $message contains “mct”,$$message will be same as $mct.
Explain the different type of errors?
There are three type of errors: Notices,Warnings and Fatal errors.
Notices represent non-critical errors, i.e. accessing a variable that has not yet been defined. By Default, these errors are not displayed to users at all, but you can change this default behaviour.
Warnings are serious errors, but they don’t result in script termination, i.e calling include() function with file as a parameter but the file doesn’t exit.By Default, these errors are displayed to users.
- Fatal Errors:
Fatal Errors are critical errors i.e calling a non-existent function or class. They result in immediate termination of script, i.e calling a require() function with a file as a parameter but the file doesn’t exist. By Default these errors are displayed to users.
How to include a file in php page?
Answer – We can include a file in php using require() or include() function.
What is the difference between include(), require(), and require_once() ?
- include() and require() :include() and require() will include the file specified as a parameter.The difference between them are the way the handle the failures. include() produces a warning while require() produces a fatal error.
- require_once() : require_once() include and execute the file if the code of the file has not been included before.
Summarised Answer – require() and include() are the almost similar except the way they handle failures. include() produces a Warning while require() produces a fatal error.require_once() includes the file i the file has not been included before.
Differentiate between echo Vs. print statement?
echo() and print() both are used to output strings.Their speed are almost same.
echo() can take multiple expressions while print() cannot take multiple expressions.echo has no return value while print has a return value of true or false so it can be used in expressions.
Differentiate between GET and POST methods?
We can send 1024 bytes using GET method but POST method can transfer large amount of data. POST is the secure method than GET method.
What is the Difference between Session and Cookie?
The main difference between Session and cookie is that session stores data on sever while cookie stores data on users local computer.Cookie cannot hold multiple variables while session can hold multiple variables.
We can set expiry for cookie but the session remained active till the browser is open.
Users don’t have access to session whereas they have access to cookies.
We can set cookie using the cookie syntax.
<?php $value = 'Your Cookie Value'; setcookie("TestCookie", $value, time()+3600); /* expire in 1 hour */ ?>
and to retrieve our TestCookie we will use
What is urlencode and urldecode?
urlencode can be used to encode a string that can be used in a url. It encodes the data same way posted data from web page is encoded. It returns the encoded string.
urlencode (string $str )
urldecode() is the function that is used to decode the encoded string.
What does PEAR stand for in PHP?
PEAR stand for PHP Extension and Application Repository. It is a framework and repository for reusable PHP Components.
What is the difference between “==” and “===” operator?
“==” compares the value of a variable for equality.It’s not case sensitive while “===” is case sensitive.”===” will return true if both operands have same type and same value.
unlink is a function for file system handling which deletes a file.unset function is used to destroy a variable.
What is difference between split and explode?
Both the functions are used to split a string.But split is used to split a string using regular expression while explode is used to split a string using another string.
$string = "India Vs South Africa"; $explode = explode("Vs",$string); returns an array Contains India, South Africa $split = split("/Vs/",$string); return same as Above
What is Type Juggling in PHP?
Type Juggling is dealing with variables.In other programming languages declaring variables requires variable’s data type and name of that variable and then variable will be initialized.
But, in PHP there is no need of any type of specification while php variable declaration.
What is Zend Engine?
Zend Engine is used internally by PHP as a compiler and runtime engine.Its reliability, performance and extensibility has a significant role in increasing the PHP’s popularity.The Zend Engine II is the heart of PHP 5. It is an open source project and freely available under BSD style license.
What is CAPTCHA?
CAPTCHA is a test to determine whether the user using the System is a Human or Robot. Captcha are images with some characters/digits on it.One need to type the character or digits in the text box for the purpose of submitting.
Explain How to Execute a PHP Script from command line?
Just Use the PHP CLI(Command Line Interface) and specify the file name of the php script to be executed.
What is the main difference between PHP 4 and PHP 5?
PHP 5 presents many additional OOP(Object Oriented Programming) features.
In PHP4, Constructor have same name as the Class name.
In PHP5, name Constructors as _construct and Destructors as _destruct().