Top PHP Interview Questions And Answers

Top PHP Interview Questions And Answers

1. Compare PHP & JAVA
Criteria PHP Java
Deployment area Server-side scripting General purpose programming
Language type Dynamic typed Static typed
Rich set of APIs No Yes
2. How can we encrypt password using PHP?

crypt () function is used to create one way encryption. It takes one input string and one optional parameter. The function is defined as: crypt (input_string, salt), where input_string consists of the string that has to be encrypted and salt is an optional parameter. PHP uses DES for encryption. The format is as follows:

php code
3. Explain how to submit a Form without a submit button.

A form can be posted or submitted without the button in the following ways:

1. On OnClick event of a label in the form, a JavaScript function can be called to submit the form.


2. Using a Hyperlink: On clicking the link, JavaScript function can be called.


Q5 php IQA code

A form can be submitted in these other ways without using submit button.

  • Submitting a form by clicking a link
  • Submitting a form by selecting an option from drop down box with the invocation of onChange event
  • Using java script : document.form.submit();
  • Using header(“location:page.php”);
4. How can we increase the execution time of a PHP script?
  • Default time allowed for the PHP scripts to execute is 30 secs mentioned in the php.inifile. The function used is set_time_limit(int sec). If the value passed is ‘0’, it takes unlimited time. It should be noted that if the default timer is set to 30 sec, and 20 sec is specified in set_time_limit(), the script will run for 45 seconds.
  • This time can be increased by modifying the max_execution_time in secs. The time must be changed keeping the environment of the server. This is because modifying the execution time will affect all the sites hosted by the server.
  • The script execution time can be increased by
  1. Using sleep() function in PHP script
  2. Using set_time_limit() function
  3. The default limit is 30 seconds. The time limit can be set to zero to impose no time limit to pause.
5. What is Zend Engine?
  • Zend Engine is used internally by PHP as a compiler and runtime engine. PHP Scripts are loaded into memory and compiled into Zend opcodes.
  • These opcodes are executed and the HTML generated is sent to the client.
  • The Zend Engine provides memory and resource management, and other standard services for the PHP language. Its performance, reliability and extensibility played a significant role in PHP’s increasing popularity.
6. What library is used for pdf in PHP?

The PDF functions in PHP can create PDF files using the PDFlib library Version 6. PDFlib offers an object-oriented API for PHP 5 in addition to the function-oriented API for PHP 4.
There is also the » Panda module. FPDF is a PHP class, which allows generating PDF files with pure PHP (without using the PDFlib library.)
F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs. FPDF requires no extension (except zlib to activate compression and GD for GIF support) and works with PHP4 and PHP5.

7. What are some new features introduced in PHP7?
  1. Zend Engine 3 performance improvements and 64-bit integer support on Windows
  2. uniform variable syntax AST-based compilation process
  3. added Closure::call()
  4. bitwise shift consistency across platforms
  5. (null coalesce) operator
  6. Unicode codepoint escape syntax
  7. return type declarations
  8. and scalar type (integer, float, string and boolean) declarations.
8. What is htaccess? Why do we use this and where?
  • htaccess files are configuration files of Apache Server that provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.
  • These .htaccess files are used to change the functionality and features of Apache web server.
    For instance, htaccess file is used for url rewrite.
    –> It is used to make the site password protected.
    –> .htaccess file can restrict some ip addresses so that on restricted ip addresses, the site will not open.
9. What are magic methods?
  • Magic methods are member functions that are available to all the instance of class. Magic methods always start with “__”. Eg. __construct.
  • All magic methods need to be declared as public
  • To use a method, it should be defined within the class or program scope
  • Various Magic Methods used in PHP 5 are: __construct() __destruct() __set() __get() __call() __toString() __sleep() __wakeup() __isset() __unset() __autoload() __clone().

10. What is meant by PEAR in PHP?

PEAR is an acronym for “PHP Extension and Application Repository” The purpose of PEAR is to provide:

  • A structured library of open-sourced code for PHP users
  • A system for code distribution and package maintenance
  • A standard style for writing code in PHP
  • PHP Foundation Classes (PFC)
  • PHP Extension Community Library (PECL)
  • A website, mailing lists and download mirrors to support the PHP/PEAR community
11. Explain soundex() and metaphone().

The soundex() function calculates the soundex key of a string. A soundex key is a four character long alphanumeric strings that represents English pronunciation of a word. The soundex() function can be used for spelling applications.

$str= “hello”;
Echo soundex($str);

the metaphone() function calculates the metaphone key of a string. A metaphone key represents how a string sounds if pronounced by an English person. This function can also be used for spelling applications.

echo metaphone(“world”);
12. What is smarty?

Smarty is a template engine written in PHP. Typically, these templates will include variables —like {$variable} — and a range of logical and loop operators to allow adaptability within of the template.

13. What is Memcache?

Memcache is a technology that caches objects in memory such that your web application can get to them really fast. It is used by sites such as, and and is widely recognized as an essential ingredient in scaling any LAMP.
14. How can we execute a PHP script using command line?
  • Just run the PHP CLI (Command Line Interface) program and provide the PHP script file name as the command line argument. For example, “php myScript.php”, assuming “php” is the command to invoke the CLI program.
  • Remember that if your PHP script was written for the Web CGI interface, it may not execute properly in command line environment.

Advanced Questions

1. How to scrape data from website using CURL?

To scrap the data from website, Website must be public and open for scrapable.
In the blow code, just update the CURLOPT_URL to which websites data you want to scrap.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
echo $output;
2. Explain the difference between $message and $$message?
  • $message is used to store variable data. $$message can be used to store variable of a variable. Data stored in $message is fixed while data stored in $$message can be changed dynamically.
$var1 = ‘Variable 1’
$var1= ‘variable2’
This can be interpreted as $ Variable 1=‘variable2’;
For me to print value of both variables, I will write
$var1 $($var1)
  • $message is a variable and $$message is a variable of another variable.
$Message = "YOU";
$you= "Me";
echo $message //Output:- you
echo $message //output :-Me

$$message allows the developer to change the name of the variable dynamically.

3. How urlencode and urldecode can be used?

Urlencode can be used to encode a string that can be used in a url. It encodes the same way as posted data from web page is encoded. It returns the encoded string.

urlencode (string $str )

urlencode () is the function that can be used conveniently to encode a string before using in a query part of a URL. This is a convenient way for passing variables to the next page.

urldecode (string $str )

urldecode() is the function that is used to decode the encoded string. Urldecode can be used to decode a string. Decodes any %## encoding in the given string (Inserted by urlencode.)

4. How to set HTTP header to UTF-8 using?

header(‘Content-Type: text/html; charset=utf-8’);

5. Which PHP Extension help to debug the code?

Xdebug: – It uses the DBGp debugging protocol for debugging.
The debug information that Xdebug can provide includes the following:

  • stack and function traces in error messages with:
  • full parameter display for user defined functions
  • function name, file name and line indications
  • support for member functions
  • memory allocation
  • protection for infinite recursions

Xdebug also provides:

  • profiling information for PHP scripts
  • code coverage analysis
  • Capabilities to debug your scripts interactively with a debugger front-end.[4] Xdebug is also available via PECL

6. How can I execute an anonymous function?

call_user_func(function() { echo ‘anonymous function called.’; });

7. Explain how to send large amounts of emails using PHP.

There are different methods through which we can send mails in PHP. They are as follows:

  1. PHP mail() function
    It implicitly sends a message to SMTP server, which is configured in the php.ini file. This function is used by the base class of MIME message composing and sending package.
  2. SMTP server relay
    They are used to relay the messages to an intermediate SMTP server. This server stores the messages temporarily and will try to deliver them in the destination SMTP server.
  3. Sending urgent messages by doing direct delivery to the destination SMTP server
    A variable named direct_delivery is provided by the smtp_message_class sub-class, which connects to the destination SMTP server and sends the message directly.
8. Explain how to get DNS servers of a domain name.
  • Include Net/DNS.php file in the beginning of the script
  • Create an object for DNS resolver by using $ndr = Net_DNS_Resolver()
  • Query the ip address using $ndr->search(“”,”A”) and assign to a relevant variable. Ex: $result
  • Now, display the value of $result
9. How can I measure the speed of code written in PHP?

$startTime= microtime(true);
/** Write here you code to check **/
/** Write here you code to check **/
$endTime= microtime(true);
echo ‘Time Taken to execute the code:’.$endTime-$startTime

10. How can we resolve maximum allocation time exceeds error?

We can resolve these errors through php.ini file or through .htaccess file.

  1. From php.ini file, increase the max_execution_time =360 (or more according to need)
    and change memory_limit =128M (or more according to need)
  2. From php file, we can increase time by writing ini_set(‘max_execution_time’,360 ) at top of php page to increase the execution time.And to change memory_limit write ini_set(‘memory_limit ,128M )
  3. From .htaccess file, we can increase time and memory by:
Q15 php IQA code
11. In how many ways can you retrieve data in the result set of MySQL using PHP? What is the difference between mysql_fetch_object and mysql_fetch_array?

We can retrieve data in the result set of MySQL using PHP in four Ways

  1. mysqli_fetch_row >> Get a result row as an enumerated array
  2. mysqli_fetch_array >> Fetch a result row as associative and numeric array
  3. mysqli_fetch_object >> Returns the current row of a result set as an object
  4. mysqli_fetch_assoc >> Fetch a result row as an associative array
    mysqli_fetch_object() is similar to mysqli_fetch_array(), with one difference –
    an object is returned instead of an array, which implies that that we can only access the data by the field names, and not by their offsets (numbers are illegal property names).
12. Can we use include (“xyz.PHP”) two times in a PHP page “index.PHP”?

How can we destroy a session in PHP
Yes, we can include (“xyz.php”) more than one time in any page. But it creates a problem when a xyz.php file contains some function declaration- an error occurs due to an already present function in this file. Otherwise, there is no problem, for instance if you want to show same content two times in the page then you must include it two times.

13. How do we change a password for an existing user via mysqladmin?

mysqladmin -u root -p password “newpassword”

14. How to Get the Uploaded File Information in the Receiving Script?

Once the Web server received the uploaded file, it will call the PHP script specified in the form action attribute to process them. This received PHP script can get the uploaded file information through a predefined array called $_FILES. Uploaded file information is organized in $_FILES as a two-dimensional array as:

  • $_FILES[$fieldName][‘name’] : Original file name on the browser system.
  • $_FILES[$fieldName][‘type’] : the file type determined by the browser.
  • $_FILES[$fieldName][‘size’] : Number of bytes of the file content.
  • $_FILES[$fieldName][‘tmp_name’] : a temporary filename of the file in which the uploaded file was stored on the server.
  • $_FILES[$fieldName][‘error’] an error code associated with this file upload.
  • The $fieldName is the name used in the <INPUT,>.
15. How to protect Special Characters in Query String?

If you want to include special characters like spaces in the query string, you need to protect them by applying the urlencode() translation function. The script below shows how to use urlencode():

Q30 php IQA code
16. How can we destroy a session in PHP?

We can destroy a session by:

Q22 part 1 php IQA code

To delete a specific session variable, we use

Q22 part 2 php IQA code

17. What will be the output of following?

function changevalue(&$y)  {  $y = $y + 7;  }  $num = 8;   
echo $num;

It would be: 15
Reference will take the value and will add 5 to it.

Leave a Reply