Top 20 PHP Interview Questions and Answers

This post covers Top 20 PHP Interview Questions Asked to freshers and experienced. If you are a PHP Programmer, can you answer these questions?

Let’s start with the Basics and then we will move to the tough questions asked in the php interviews.

  1. What is PHP?

    Sort Answer – PHP (Hypertext Processor) is a server side scripting language commonly used for web applications.

    Long Answer – PHP : Hypertext Processor is open source server side scripting language that is widely used for web development. PHP allows writing dynamically generate web pages efficiently and quickly.It is also an object oriented programming language and its syntax is mostly borrowed from C, Java, Perl. PHP is Free to download and ease to use.

  2. Explain the differnce between $message vs $$message?

    Answer – $message is a variable with a fixed name while $$message is a variable whose name is stored in $message.
    For Example: If $message contains “mct”,$$message will be same as $mct.

  3. Explain the different type of errors?

    There are three type of errors: Notices,Warnings and Fatal errors.

    • Notices:

      Notices represent non-critical errors, i.e. accessing a variable that has not yet been defined. By Default, these errors are not displayed to users at all, but you can change this default behaviour.

    • Warnings:

      Warnings are serious errors, but they don’t result in script termination, i.e calling include() function with file as a parameter but the file doesn’t exit.By Default, these errors are displayed to users.

    • Fatal Errors:

      Fatal Errors are critical errors i.e calling a non-existent function or class. They result in immediate termination of script, i.e calling a require() function with a file as a parameter but the file doesn’t exist. By Default these errors are displayed to users.

  4. How to include a file in php page?

    Answer – We can include a file in php using require() or include() function.

  5. What is the difference between include(), require(), and require_once() ?

    • include() and require() :include() and require() will include the file specified as a parameter.The difference between them are the way the handle the failures. include() produces a warning while require() produces a fatal error.
    • require_once() : require_once() include and execute the file if the code of the file has not been included before.

    Summarised Answer – require() and include() are the almost similar except the way they handle failures. include() produces a Warning while require() produces a fatal error.require_once() includes the file i the file has not been included before.

  6. Differentiate between echo Vs. print statement?

    echo() and print() both are used to output strings.Their speed are almost same.
    echo() can take multiple expressions while print() cannot take multiple expressions.echo has no return value while print has a return value of true or false so it can be used in expressions.

  7. Differentiate between GET and POST methods?

    We can send 1024 bytes using GET method but POST method can transfer large amount of data. POST is the secure method than GET method.

  8. What is the Difference between Session and Cookie?

    The main difference between Session and cookie is that session stores data on sever while cookie stores data on users local computer.Cookie cannot hold multiple variables while session can hold multiple variables.

    We can set expiry for cookie but the session remained active till the browser is open.

    Users don’t have access to session whereas they have access to cookies.

  9. How to set and retrieve cookie in PHP?

    We can set cookie using the cookie syntax.

    For Example:

    <?php
    $value = 'Your Cookie Value';
    setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */
    ?>
    

    and to retrieve our TestCookie we will use

    echo $_COOKIE['TestCookie'];

  10. What is urlencode and urldecode?

    urlencode can be used to encode a string that can be used in a url. It encodes the data same way posted data from web page is encoded. It returns the encoded string.
    Syntax :

    urlencode (string $str )

    urldecode() is the function that is used to decode the encoded string.

    urldecode(string $str)
  11. What does PEAR stand for in PHP?

    PEAR stand for PHP Extension and Application Repository. It is a framework and repository for reusable PHP Components.

  12. What is the difference between “==” and “===” operator?

    “==” compares the value of a variable for equality.It’s not case sensitive while “===” is case sensitive.”===” will return true if both operands have same type and same value.

  13. How to submit form without a Submit button?

    We can achieve this task by using JavaScript. We will link JavaScript code linked to an event trigger of any form field and call the document.form.submit() function.

  14. Differentiate between unlink Vs unset?

    unlink is a function for file system handling which deletes a file.unset function is used to destroy a variable.

  15. What is difference between split and explode?

    Both the functions are used to split a string.But split is used to split a string using regular expression while explode is used to split a string using another string.

    $string = "India Vs South Africa";
    
    $explode = explode("Vs",$string); returns an array Contains India, South Africa
    
    $split = split("/Vs/",$string); return same as Above
    
  16. What is Type Juggling in PHP?

    Type Juggling is dealing with variables.In other programming languages declaring variables requires variable’s data type and name of that variable and then variable will be initialized.

    But, in PHP there is no need of any type of specification while php variable declaration.

  17. What is Zend Engine?

    Zend Engine is used internally by PHP as a compiler and runtime engine.Its reliability, performance and extensibility has a significant role in increasing the PHP’s popularity.The Zend Engine II is the heart of PHP 5. It is an open source project and freely available under BSD style license.

  18. What is CAPTCHA?

    CAPTCHA is a test to determine whether the user using the System is a Human or Robot. Captcha are images with some characters/digits on it.One need to type the character or digits in the text box for the purpose of submitting.

  19. Explain How to Execute a PHP Script from command line?

    Just Use the PHP CLI(Command Line Interface) and specify the file name of the php script to be executed.

    php myscript.php
    
  20. What is the main difference between PHP 4 and PHP 5?

    PHP 5 presents many additional OOP(Object Oriented Programming) features.
    For example:
    In PHP4, Constructor have same name as the Class name.
    In PHP5, name Constructors as _construct and Destructors as _destruct().

Shubham Kumar

Hey, I am Shubham and i love Blogging, Coding and exploring new things and obviously sharing my experience with you.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *