php interview questions (0-2 years exp) – 2023

Programming questions

These questions are for interview preparation on php, it is suitable for candidates with experience range 0-2 years.


1. How to execute any php file using cmd? Answer: PHP file or scripts can easily executed by command-line Interface. The command is as follows:
>> php filename.php
Here filename is the name of the script or file you saved.

2. Top 5 features of PHP 8 Version?


i) Union types :
Union Types extend type declarations to mention more than one type.

>> function parse_value(float|int|string): string|null {}

It also support boolean data type as a special type.

ii) NULL-Safe
operator (?->):

Before PHP 8.0

return ($student->getAddress() ? $student->getAddress()->getCountry() : null)

It will fetch country code if address is not null otherwise
it will return null.

PHP 8.0

return $student->getAddress()?->getCountry();

It will not throw any error even if getAddress() returns null.


iii) ext-JSON always available:

It is no longer possible to compile your PHP script or file
withour ext-JSON because JSON is very commonly used in our day to day PHP

Previously we firstly ensuring that this extension is available or not. Some of the composer packages can’t be installed without this extension. But now we do not need to worry as it is always available in PHP 8.0.

iv) str_contains() function

Before PHP 8.0

if (strpos('abc', 'a') !== false) { /*....*/ }

Now PHP 8.0

if (Str_contains('abc', 'a')) { /*....*/ }

 v) get_debug_type() function

The new PHP get_debug_type() function will return the true “native” type of a variable. The function would differ from gettype in that it would return native type names, e.g. int rather than integer, double instead of float, bool instead of boolean.

3. What is the meaning of continue and break statements
in PHP ?


Break: This statement is used in a looping construct to terminate the execution of the iteration and to immediately execute the next snippet of code outside the block of the looping construct.


$k = 5;

	for ($n = 0; $n < 5; $n++) {
		echo $n;
		$k = $k+1;
		if ($k == 7) {

Continue: This statement is used to skip the current iteration of the loop and continue to execute the next iteration until the looping construct is exited.


$k = 1;

	for ($n = 0; $n < 5; $n++) {
		echo $n;
		$k = $k+1;
		if ($k < 3) {
4. What are include() and require() functions in PHP? Answer: The Include() function is used to include or put content of one PHP file into another PHP file. It does not stop the execution even if any error occurs it will only produce a warning. The Require() function is also used to put data of one PHP file to another PHP file. It stops the further execution if any error occurs and produces a warning and a fatal error.

5. What is the main difference between require() and require_once()?


The require() includes and evaluates a particular PHP file, while require_once() does that only if it has not been included before.
The require_once() statement can be used to include a php file in another one like require(), when you may need to include the called
file more than once. So, require_once() is recommended to use anytime.

6. What are different types of errors available in Php?


  • E_ERROR – A fatal error that can terminate further exceution of your code.
  • E_WARNING – Run-time warning that does not terminate further exceution.
  • E_PARSE– Compile time parse error.
  • E_NOTICE– Run time notice caused due to error in code.
  • E_CORE_ERROR– Fatal errors that occur during PHP initial startup.
  • E_CORE_WARNING– Warnings that occur during PHP initial startup.
  • E_COMPILE_ERROR– Fatal compile-time errors indication problem with script.
  • E_USER_ERROR– User-generated error message.
  • E_USER_WARNING– User-generated warning message.
  • E_USER_NOTICE- User-generated notice message.
  • E_STRICT– Run-time notices.
  • E_RECOVERABLE_ERROR– Catchable fatal error indicating a dangerous error
  • E_ALL– Catches all errors and warnings.

7. Explain ‘foreach’ loop with example in PHP.

The foreach statement is used to loop through arrays.
    $animal = [‘dog’, ‘cat’, ‘ant’, ‘bee’];
    foreach($animal as $anm) {
      echo $anm.PHP_EOL;

    }  ?>

  8. What are the different types of Array in PHP?

As we all know like any other programming language, PHP has also 3 types of arrays.

1. Indexed Array :

An array with numberic index is called as Indexed arrays.

 Ex: $colors = ['black', 'red', 'white', 'yellow'];
2. Associative Array:

An array with strings as index is called as Associative array.

 Ex:    [
          'a' => 'Apple',
          'b' => 'Blue',
          'c' => 'Cat',
          'd' => 'Dog'
3. Multidimensional Array:

An array containing more that 1 array is known as Multidimensional array.


 [ [1, 2, 3] , [4, 5, 6], [7, 8, 9] ];

9. How to concatenate two strings in PHP?


Using a dot(.) operator we can concat strings in PHP.


      $a = 'Apple';
      $b = 'Cat';
      $c = $a.$b;
10. How can you compare objects in PHP? Answer: We use the operator ‘==’ to test if two objects are instanced from the same class and have same attributes and equal values. We can also test if two objects are referring to the same instance of the same class with the help of the identity operator ‘===’.

Related Interview questions Node/Javascript:

3 thoughts on “php interview questions (0-2 years exp) – 2023

  1. It’s actually a cool and useful piece of information. I’m satisfied that you just shared this useful information with us.

    Please keep us up to date like this. Thanks for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *