Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

technoknol/php-interview-questions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

3 Commits

Repository files navigation

Top 82 PHP interview questions and answers in 2021.

You can check all 82 PHP interview questions here πŸ‘‰ https://devinterview.io/dev/php-interview-questions


πŸ”Ή 1. What is the use of ini_set()?

Answer:

PHP allows the user to modify some of its settings mentioned in php.ini using ini_set(). This function requires two string arguments. First one is the name of the setting to be modified and the second one is the new value to be assigned to it.

Given line of code will enable the display_error setting for the script if it’s disabled.

ini_set('display_errors', '1');

We need to put the above statement, at the top of the script so that, the setting remains enabled till the end. Also, the values set via ini_set() are applicable, only to the current script. Thereafter, PHP will start using the original values from php.ini.



πŸ”Ή 2. What is the difference between == and ===?

Answer:

  • The operator == casts between two different types if they are different
  • The === operator performs a 'typesafe comparison'

That means that it will only return true if both operands have the same type and the same value.

1 === 1: true
1 == 1: true
1 === "1": false // 1 is an integer, "1" is a string
1 == "1": true // "1" gets casted to an integer, which is 1
"foo" === "foo": true // both operands are strings and have the same value


πŸ”Ή 3. What is the return type of a function that doesn't return anything?

Answer:

void which mean nothing.



πŸ”Ή 4. What does $GLOBALS mean?

Answer:

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

Source: guru99.com


πŸ”Ή 5. What are the keys & values in an indexed array?

Answer:

Consider:

Array ( [0] => Hello [1] => world [2] => It's [3] => a [4] => beautiful [5] => day)

The keys of an indexed array are 0, 1, 2 etc. (the index values) and values are "Hello", "world", "It's", "beautiful", "day".



πŸ”Ή 6. What is the purpose of php.ini file?

Answer:

The PHP configuration file, php.ini, is the final and most immediate way to affect PHP's functionality. The php.ini file is read each time PHP is initialized.in other words, whenever httpd is restarted for the module version or with each script execution for the CGI version.



πŸ”Ή 7. How can you pass a variable by reference?

Answer:

To be able to pass a variable by reference, we use an ampersand in front of it, as follows:

$var1 = &$var2
Source: guru99.com


πŸ”Ή 8. Is multiple inheritance supported in PHP?

Answer:

PHP supports only single inheritance; it means that a class can be extended from only one single class using the keyword 'extended'.

Source: guru99.com


πŸ”Ή 9. What is stdClass in PHP?

Answer:

stdClass is just a generic 'empty' class that's used when casting other types to objects. stdClass is not the base class for objects in PHP. This can be demonstrated fairly easily:

class Foo{}
$foo = new Foo();
echo ($foo instanceof stdClass)?'Y':'N'; // outputs 'N'

It is useful for anonymous objects, dynamic properties, etc.

An easy way to consider the StdClass is as an alternative to associative array. See this example below that shows how json_decode() allows to get an StdClass instance or an associative array. Also but not shown in this example, SoapClient::__soapCall returns an StdClass instance.

//Example with StdClass
$json = '{ "foo": "bar", "number": 42 }';
$stdInstance = json_decode($json);

echo $stdInstance - > foo.PHP_EOL; //"bar" echo $stdInstance - > number.PHP_EOL; //42

//Example with associative array $array = json_decode($json, true);

echo $array['foo'].PHP_EOL; //"bar" echo $array['number'].PHP_EOL; //42



πŸ”Ή 10. In PHP, objects are they passed by value or by reference?

Answer:

In PHP, objects passed by value.

Source: guru99.com


πŸ”Ή 11. What is PDO in PHP?

Answer:

PDO stands for PHP Data Object.

It is a set of PHP extensions that provide a core PDO class and database, specific drivers. It provides a vendor-neutral, lightweight, data-access abstraction layer. Thus, no matter what database we use, the function to issue queries and fetch data will be same. It focuses on data access abstraction rather than database abstraction.



πŸ”Ή 12. Is there a difference between isset and !empty?

Answer:

empty is more or less shorthand for!isset($foo) || !$foo, and !empty is analogous to isset($foo) && $foo. empty is the same as !$foo, but doesn't throw warnings if the variable doesn't exist. That's the main point of this function: do a boolean comparison without worrying about the variable being set.



πŸ”Ή 13. Differentiate between echo and print()

Answer:

echo and print are more or less the same. They are both used to output data to the screen.

The differences are:

  • echo has no return value while print has a return value of 1 so it can be used in expressions.
  • echo can take multiple parameters (although such usage is rare) while print can take one argument.
  • echo is faster than print.


πŸ”Ή 14. What is the differences between $a != $b and $a !== $b?

Answer:

!= means inequality (TRUE if $a is not equal to $b) and !== means non-identity (TRUE if $a is not identical to $b).

Source: guru99.com


πŸ”Ή 15. What does the 'var' keyword mean in PHP?

Answer:

It's for declaring class member variables in PHP4, and is no longer needed. It will work in PHP5, but will raise an E_STRICT warning in PHP from version 5.0.0 up to version 5.1.2, as of when it was deprecated. Since PHP 5.3, var has been un-deprecated and is a synonym for 'public'.

Consider:

class foo {
var $x = 'y'; // or you can use public like...
public $x = 'y'; //this is also a class member variables.
function bar() {
}
}


πŸ”Ή 16. What do we mean by keys and values?

Answer:

In associative arrays, we can use named keys that you assign to them. There are two ways to create an associative array:

// first way -

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");`

// another method - $age['Peter'] = "35"; //Peter, Ben & Joe are keys $age['Ben'] = "37"; //35, 37 & 43 are values $age['Joe'] = "43";



πŸ”Ή 17. What are the differences between die() and exit() functions in PHP?

Answer:

There's no difference - they are the same. The only advantage of choosing die() over exit(), might be the time you spare on typing an extra letter.



πŸ”Ή 18. Explain what the different PHP errors are

Answer:

  • A notice is a non-critical error saying something went wrong in execution, something minor like an undefined variable.
  • A warning is given when a more critical error like if an include() command went to retrieve a non-existent file. In both this and the error above, the script would continue.
  • A fatal error would terminate the code. Failure to satisfy a require() would generate this type of error, for example.
Source: pangara.com


πŸ”Ή 19. Explain usage of enumerations in PHP

Answer:

PHP doesn't have native Enumerations. Depending upon use case, I would normally use something simple like the following:

abstract class DaysOfWeek
{
const Sunday = 0;
const Monday = 1;
// etc.
}

$today = DaysOfWeek::Sunday;

There is a native extension, too. SplEnum gives the ability to emulate and create enumeration objects natively in PHP.



πŸ”Ή 20. Explain how we handle exceptions in PHP?

Answer:

When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception". An exception can be thrown, and caught within PHP.

To handle exceptions, code may be surrounded in a try block. Each try must have at least one corresponding catch block. Multiple catch blocks can be used to catch different classes of exceptions. Exceptions can be thrown (or re-thrown) within a catch block.

Consider:

try {
print "this is our try block n";
throw new Exception();
} catch (Exception $e) {
print "something went wrong, caught yah! n";
} finally {
print "this part is always executed n";
}


πŸ”Ή 21. What are the main differences between const vs define

Answer:

The fundamental difference between const vs define is that const defines constants at compile time, whereas define defines them at run time.

const FOO = 'BAR';
define('FOO', 'BAR');

// but if (...) { const FOO = 'BAR'; // Invalid } if (...) { define('FOO', 'BAR'); // Valid }

Also until PHP 5.3, const could not be used in the global scope. You could only use this from within a class. This should be used when you want to set some kind of constant option or setting that pertains to that class. Or maybe you want to create some kind of enum. An example of good const usage is to get rid of magic numbers.

Define can be used for the same purpose, but it can only be used in the global scope. It should only be used for global settings that affect the entire application.

Unless you need any type of conditional or expressional definition, use consts instead of define()- simply for the sake of readability!



πŸ”Ή 22. What's the difference between isset() and array_key_exists()?

Answer:

  • array_key_exists will tell you if a key exists in an array and complains when $a does not exist.
  • isset will only return true if the key/variable exists and is not null. isset doesn't complain when $a does not exist.

Consider:

$a = array('key1' => 'Foo Bar', 'key2' => null);

isset($a['key1']); // true array_key_exists('key1', $a); // true

isset($a['key2']); // false array_key_exists('key2', $a); // true



πŸ”Ή 23. Can you extend a Final defined class?

Answer:

No, you cannot extend a Final defined class. A Final class or method declaration prevents child class or method overriding.

Source: codementor.io


πŸ”Ή 24. What are PSRs? Choose 1 and briefly describe it.

Answer:

PSRs are PHP Standards Recommendations that aim at standardising common aspects of PHP Development. An example of a PSR is PSR-2, which is a coding style guide.

Source: codementor.io


πŸ”Ή 25. How can you enable error reporting in PHP?

Answer:

Check if "display_errors" is equal "on" in the php.ini or declare "ini_set('display_errors', 1)" in your script.

Then, include "error_reporting(E_ALL)" in your code to display all types of error messages during the script execution.

Source: codementor.io


πŸ”Ή 26. When should I use require vs. include?

Answer:

The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

My suggestion is to just use require_once 99.9% of the time.

Using require or include instead implies that your code is not reusable elsewhere, i.e. that the scripts you're pulling in actually execute code instead of making available a class or some function libraries.



πŸ”Ή 27. What are the different scopes of variables?

Answer:

Variable scope is known as its boundary within which it can be visible or accessed from code. In other words, it is the context within which a variable is defined. There are only two scopes available in PHP namely local and global scopes.

  • Local variables (local scope)
  • Global variables (special global scope)
  • Static variables (local scope)
  • Function parameters (local scope) When a variable is accessed outside its scope it will cause PHP error undefined variable.


πŸ”Ή 28. What is the difference between var_dump() and print_r()?

Answer:

  • The var_dump function displays structured information about variables/expressions including its type and value. Arrays are explored recursively with values indented to show structure. It also shows which array values and object properties are references.

  • The print_r() displays information about a variable in a way that's readable by humans. array values will be presented in a format that shows keys and elements. Similar notation is used for objects.

Consider:

$obj = (object) array('qualitypoint', 'technologies', 'India');

var_dump($obj)will display below output in the screen:

object(stdClass)#1 (3) {
[0]=> string(12) "qualitypoint"
[1]=> string(12) "technologies"
[2]=> string(5) "India"
}

And, print_r($obj) will display below output in the screen.

stdClass Object (
[0] => qualitypoint
[1] => technologies
[2] => India
)


πŸ”Ή 29. What is the difference between single-quoted and double-quoted strings in PHP?

Answer:

  • Single quoted strings will display things almost completely "as is."
  • Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated.

Things get evaluated in double quotes but not in single:

$s = "dollars";
echo 'This costs a lot of $s.'; // This costs a lot of $s.
echo "This costs a lot of $s."; // This costs a lot of dollars.


πŸ”Ή 30. How is it possible to set an infinite execution time for PHP script?

Answer:

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.

Source: guru99.com


πŸ”Ή 31. Give me some real life examples when you had to use __destruct in your classes

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 32. Declare some function with default parameter

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 33. PHP array delete by value (not key)

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 34. Is there a function to make a copy of a PHP array to another?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 35. Explain the difference between exec() vs system() vs passthru()?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 36. Are Parent constructors called implicitly inside a class constructor?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 37. Maximum how many arguments are allowed in a function in PHP?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 38. What is the difference between PDO's query() vs execute()?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 39. Explain the difference between shell_exec() and exec()

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 40. What is the difference between a PHP interpreter and a PHP handler?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 41. Differentiate between exception and error

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 42. What exactly is the the difference between array_map, array_walk and array_filter?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 43. What are the exception class functions?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 44. What are some of the big changes PHP has gone through in the past few years?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 45. Differentiate between parameterised and non parameterised functions

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 46. Why do we use extract()?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 47. Explain function call by reference

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 48. What is the difference between using self and $this?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 49. What is use of Null Coalesce Operator?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 50. Does PHP support method overloading?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 51. What is the difference between MySQL, MySQLi and PDO?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 52. What is autoloading classes in PHP?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 53. When should I use require_once vs. require?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 54. Is there any reason to use strcmp() for strings comparison?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 55. What will be returned by this code? Explain the result.

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 56. How do I pass variables and data from PHP to JavaScript?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 57. Let's create Enumerations for PHP. Prove some code examples.

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 58. How would you create a Singleton class using PHP?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 59. What does the following code output?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 60. What will be returned by this code?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 61. Check if PHP array is associative

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 62. Compare mysqli or PDO - what are the pros and cons?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 63. Store an array as JSON or as a PHP serialized array?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 64. What are the disadvantages of using persistent connection in PDO?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 65. What exactly are late static bindings in PHP?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 66. Explain the Exception Hierarchy introduced in PHP7

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 67. explain what is a closure in PHP and why does it use the "use" identifier?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 68. What is use of Spaceship Operator?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 69. What is the best method to merge two PHP objects?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 70. Is PHP single or multi threaded?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 71. How to turn errors into exceptions in PHP?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 72. What does $$ mean?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 73. What is the crucial difference between using traits versus interfaces?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 74. What does yield mean in PHP?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 75. What's better at freeing memory with PHP: unset() or $var = null?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 76. Does PHP have threading?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 77. Are PDO prepared statements sufficient to prevent SQL injection?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 78. Explain the Order of Precedence for Traits in PHP

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 79. What does a $$$ mean in PHP?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 80. How to measure execution times of PHP scripts?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 81. How could we implement method overloading in PHP?

πŸ‘‰πŸΌ Check all 82 answers


πŸ”Ή 82. Provide some ways to mimic multiple constructors in PHP

πŸ‘‰πŸΌ Check all 82 answers



Thanks πŸ™Œ for reading and good luck on your next tech interview!
Explore 3800+ dev interview question here πŸ‘‰ Devinterview.io

About

🟣 PHP coding interview questions and answers for developers.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /