PHP interview questions and answers for experienced

In this section we are giving you some frequently asked PHP Interview Questions for experienced developers who have minimum 6 months of relevant experience in PHP based web developement. PHP programming knowledge in not only the measure of your web development experience, so that you must have good knowledge in its related technologies like CSS, HTML, JavaScript, Jquery etc and it will be a plus point if you have knowledge in Frameworks/ Tools like AngularJs, BootStrap, Linux and its commads , experience in deploying a website in a live server and good communication skill english. Before start reading the questions. It will be good to go through the php questions for freshers

  1. How to collect  IP address from an HTTP request ? 

    $_SERVER['REMOTE_ADDR'];

  2. How to collect  IP address of the Web server in php ? 

    $_SERVER['SERVER_ADDR'];

  3. How to enable error reporting ?

    error_reporting(E_ALL);

  4. What is $_GLOBAL ?

    It is an associative array which containing references to all variables currently defined in the global scope of the script.

  5. What is .htaccess file ?

    .htaccess is a configuration file used to alter the default behavior of a Apache web server software. Most common usage is to redirect the http request to some URLs based on some conditions. For example, we can hide the .html or .php extensions of the URLs to make it SEO friendly

  6. How to print structured information about a available in PHP ?

    var_dump — This function displays structured information about one or more expressions that includes its value and type. Objects and arrays are explored recursively with values indented to show structure.

    Usage   var_dump($a);

  7. What is the difference between var_dump and print_r ?

    var_dump will display all the information of a variable including keys values and types while print_r display the keys and values only in a human readable format.

  8. What is automatic type conversion ?

    In php we can declare variables without specifying its type, php it do that process automatically since PHP is a loosely types language.

    For example : 

    <?php  //$count is a string variable  $count = "5";  //$count is an int variable  $count = 5;  ?>
  9. How to make API calls from php scripts ?

    We can use cURL library to make HTTP calls from a php script

    $ch = curl_init();  $postData='var1=value1&var2=value2&var3=value3';  curl_setopt($ch, CURLOPT_URL, "http://mydomain.com/ajaxurl");  curl_setopt($ch, CURLOPT_POST, 1);  curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  $result = curl_exec($ch);  curl_close($ch);    echo $result;die;
  10. How to change the php configurations at run time ?

    We can use `ini_set` function to change the configurations of php at run time.

  11. How can we resolve the errors like 'Maximum execution time of 30 seconds exceeded' ?

    We can use the following code to increase the maximum execution time of the script 

    ini_set('max_execution_time', 300);

    We can also set the max_execution_time for all the scripts in a website by the following code in .htaccess file

    <IfModule mod_php5.c>  php_value max_execution_time 300  </IfModule> 

    But, we must try to optimize the php script to avoid this kind of situations as a part good user experience.

  12. How to avoid email sent through php getting into spam folder?

    There's no special method of keeping your emails from being identified as spam. However we can consider some points that cause this problem. Let me explain few common reasons.

    1. sending mail using the `mail` function with minimum parameters

        We must use all possible mail headers like `MIME-version`, `Content-type`, `reply address`, `from address` etc in order to avoid this situation

    2. Not using a proper SMTP mail script like PHPmailer or SwiftMailer with an actual e-mail credentials including username, password etc

        If we are able to send e-mail from an actual e-mail account using an SMTP mailer script with username and password, then we can avoid  

    If you’re on a shared web server, consider buying a unique IP address for yourself, because others using your IP may have gotten your IP blacklisted for spam. Do not send more than 250 emails to each provider per hour.

    Give your users unsubscribe link and view in browser link, if they cannot see the email properly they will mark you as spam, if they no longer want emails for you they will mark you as spam. 

    How can we prevent SQL injection in PHP?

    Most popular way is, use prepared statements and parameterized queries. These are SQL statements that are sent to and parsed by the database server separately from any parameters. This way it is impossible for an attacker to inject malicious SQL.

    You basically have two options to achieve this:

    1. Using PDO (for any supported database driver):

      $stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name');
      $stmt->execute(array('name' => $name));
      foreach ($stmt as $row) {
            // do something with $row  
      }
    2. Using MySQLi (for MySQL):

      $stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?');  
      $stmt->bind_param('s', $name); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { // do something with $row }
      If you're connecting to a database other than MySQL, there is a driver-specific second option that you can refer to (e.g. pg_prepare() and pg_execute() for PostgreSQL). PDO is the universal option.
  13. How to fix “Headers already sent” error in PHP ?

    No output before sending headers. there should not be any output (i.e. echo.. or HTML codes) before the header(.......);command. remove any white-space(or newline) before <?php and after ?> tags. After header(...); you must use exit;

  14. How to return JSON from a PHP Script ?

    We have to set the Content-Type header with application/json  as the value and print a valid JSON data using echo method

    For example

    <?php  
    $data = /** whatever you're serializing **/; header('Content-Type: application/json'); echo json_encode($data);
  15. How can we fix "Memory size Exhausted" errors ?

    You can fix it at run time or can be set required size on the php.ini file

    ini_set('memory_limit', '512M'); // In a php script at run time