PHP Interview Question Answers

  1. What is PHP?
    PHP
    is a robust, server-side, open source scripting language.PHP is also cross platform, which means you can execute PHP scripts on Unix, Linux, or an NT server.
    It is easy to learn and widely used web technologies.
  2. What’s the difference between include and require?
    Both functions are used for file inclusion but differ how they handle the exception.If file not found in case of include() a warning will be issued but execution will continue. while in case of require() fatal error issued and terminate the execution.
  3. Differences between GET and POST methods ?
    Both are used to send request to the server.In case of GET request only headers will be sent to the servers.Headers length of TCP/IP packet is fixed so we can’t send large amount of data by this method.Only 1024 bytes data can be transferred.But in case of POST method,the values are sent in the request body so can we transferred large amount of data by this method.GET is insecure as data is transferred through headers and can be seen by url of the request but POST is secure as data is not shown in url.
  4. How to declare an array in php?

     
    <?php 
    $array = array('Ram', 'Aman', 'Sohan');
     //or $array = array();
     $array[0]="Ram"; 
    $array[1]="Aman";
     $array[2]="Sohan"; 
    ?>
    
  5. What is the main difference between PHP 4 and PHP 5?

    In PHP4, Constructor have same name as the Class name but in PHP 5 funtion named __construct is introduced.

  6. What is use of in_array() function in php ?
    in_array() is used to find out that a value exists in an array or not.It return true if found otherwise false.
  7. How to define a cookie in PHP?
    Setcookie(“cookie_name”, “cookie_value”, expired_time_in_milliseconds);
    eg.
    Setcookie(“user_name”, “ram”, time()+3600);
    Here expired will be for an hour.
  8. What is final class and a final method?
    ‘final’ is introduced in PHP5. final class cannot be extended and a final method cannot be overridden.

     
    <?php 
    final class A{ 
    public void display(){
      echo "Hello,How are you?"; 
    } 
    }
     //You can't do the following. 
    class B extends A{} 
    ?>
    
  9. How to connection Mysql with PHP?
    mysql_connect(servername,username,password);
    e.g.
    mysql_connection(“localhost”,”root”,”1234″);
  10. What is of mysql_real_escape_string() function?
    It is used to escapes special characters in a string for use in an SQL statement.It prevents from SQL Injection.
  11. Different types of errors in PHP.
    Notices, Warnings and Fatal errors are the types of errors in PHP.
  12. What function do we use to find length of string, and length of array.
    For finding length of string we use strlen() function and for array we use count() function.
  13. What is meant by nl2br().
    Inserts HTML line breaks before all newlines in a string.
  14. What function do we use to find length of string, and length of array.
    For finding length of string we use strlen() function and for array we use count() function.
  15. What is the difference between unset() and unlink() function.
    unset() is used to destroy a variable where as unlink() is used to delete a file.
  16. What is an associative array?
    Associative arrays are arrays that use named keys that you assign to them.

    <?php
     $countries = array();
    $countries['IN']="India";
    $countries['US']="United States of America";
    $countries['UK']="United kingdom"; ?>
    
  17. How can we submit a form without using submit buttons?
    You can use javascript or jQuery to do the same.

    jQuery("#formId").submit();
    document.getElementbyId("formId").submit();
    
  18. Difference between echo() and print()
    echo() and print() are language constructs in PHP, both are used to output strings. echo() can take multiple expressions whereas print cannot take multiple expressions.
    Print return true or false based on success or failure whereas echo doesn’t return true or false.
  19. What is the main difference between require() and require_once()
    require() and require_once() perform the same task except that the require_once function checks if the PHP script is already included or not before executing it.
  20. How stop the execution of a php script?
    exit() and die() functions are used to stop the execution of PHP script at certain point.
  21. What is the difference between mysql_fetch_array() and mysql_fetch_assoc() ?
    mysql_fetch_assoc function Fetch a result row as an associative array, While mysql_fetch_array() fetches an associative array, a numeric array, or both.
  22. Difference between mysql_connect() and mysql_pconnect()
    mysql_connect() and mysql_pconnect() both are working for database connection but with little difference.While using mysql_connect() function, every time it is opening and closing the database connection, depending on the request .While mysql_pconnect() is used, the function initially tries to find an open persistent connection. If found; the identifier is returned. In this case, a new connection is not established. On the other hand, when mysql_connect() is used, a new connection is established.
    <?php
    session_register($ur_session_var);
    ?>
  23. How to find third largest salary in mysql?

     SELECT salary from Employee order by salary desc limit 2,1
    
  24. What is current version of Apache,PHP and Mysql?
  25. How to find the sum of digits of number?Example if number is 213 then sum is 6.
    <?php
    $num=213;
    $sum=0;
    while($num>0){
    $rem = $num%10;
    $sum += $rem;
    $num= $num/10;
    }
    echo "Sum:".$sum;
    ?>
    
  26. How to find the sum of array values?
    <?php
    $array = array(10,20,30,40,50);
    $sum  =0;
    for($i=0;$i<count($array);$i++){
    $sum +=$array[$i];
    }
    echo "Sum of Array Elements:"+$sum;
    ?>
    
  27. How to upload file using Ajax?
    Ajax File Upload

Leave a Reply