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

>> php filename.phpHere filename is the name of the script or file you saved.
2. Top 5 features of PHP 8 Version?
Answer:
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
programmaing.
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 ?
Answer:
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.
Ex:
$k = 5; for ($n = 0; $n < 5; $n++) { echo $n; $k = $k+1; if ($k == 7) { break; } }
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.
Ex:
$k = 1; for ($n = 0; $n < 5; $n++) { echo $n; $k = $k+1; if ($k < 3) { continue; } }
5. What is the main difference between require() and require_once()?
Answer:
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?
Answer:
- 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.
Answer:
The foreach statement is used to loop through arrays.
Syntax:
<?php
$animal = [‘dog’, ‘cat’, ‘ant’, ‘bee’];
foreach($animal as $anm) {
echo $anm.PHP_EOL;
} ?>
8. What are the different types of Array in PHP?
Answer:
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.
Ex:
[ [1, 2, 3] , [4, 5, 6], [7, 8, 9] ];
9. How to concatenate two strings in PHP?
Answer:
Using a dot(.) operator we can concat strings in PHP.
Ex:
$a = 'Apple'; $b = 'Cat'; $c = $a.$b;
Related Interview questions Node/Javascript:


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.
I needed it!
Thanks for sharing 🙂