Interviews Commonly asked PHP interview questions

Commonly asked PHP interview questions

Commonly asked PHP interview questions

In this article, we’ve provided selected PHP interview questions and answers for freshers going to appear for employment interview as a PHP developer. These PHP interview questions are picked for you to help you in the preparations of your interview for PHP Programming Language.

1. How can we identify the number of days between two given dates in PHP?

Simple arithmetic:

2. What is a persistent cookie?

A persistent cookie is a cookie which is stored in a cookie file permanently on the browser’s computer. By default, cookies are created as temporary cookies which are stored only in the browser’s memory. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use persistent cookies based on their differences:

  • Temporary cookies cannot be used for tracking long-term information unlike Persistent cookies, which are used for tracking long-term information.
  • Temporary cookies are safer because no programs other than the browser can access them.
  • Persistent cookies are less secure because users can open cookie files see the cookie values.

3. How to define a constant?

Using define() directive, like define (“MY_CONSTANT”,  ‘9999’);

4. What is meant by Urlencode and Urldecode?

urlencode() method returns the encoded version of the given URL string. It converts special characters into ‘%’ signs followed by two hex digits and spaces into ‘+’ and so on.

For example: urlencode(“10.00%“) will return “10%2E00%25“. URL encoded strings are safe to be used as part of URLs.
urldecode() decodes the given encoded URL string. That means it will turn “10%2E00%25” back to “10.00%” form.

5. What is the functionality of the functions strstr() and stristr()?

strstr() and stristr() both are PHP string functions.

strstr (string haystack, string needle)  returns the part of first parameter string from the first occurrence of the second parameter string till the end 

strstr ( string haystack, string needle ) returns part of haystack string from the first occurrence of needle to the end of haystack. This function is case-sensitive.
stristr() is idential to strstr() except that it is case insensitive.

6. If the variable $a is equal to 5 and variable $b is equal to character a, what will be the value of $$b?

$$b = 5.  $$b is a reference to existing variable $b which is equal to a. So $$b = $a = 5

7. Are objects passed by value or by reference in PHP?

Everything in PHP is passed by value.

8. How will you call a constructor for a parent class?

parent::constructor($value).

9. What’s the special meaning of __sleep() and __wakeup() ?

__sleep() method returns the array of all the variables that need to be saved, whereas __wakeup() method is used to  retrieve them. Both are magic methods of PHP.

10. What is the difference between the functions unlink and unset?

  • unlink() is a function for file system handling. It will simply delete the file in present in the path provided as parameter. Eg: unlink(‘D://myfolder/myfile.txt’) will delete myfile.txt’.
  • unset() is a function for variable management. It will make a variable undefined.

11. How to extract second sentence of a paragraph in PHP?

12. What is “Magic Constant” in PHP

Magic constants are predefined constant by PHP. These constants are resolved at compile time, unlike regular constants, which are resolved at runtime.

Eg: __LINE__ , __FILE__, __DIR__ etc.

Check PHP articles, definitely going to help you answer PHP interview questions.

13. What is alternative syntax for control structure “if”

The alternative syntax for if statement is:

14. How will you check your version of PHP through code?

To check the version of PHP we can use phpversion function. phpversion() function returns the current PHP version.
Eg: echo phpversion();

15. What is the maximum limit for $_GET?

$_GET can handle a maximum of 2048 characters which are passed in query string.

16. How can you identify user’s IP address in PHP?

To identify a user’s/ ISP IP address in PHP we can use predefined variables $_SERVER. $_SERVER is an array that contains information about server and execution environment.

17. WHAT is HTTP_USER_AGENT index stands for?

$_SERVER[“HTTP_USER_AGENT”] returns information about User-Agent. It contains information of header from the current request, if there is one about browser. Like- user platform, User browser etc.

Eg: Mozilla/5.0 (Windows NT 6.1; Win64; x64)

18. Do you know about session_encode() function?

session_encode() is a PHP session function which returns a serialized string of all the contents of the current session data stored in the $_SESSION superglobal variable. Function session_start() must be called before using session_encode().

Eg: Taking session

echo session_encode(); will print user_id|i:1234;user_name|s:12:”TechBriefers”;

19. What is the difference between unset() and session_unset() ?

session_unset() function removes/ frees all session variables currently registered.

unset() function unset/removes/frees one or more variable passed as parameter to the function.

We have some more interview questions and answers for you. Click here to explore more.

Leave a Reply