PHP interview questions and answers

In this tutorial we are providing you some frequently asked PHP Interview Questions which will help you to get success in interview session easily. PHP is an important part of the web world, and every web developer should have the basic knowledge in PHP. Common PHP interview questions, which should help you become a best PHP developer.

  1. What is PHP?

    PHP is a server side scripting language commonly used for web applications. PHP is a web language based on scripts that allows developers to dynamically create generated web pages.

  2. What does the initials of PHP stand for?

    PHP means PHP: Hypertext Preprocessor.

  3. Which programming language does PHP resemble to?

    PHP syntax resembles Perl and C.

  4. What is the use of “echo” in php?

    It is used to print the data in the webpage, For example: <?php echo ‘PHP Interview’; ?> , The following code print the text in the webpage.

  5. 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.

  6. 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.
  7. What is difference between require_once(), require()?

    require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page).

    If you are including a file that have functions, it is recommended to use require_once(). By this way file will included only once and you will not get the “function re-declared” error.

  8. What is the difference between GET and POST method?

    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.

  9. What does PEAR stands for?

    PEAR means “PHP Extension and Application Repository”. It extends PHP and provides a higher level of programming for web developers.

  10. How do you execute a PHP script from the command line?

    Just use the PHP command line interface (CLI) and specify the file name of the script to be executed as follows:

    php filename.php
  11. What are the correct and the most two common way to start and finish a PHP block of code?

    The two most common ways to start and finish a PHP script are: <?php [- PHP code -] ?> and <? [- PHP code -] ?>.

  12. How can we display the output directly to the browser?

    We have to use the special tags <?= and ?> to display the output directly to the browser.

  13. What is the main difference between PHP 4 and PHP 5?

    PHP 5 presents many additional OOP (Object Oriented Programming) features.

  14. Is multiple inheritance supported in PHP?

    PHP includes only single inheritance, it means that a class can be extended from only one single class using the keyword “extended”.

  15. How to declare an array in php?
     $arr = array('first', 'second', 'third');
  16. What is the use of “print” in php?

    print is not actually a real function (it is a language construct) so you are not required to use parentheses with its argument list.

    print("Hello World"); 
    print "without parentheses."; 
  17. What is use of in_array() function in php?

    in_array used to check if a value exists in an array.

  18. What is use of count() function in php?

    count() is used to count all elements in an array, or something in an object.

  19. 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 can’t hold multiple variable while 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.

  20. What is the meaning of a final class and a final method?

    “final” is introduced in PHP5. Final class means that this class cannot be extended and a final method cannot be overrided.

  21. How comparison of objects is done in PHP5?

    We use the operator “==” to test is two object are instanced from the same class and have same attributes and equal values. We can test if two object are referring to the same instance of the same class by the use of the identity operator “===”.

  22. How can PHP and HTML interact?

    It is possible to generate HTML through PHP scripts, and it is possible to pass information from HTML to PHP.

  23. What type of operation is needed when passing values through a form or an URL?

    If we would like to pass values through a form or an URL then we need to encode and to decode them using htmlspecialchars() and urlencode().

  24. How can PHP and JavaScript interact?

    PHP and JavaScript cannot directly interacts since PHP is a server side language and JavaScript is a client side language. However we can exchange variables since PHP is able to generate JavaScript code to be executed by the browser and it is possible to pass specific variables back to PHP via the URL.

  25. What is needed to be able to use image function?

    GD library is needed to be able execute image functions.

  26. What are the functions to be used to get the image’s properties (size, width and height)?

    The functions are getimagesize() for size, imagesx() for width and imagesy() for height.

  27. How can we display information of a variable and readable by human with PHP?

    To be able to display a human-readable result we use print_r().

  28. How is it possible to set an infinite execution time for PHP script?

    The set_time_limit(0) added at the beginning of a script sets to infinite the time of execution to not have the PHP error “maximum execution time exceeded”. It is also possible to specify this in the php.ini file.

  29. What does the PHP error “Parse error in PHP – unexpected T_variable at line x” means?

    This is a PHP syntax error expressing that a mistake at the line x stops parsing and executing the program.

  30. How to set cookies in PHP?
    setcookie("var", "value", time()+3600);
  31. How to Retrieve a Cookie Value?
     echo $_COOKIE["var"]; 
  32. How to create a session? How to set a value in session ? How to Remove data from a session?
    Create session : session_start();
    Set value into session : $_SESSION["ID"]=10;
    Remove data from a session : unset($_SESSION["ID"];
  33. What types of loops exist in php?

    for, while, do while and foreach.

Read PHP interview questions and answers Set – 2