PHP interview questions and answers

Home >> PHP interview questions and answers  >>

  PHP interview questions and answers part - 1

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 PHP?
    PHP is a server side scripting language commonly used for web applications.
    PHP stands for “Hypertext Preprocessor"
    PHP is a widely-used, open source scripting language.
    PHP scripts are executed on the server.
    PHP is free to download and use.

Question: What is the use of "echo" in php?
    It is used to print data to the screen.

Question: What is the use of "print" in php?
    It is used to print data to the screen.

Question: What is the difference between print and echo in PHP?
     echo and print are more or less the same. They are both used to output data to the screen.

    The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument.

So, echo without parentheses can take multiple parameters, which get concatenated
   echo  "php", 1, 2, "codeIgniter";   // Comma-separated without parentheses
   echo ("Laravel");  // Just one parameter with parentheses

print() can only take one parameter:
   print ("WordPress");
   print  "Magento";

Question: What is the use of header () function in PHP?
    1. Header is used to redirect from current page to another.    

header("Location: validate_user.php");

    2. Header is used to send HTTP status code.   

 header("HTTP/1.0 404 Not Found");

    3. Header is used to send Send a raw HTTP header   

header('Content-Type: application/pdf');

Question: What is the difference between the function unlink() and unset()?
    unlink(): It is used to remove the file from a given directory.    

unlink('/path/file-name');

    unset(): It is used to remove the variable.   

unset($variable_name);  

Question: How can we get the current session id?    

echo session_id();

Question: What are default session time and path?
    Session Time: It depends on the server configuration in php.ini and default time is 24 minutes (1440 seconds).
    Session Path: /temp folder on server
    
Question: What is PEAR?
    PHP Extension and Application Repository (PEAR) is a framework and repository for reusable PHP components.

Question: What is MIME?
    The full form of MIME is "Multipurpose Internet Mail Extensions".
    It is an extension of e-mail protocol helps to exchange the different kinds of data files over the internet. Data files may be images, text, audio, video, pdf, doc etc.
    
Question: How can I execute PHP File using Command Line?
   Open command line interface and get login into it, if required and then You have to prepend the "PHP" to mention the absolute/relative path of the file. Execute the file in the following way.   

 php myfile.php 

Question: What are different type of sorting functions in PHP?
    sort() - sort arrays in ascending order. 
    rsort() - sort arrays in descending order.
    
    asort() - sort associative arrays in ascending order, according to the value.
    ksort() - sort associative arrays in ascending order, according to the key.
    arsort() - sort associative arrays in descending order, according to the value.
    krsort() - sort associative arrays in descending order, according to the key.
    
    array_multisort() - sort the multi dimension array.
    usort()- Sort the array using user defined function.

Question: How to include a file to a php page?
    We can include a file using "include()" or "require()" function with file path as its parameter.

Question: What's the difference between include and require?
    If the file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue.

Question: What is difference between require_once(), require(), include()
    require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page). This way you make sure you don't include the file more times and you will not get the "function re-declared" error.

Question: Differences between GET and POST methods?
    We can send 1024 bytes using GET method but POST method can transfer large amount of data and POST is the secure method than GET method.

Advantages and Disadvantages of Using the GET Method

  • Since the data sent by the GET method are displayed in the URL, it is possible to bookmark the page with specific query string values.
  • The GET method is not suitable for passing sensitive information such as the username and password, because these are fully visible in the URL query string as well as potentially stored in the client browser's memory as a visited page.
  • Because the GET method assigns data to a server environment variable, the length of the URL is limited. So, there is a limitation for the total data to be sent.

Advantages and Disadvantages of Using the POST Method

  • It is more secure than GET because user-entered information is never visible in the URL query string or in the server logs.
  • There is a much larger limit on the amount of data that can be passed and one can send text data as well as binary data (uploading a file) using POST.
  • Since the data sent by the POST method is not visible in the URL, so it is not possible to bookmark the page with specific query.

Question: How to declare an array in php?

 $arr = array('red', 'green', 'black');

Question: What is use of count() function in php?
    count() is used to count all elements in an array, or something in an object

Question: What is the difference between Session and Cookie?

  • The main difference between sessions and cookies is that sessions are stored on the server, and cookies are stored on the user’s computers in the text file format.
  • Cookies cannot hold multiple variables; But Session can hold multiple variables. We can set expiry for a cookie, The session only remains active as long as the browser is open.
  • Users do not have access to the data you stored in Session, since it is stored in the server.
  • Session is mainly used for login/logout purpose while cookies using for user activity tracking.

Question: What types of loops exist in php?
    for, foreach, while and do while

Write your comment now