PHP Interview Questions and Answers Set – 4

In this tutorial we are providing some set of frequently asked PHP Interview Questions for experienced candidates which will help you to get success in interview. Read PHP MySQL Interview Questions and Answers Set – 3.

  1. How to find current date and time?

    The date() function provides you with a means of retrieving the current date and time, applying the format integer parameters indicated in your script to the timestamp provided or the current local time if no timestamp is given. In simplified terms, passing a time parameter is optional – if you don’t, the current timestamp will be used.

  2. What is the static variable in function useful for?

    A static variable is defined within a function only the first time and its value can be modified during function calls as follows:

    function testStatic() { 
    	static $testVar = 1; 
    	echo $testVar; 
    testStatic(); //1
    testStatic(); //2
    testStatic(); //3       
  3. How can we define a variable accessible in functions of a PHP script?

    This feature is possible using the global keyword.

  4. How is it possible to return a value from a function?

    A function returns a value using the instruction “return $value;”.

  5. What is the most convenient hashing method to be used to hash passwords?

    It is preferable to use crypt() which natively supports several hashing algorithms or the function hash() which supports more variants than crypt() rather than using the common hashing algorithms such as md5, sha1 or sha256 because they are conceived to be fast. hence, hashing passwords with these algorithms can vulnerability.

  6. How can you pass a variable by reference?

    To be able to pass a variable by reference, we use an ampersand in front of it, as follows $var1 = &$var2

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

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

  9. Will a comparison of an integer 12 and a string “13” work in PHP?

    “13” and 12 can be compared in PHP since it casts everything to the integer type.

  10. How is it possible to cast types in PHP?

    The name of the output type have to be specified in parentheses before the variable which is to be cast as follows:

    * (int), (integer) – cast to integer
    * (bool), (boolean) – cast to boolean
    * (float), (double), (real) – cast to float
    * (string) – cast to string
    * (array) – cast to array
    * (object) – cast to object
  11. When a conditional statement is ended with an endif?

    When the original if was followed by : and then the code block without braces.

  12. What is the use of the function htmlentities?

    htmlentities Convert all applicable characters to HTML entities This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.

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

  14. 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.
    For instance :

    <input name="animals[]" id="cat" />
    <input name="animals[]" id="rat" />
    <input name="animals[]" id="lion" />
  15. How is the ternary conditional operator used in PHP?

    It is composed of three expressions: a condition, and two operands describing what instruction should be performed when the specified condition is true or false as follows:

    expression ? expression 2 : expression 3;
  16. What is the function func_num_args() used for?

    The function func_num_args() is used to give the number of parameters passed into a function.

  17. If the variable $var1 is set to 10 and the $var2 is set to the character var1, what’s the value of $$var2?

    $$var2 contains the value 10.

  18. What is the definition of a session?

    A session is a logical object enabling us to preserve temporary data across multiple PHP pages.

  19. How to initiate a session in PHP?

    The use of the function session_start() lets us activating a session.

  20. How is it possible to propagate a session id?

    It is possible to propagate a session id via cookies or URL parameters.

  21. What is the meaning of a Persistent Cookie?

    A persistent cookie is permanently stored in a cookie file on the browser’s computer. By default, cookies are temporary and are erased if we close the browser.

  22. When sessions ends?

    Sessions automatically ends when the PHP script finishes executing, but can be manually ended using the session_write_close().

  23. What is the difference between session_unregister() and session_unset()?

    The session_unregister() function unregister a global variable from the current session and the session_unset() function free all session variables.

  24. What does $GLOBALS means?

    $GLOBALS is associative array including references to all variables which are currently defined in the global scope of the script.

  25. What does $_SERVER means?

    $_SERVER is an array including information created by the web server such as paths, headers, and script locations.

  26. What does $_COOKIE means?

    $_COOKIE is an associative array of variables sent to the current PHP script using the HTTP Cookies.

  27. What does accessing a class via :: means?

    :: is used to access static methods that do not require object initialization.

  28. Are Parent constructors called implicitly inside a class constructor?

    No, a parent constructor have to be called explicitly as follows:


  29. What’s the difference between __sleep and __wakeup?

    __sleep returns the array of all the variables that need to be saved, while __wakeup retrieves them.

  30. What is faster?

    1: Combining two variables as follows:

    $variable1 = 'Hello ';
    $variable2 = 'World';
    $variable3 = $variable1.$variable2;



    $variable3 = "$variable1$variable2";

    $variable3 will contain “Hello World”. The first code is faster than the second code especially for large large sets of data.

Read Next tutorial PHP Interview Questions and Answers Set – 5