Do you have an interview with a hiring manager for a "PHP Developer" position? Are you worried about what will you be asked in the interview? Even if you're just studing for a test or you're an autodidact trying to master PHP, you should review the top 25 PHP interview questions and their answers.
require_once()
, require()
, include()
, and include_once()
functions?require()
will cause E_COMPILE_ERROR which is a fatal error and the script will stop.
include()
will only cause E_WARNING which is a warning and the script will continue.
require_once()
and include_once()
statements are used when you need to include a file more than once. If it is found that the file has already been included, calling script is going to ignore further fatal errors or warnings respectively.
explode()
, preg_split()
and str_split()
functions?explode()
splits a given string into array by string.
preg_split()
splits a string by using a regular expression.
str_split()
converts a given string to an array.
ksort()
, krsort()
, asort()
and arsort()
functions.ksort()
function sorts an associative array according to the key in ascending order.
krsort()
function sorts an associative array according to the key in descending order.
asort()
function sorts an associative array according to the value* in ascending order.
arsort()
function sorts an associative array according to the value in descending order.
The magic methods (or functions) begin with two underscores (__) and have special names. These methods are invoked by particular events. PHP calls the function instead of the coder and provides more functionality. Magic methods are:
__construct()
, __destruct()
, __call($funName, $arguments)
, __callStatic($funName, $arguments)
, __get($propertyName)
, __set($property, $value)
, __isset($content)
, __unset($content)
, __sleep()
, __wakeup()
, __toString()
, __invoke()
, __set_state($an_array)
, __clone()
, __autoload($className)
, __debugInfo()
$var
and $$var
?$var
and $$var
are both variables. The first one is a normal variable, and the second one is a reference variable. $var
has a fixed name, while the name of $$var
is stored in $var
.
htmlentities()
and htmlspecialchars()
functions?htmlentities()
converts all possible characters to HTML entities. htmlspecialchars()
converts only predefined characters to HTML entities. Notices
: These are non-critical errors which occur during executing a script. These errors are not displayed to the user by default. For example, trying to call an undefined variable.
Warnings
: These are serious errors which are displayed to the user, but don't cause script termination. For example, trying to call a non-existent file.
Fatal errors
: These are critical errors which cause the termination of the script. For example, trying to call a non-existent function.
Numeric array is an array that uses numeric index. Values are stored in a linear way.
Associative array is the array in which the keys are strings.
Multidimensional array is an array which contains one or more arrays. The values can be accessed by multiple indices.
getenv()
function provides access to the value of all environment variables.$_REQUEST
$_REQUEST
variable contains $_GET
, $_POST
, and $_COOKIE
. $_REQUEST
can be used to get form data sent with GET
and POST
methods.
===
?The array operator checks the variables on the left and the right side, whether they have the same key-values in the same order and they are of the same types.
preg_match()
and preg_split()
functions?preg_match()
function searches the given string for a required pattern, returning true if the pattern exists, or false, otherwise.
preg_split()
function uses regular expressions as input parameters for a pattern. It operates like split()
.
In a child class, the definition of a function which is inherited from parent class can be changed by overriding.
final
keyword?The final
keyword prevents the child class from overriding a method. If the class is defined final
, then it can not be extended.
Answer:
Joe
Jack
William
Avarel
Validate filters
Sanitize filters
Answer: valid
The seven methods are: getCode()
, getFile()
, getLine()
, getMessage()
, getPrevious()
, getTrace()
, getTraceAsString()
.
file_get_contents()
Answer:
bool(true)
bool(true)
Answer: no output
Answer: 2
$_SERVER['PHP_SELF']
?$_SERVER['PHP_SELF']
is a variable that returns the filename of the current script.
Answer:
Array ( [2] => Array ( [0] => William [1] => Avarel) [1] => Jack [0] => Joe)
A session is started by calling session_start()
function. This function checks if a session has already started. If not, it starts one.
In conclusion, these PHP questions are some of the most asked questions in interviews worldwide, but it doesn't mean that's all. You should continue to review and you might want to continue reading some interesting questions with their answers here. If you think there are more questions that should be included in this list, please, share in the comments below.