PHP interview questions and answers

Home >> PHP interview questions and answers  >>

  PHP interview questions and answers part - 2

This article will give you an overview of PHP basic and intermediate level 20 questions and answers that may be helpful for you in an interview or online test.

Question: What is the use of explode() function ?
    The explode() function breaks a string into an array.

Question: What is the difference between explode() and split() functions?
    The explode() function splits the string by string.
    The split() is deprecated and will be removed in the future.

 Example :
    $explode_array=explode('and','PHP and Laravel and CodeIgniter');

 Output Will be :
  [0] => PHP
  [1] => Laravel
  [2] => CodeIgniter
  [0] => PHP 
  [1] =>  Laravel 
  [2] =>  CodeIgniter

Question: What is the use of mysql_real_escape_string() function?    
    It is used to escapes special characters in a string for use in an SQL statement
Question: Write down the code for save an uploaded file in php.

 if ($_FILES["file"]["error"] == 0)
  move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; 

Question: How to create a text file in php?

 $file_name = "test_file.txt";
 $file = fopen($file_name, "w");
 if($file == false)
  echo ("Error in opening new file" ); 

 fwrite($file, "This is a simple test\n" );
 fclose($file );

Question: How to strip whitespace (or other characters) from the beginning and end of a string ?
    The trim() function removes whitespaces or other predefined characters from both sides of a string.    
Question: How stop the execution of a php scrip ?
    exit() function is used to stop the execution of a page.

Question: How to set a page as a home page in a php based site ?
    index.php is the default name of the home page in php based sites.

Question: How to find the length of a string?
    strlen() function used to find the length of a string.
Question: What is the use of rand() in php?
    It is used to generate random numbers.
Question: What is the use of isset() in php?
    This function is used to determine if a variable is set and is not NULL    
Question: What is mean by an associative array?
    Associative arrays are arrays that use string keys is called associative arrays.    
Question: What is the importance of "method" attribute in a html form?
    "method" attribute determines how to send the form-data into the server.There are two methods, get and post. 

    The default method is get. This sends the form information by appending it on the URL.Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

Question: What is the importance of "action" attribute in a html form?
    The action attribute determines where to send the form-data in the form submission.

Question: What is the use of "enctype" attribute in a html form?
    The enctype attribute determines how the form-data should be encoded when submitting it to the server. We need to set enctype as "multipart/form-data" when we are using a form for uploading files.

Question: How to create an array of a group of items inside an HTML form ?
    We can create input fields with same name for "name" attribute with squire bracket at the end of the name of the name attribute, It passes data as an array to PHP.

Example :
 <input name="user_id[]" />  
 <input name="user_id[]" />  
 <input name="user_id[]" />  
 <input name="user_id[]" /> 

Question: How do you define a constant?
     Using define() directive, like define ("YOUR_CONSTANT_NAME","YOUR_CONSTANT_VALUE");

Example :

Output will be :
echo ENC_KEY; // PHPCODEHUB@#123

Question: How send email using php?
    To send email using PHP, you use the mail() function.
    This mail() function accepts 5 parameters as follows (the last 2 are optional). 
    You need webserver, you can't send email from localhost. 

Example: mail($to, $subject, $message,$headers);

Question: How to find current date and time?
    The date('date-format') function provides you current date and time.

 echo date("d-m-Y h:i:s");
 Output will be:
 06-07-2016 04:58:05

Question: Difference between mysql_connect and mysql_pconnect?
    mysql_connect() and mysql_pconnect() both are working for database connection but with little difference.

    When we are using mysql_connect() function, every time it is opening and closing the database connection, depending on the request .

    But in case of mysql_pconnect() function, First, when connecting, the function would try to find a (persistent) connection that’s already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

Write your comment now