preloader
PHP Interview Questions

Top PHP Interview Questions and Answers For 2022 Practice Now

author image

PHP is a very important language for development and is generally used in today’s world. So because of its popularity companies directly hire PHP developers. So if you are appearing for any such post then you need to well-prepare the interview from PHP Interview Questions and Answers. In this, we have covered most of the PHP interview questions for better preparation.

About PHP: PHP (PHP: Hypertext Preprocessor) is a very much used, open-source scripting language that is suited for web development. It is the first server-side language that can embed with HTML, making it smooth to add functionality to web pages with no need to call external files. Read on to learn PHP Interview Questions And Answers.

Also, prepare for Embedded C interview questions, Networking interview questions, and other language interview questions from here.

PHP Interview Questions

1. Explain PHP?

2. What does PHP stand for?

3. What does PEAR stand for?

4. Which PHP version is actually used in working?

5. Which command line is used to execute a PHP script?

6. How to run the PHP shell from the CLI?

7. What are the best two common ways to start and end a PHP block of code?

8. Tell the tags to directly display the output to the browser?

9. Tell the one most important difference between PHP 4 and PHP 5?

10. Does PHP support multiple inheritances?

11. Explain the final class and a final method?

12. In PHP how objects comparison is done?

13. Explain the type of operation required to pass values through a form or an URL?

14. Tell us how PHP and Javascript interact with each other?

15. What is required to use the image function?

16. Explain the use of ‘imagetypes()’ function?

17. Tell the functions required to get the image properties like size, width, and height?

18. How to handle failures in execution by using include() and require() functions?

19. Differentiate between require() and require_once()?

20. Show the way to display text with a PHP script?

21. In PHP, is it possible to set an infinite execution time?

22. Tell the use of file_get_contents() function?

23. Tell the way to connect a MySQL database from a PHP script?

24. Tell the use of mysql_pconnect() function?

25. How to handle the result set of Mysql in PHP?

26. Which function is used to remember the number of rows returned in the result set?

27. Tell the function that provides the number of affected entries by a SQL query?

28. Differentiate mysqli_fetch_object() and mysqli_fetch_array()?

29. Tell the way to access the data sent via URL with the GET method?

30. Tell the way to access the data sent via the URL with the POST method?

31. Tell the way to check the given variable value is a number?

32. Tell the way to check the given variable value is alphanumeric?

33. How do we come to know if the given variable is empty?

34. Explain the unlink() function?

35. Explain the unset() function?

36. Tell the way to escape data before storage into the database?

37. Tell the way to remove escape characters from a string?

38. Tell the way to automatically escape incoming data?

39. Explain get_magic_quotes_gpc() function?

40. How do we remove the HTML tags from data?

41. what is the use of a static variable in function?

42. Tell the way to return a value from a function?

43. Which cryptographic extension does verification of digital signatures?

44. How to define a constant in a PHP script?

45. How are you able to pass a variable by reference?

46. Do we compare an integer 12 and a string “13” in PHP?

47. How to cast types in PHP?

48. How to use the ternary conditional operator in PHP?

49. What is the use of func_num_args() function?

50. Define a session in PHP?


Learn More Interview Questions Here:


PHP Interview Questions For Freshers

1. Explain PHP?

PHP is a web-based language that depends on scripts that permit developers to dynamically make generated web pages.

2. What does PHP stand for?

PHP stands for PHP: Hypertext Preprocessor.

3. What does PEAR stand for?

PEAR full form is “PHP Extension and Application Repository”. It is an extended version of PHP that provides a higher level of programming for web developers.

4. Which PHP version is actually used in working?

PHP Version 7.1 or 7.2 is generally used in working.

5. Which command line is used to execute a PHP script?

You need to use the PHP command-line interface (CLI) and mention the file name of the PHP script to be executed. For example:

php script.php

6. How to run the PHP shell from the CLI?

You need to use the PHP command-line interface (CLI) program with option -a as given:

php -a

7. What are the best two common ways to start and end a PHP block of code?

The best two common ways to start and end a PHP script are:

<?php [ --- PHP code---- ] ?>

and

<? [--- PHP code ---] ?>

8. Tell the tags to directly display the output to the browser?

are the special tags we use to display the output directly to the browser

9. Tell the one most important difference between PHP 4 and PHP 5?

PHP 5 comes with many additional Object-Oriented Programming (OOP) features.

10. Does PHP support multiple inheritances?

No, PHP only supports single inheritance which means a class can be extended from one class by using the keyword ‘extended.

11. Explain the final class and a final method?

‘final’ was presented in PHP5. The final class means that this is the last class and cannot be further extended and a final method cannot be overridden.

12. In PHP how objects comparison is done?

Use the operator ‘==’ to check whether the two objects are instanced from the same class and possess the same attributes and have equal values or not. Use the identity operator ‘===’ to test if two objects are stating to the same instance of the same class or not.

13. Explain the type of operation required to pass values through a form or an URL?

We need to encode and decode the values using htmlspecialchars() and urlencode() to pass values through a form or an URL.

14. Tell us how PHP and Javascript interact with each other?

PHP and Javascript are not able to interact directly as PHP is a server-side language and different from Javascript is a client-side language. But, we can exchange variables as we know PHP can generate Javascript code to execute by the browser and to pass particular variables back to PHP through the URL.

15. What is required to use the image function?

GD library is required to use and run image functions.

16. Explain the use of ‘imagetypes()’ function?

imagetypes() provides the image format and types that present version of GD-PHP supports.

17. Tell the functions required to get the image properties like size, width, and height?

The functions to get the image properties of size, width and height are:

  • getimagesize() for size
  • imagesx() for width
  • imagesy() for height

18. How to handle failures in execution by using include() and require() functions?

If the require()function is not able to access the file then it finishes with a fatal error. However, the function include() provides a warning, and the PHP script carries on with execution.

19. Differentiate between require() and require_once()?

The same task is performed by require(), and require_once() but the only difference is the second function test whether the PHP script is already included before executing it or not.

20. Show the way to display text with a PHP script?

Two methods that are possible is:

<!--?php echo "Method 1"; print "Method 2"; ?-->

21. In PHP, is it possible to set an infinite execution time?

Yes, The set_time_limit(0) needs to be added at the start of a script, to set an infinite execution time to not face the PHP error ‘maximum execution time exceeded.’ It is also fine to mention this in the php.ini file.

22. Tell the use of file_get_contents() function?

file_get_contents() allows reading a file and saving it in a string variable.

Know the Interview Criteria of these MNCs!!!

PHP Interview Questions And Answers For Experienced

23. Tell the way to connect a MySQL database from a PHP script?

Use mysqli_connect() function to connect a MySQL database from a PHP script as follows:

<!--?php $database = mysqli_connect("HOST", "USER_NAME", "PASSWORD"); mysqli_select_db($database,"DATABASE_NAME"); ?-->

24. Tell the use of mysql_pconnect() function?

mysql_pconnect() males sure to create a persistent connection with the database which tells that the connection is not closed when the PHP script finishes. The given function is unsupported in PHP 7.0 and above.

25. How to handle the result set of Mysql in PHP?

By using mysqli_fetch_array, mysqli_fetch_assoc, mysqli_fetch_object or mysqli_fetch_row you can handle the result set of Mysql in PHP.

26. Which function is used to remember the number of rows returned in the result set?

The function mysqli_num_rows()is used to remember the number of rows returned in a result set.

27. Tell the function that provides the number of affected entries by a SQL query?

mysqli_affected_rows() brings back the number of affected entries by an SQL query.

28. Differentiate mysqli_fetch_object() and mysqli_fetch_array()?

The mysqli_fetch_object() function assembles the first single matching record whereas mysqli_fetch_array() pulls together all matching records from the table in an array.

29. Tell the way to access the data sent via URL with the GET method?

To access the data sent through URL by using the GET method, we use $_GET array, as shown:

For www.url.com?var=value

$variable = $_GET["var"]; this will now contain 'value'

30. Tell the way to access the data sent via the URL with the POST method?

To access the data sent through URL by using the POST method, we use the $_POST array.

Let assume, you have a form field called ‘var’, so, whenever the user presses submit to the post the form, the value you’ll get is something like this:

$_POST["var"];

31. Tell the way to check the given variable value is a number?

Use the is_numeric() function to test whether the value of a given variable is a number or not.

32. Tell the way to check the given variable value is alphanumeric?

Use the ctype_alnum function to check whether the value of a given variable is alphanumeric or not.

33. How do we come to know if the given variable is empty?

use the empty() function if you want to test whether a variable has a value or not.
The unlink() function is used for file system handling. It works is to delete the file given as an entry.

35. Explain the unset() function?

The unset() function is used for variable management. It makes an undefined variable.

36. Tell the way to escape data before storage into the database?

You need to enable the addslashes function to escape data before storing it in the database.

37. Tell the way to remove escape characters from a string?

You need to enable the stripslashes function to remove the escape characters before apostrophes in a string.

38. Tell the way to automatically escape incoming data?

You just need to activate the Magic quotes entry in the PHP configuration file.

39. Explain get_magic_quotes_gpc() function?

This function get_magic_quotes_gpc() just simply states whether the magic quote is active or not.

40. How do we remove the HTML tags from data?

You need to enable the strip_tags() function to remove a string from the HTML tags.

41. what is the use of a static variable in function?

It is defined only for the first time in a function, and its value can be changed during function calls, such as:

<!--?php function testFunction() { static $testVariable = 1; echo $testVariable; $testVariable++; } testFunction(); //1 testFunction(); //2 testFunction(); //3 ?-->

42. Tell the way to return a value from a function?

By using the command ‘return $value;’ a function returns a value.

43. Which cryptographic extension does verification of digital signatures?

The PHP-OpenSSL extension runs various cryptographic operations that include the generation and verification of digital signatures.

44. How to define a constant in a PHP script?

By using a command define() directive, we can define a constant in a PHP script which is as follows:

define ("ACONSTANT", 123);

45. How are you able to pass a variable by reference?

To pass a variable by reference, you just need to use an ampersand (&) in front of it, for example,

$var1 = &$var2

46. Do we compare an integer 12 and a string “13” in PHP?

Yes, “13” strings and 12 integers can be compared in PHP as it casts everything by using integer type.

47. How to cast types in PHP?

You need to specify the name of an output type in parentheses just before the variable which is to be cast. Cast types are:

  • (int), (integer) – cast to integer
  • (bool), (boolean) – cast to boolean
  • (float), (double), (real) – cast to float
  • (string) – cast to string
  • (array) – cast to an array
  • (object) – cast to object

48. How to use the ternary conditional operator in PHP?

It is made of three expressions: a condition, and two operands which define what instruction should be performed when the particular condition is true or false:

Expression_1?Expression_2 : Expression_3;

49. What is the use of func_num_args() function?

The func_num_args()function provides the number of passed parameters into a function.

50. Define a session in PHP?

A session is a logical object that allows us to save temporary data across different PHP pages.

It is one of the most important languages so we suggest you prepare well and crack the job interview. For more questions about different languages explore our website.


Want to prepare for these languages:

Recent Articles