PHP 7 interview questions

PHP 7 interview questions | Freshers & Experienced

  • Arushi
  • 12th Feb, 2021
  • 794 Followers

Key Features of Php 7

Below are few major features of Php 7

Anonymous classes

Spaceship operator

Null coalescing operator

Return type declarations

Unicode codepoint escape syntax

Closure::call()

Filtered unserialize()

Generator delegation

Fast Performance

PHP 7 interview questions

Q1. What's new in PHP7?

New features of PHP 7 are  - 

  1. Scalar type declaration
  2. Return type declarations
  3. Null coalescing operator
  4. Spaceship operator
  5. Constant arrays using define
  6. Anonymous classes
  7. Unicode codepoint escape syntax
  8. Closure::call
  9. Filtered unserialize
  10. IntlChar
  11. Expectations
  12. Group use declarations
  13. Generator Return Expressions
  14. Generator delegation
  15. Integer division with intdiv
  16. Session options
  17. preg_replace_callback_array
  18. CSPRNG Functions

Q2. How to upgrade the version of PHP 5 to PHP 7?

You can upgrade the version of PHP 5 to PHP 7 by asking your hosting company to update it for your account. You can follow the given steps for that:

  • Log in to your one.com control panel.
  • Click on the Advanced settings tile and then select PHP and database settings.
  • Scroll down to Update PHP versions.
  • Select the PHP version you want to switch to and click Save.

 

Q3. What is Null Coalesce Operator?

Null Coalesce Operator (??) is introduced in PHP 7. This operator is used as a replacement of isset() along with the ternary operator (?:). The Null Coalesce operator (??) returns the result of its first operand if it exists and is not NULL, or else it will return its second operand.

Q4. Explain Scalar type declarations in PHP7.

Scalar type declarations is a new feature of PHP that is introduced in PHP 7. Scalar type declaration or type hints allow the user to specify the data type for a variable. Introduced in PHP 7, Scalar type declaration supports int, float, string, and Boolean. But, by default the Scalar type declaration is non-strict. That is, you need to specify the data type for the variable, but typecasting is also possible in this type. You can also do a strict type declaration by adding (strict_types=1) on top of the file. In strict type declaration, the data type is specified, and typecasting is not possible.

There are two options of Scalar type declaration are available.

  • Strict
  • Coercive

Q5. What are anonymous classes in PHP7.

Anonymous Class in PHP is new way of creating object without the need to name them. Anonymous classes are created and initiated at same time.

Example of an Anonymous Class in PHP

$object =new class('argument'){

public $property;

public function __construct($argument){

	$this->property=$argument;

}

}

Q6. What is Coercive mode in PHP?

Coercive mode in PHP is the default mode of Scalar type declarations (Feature introduced in PHP 7).

Q7. What is Strict mode in PHP?

Strict mode is new feature in PHP in which variable declaration is required before using it.

Q8. What are Array constants?

In PHP 5.6 or we can define constants as a scalar expression. We can define an array constant using the define keyword.

Array Constant Example in PHP

 define('fruits', [
      'apple',
      'mango',
      'banana'
   ]);
 print(fruits[2]); // outputs banana

Q9. Explain Spaceship Operator?

Spaceship operator also is known as (combined comparison operator) is a new operator introduced in PHP 7. This operator is denoted by "<=>". This single operator can perform greater than, less than, and equal comparison between two operands. Its behavior is similar like strcmp() or version_compare() functions and can be used with integers, floats, strings, arrays, objects, etc.

Q10. What is use of random_bytes() in PHP?

random_bytes() function in PHP is used to generate cryptographically secure pseudo-random bytes.

Syntax:

String random_bytes ( int $length )

Usage

$length = random_bytes('4'); 
  
//Print the reult and convert by binaryhexa 
var_dump(bin2hex($length)); 

Q11. List some features of PHP that are deprecated in PHP 7?

Some of the earlier features are deprecated in PHP 7. They are,

  • Style constructors

These are the functions that have the same name as their class. It is deprecated in PHP 7 and it will be removed in the future version.

  • Static call to non-static methods

Calling static to a non-static method is deprecated in PHP 7.

  • Salt option in password_hash()

The salt option lets developers make their salt in password_hash() function. It is now deprecated in PHP 7.

  • Capture_session_meta SSL context option

This now deprecated but it can be accessed via stream_get_meta_data() function.

  • ldap_sort()

The ldap_sort() function is now deprecated in PHP 7.

Q12. What is use of set_exception_handler() in PHP?

set_exception_handler() is a library function in PHP. It is used to set the default exception handler if an exception is not caught within a try/catch block in PHP.exception_handler stop the execution of script.

Q13. List some extensions have been removed from PHP 7?

Following extensions are removed from PHP and are no longer in use

  • ereg
  • mssql
  • mysql
  • sybase_ct

Read More from https://www.php.net

Q14. What is null coalescing operator?

A new operator introduced in PHP 7, it returns the left operand of the operator if it is not null or the right operand. If the left operand is non-existent, it will raise a notice.

$name = $Name ?? "Priyag"; //example 

In the above example, if the $Name is not null then $name is assigned with $Name or it is assigned with “Priyag”. An additional feature of this operator is that you can stack it.

$name = $Name ?? $username ?? $placeholder ?? “Priyag”; //stacking null coalescing operator. 

Q15. What are traits in PHP?

A trait in PHP can be inferred as an interface or an abstract class that enables code reuse. PHP is a single inheritance language that has a lot of constraints and with traits, a group of methods can be reused in other independent classes thus reducing some of PHP’s limitations.

A trait can be declared with the “trait” keyword. It has a group of consistent functions that cannot be instantiated like a class. A trait can be used in class by using the “use” statement.

<?php 
trait exampleTrait { 
   function getReturnType() { /* ... */ } 
} 
class exampleClass extends ReflectionMethod { 
   use exampleTrait; 
   /* ... */ 
} 
?> 

The above is an example of trait. Here, the class “exampleClass” uses the trait “exampleTrait” by the “use” statement.

The trait has less precedence than an inherited class. That is, a member inherited from base class overrides the member from trait. Multiple traits can also be used by listing them in the “use” statement separated by a comma.

Q16. What is type checking in PHP?

Type checking is the process of getting the type of a variable in PHP. PHP offers a gettype() function to check the type of a variable (i.e), it returns the type of the variable passed to it.

String gettype($var); //syntax 
//example 
$var1 = “hello!”; 
$var2 = true; 
$var3 = 1; 
$var4 = 1.2; 
$var5 = array(0, 1); 
$var6 = NULL; 
$var7 = file(); 
$var8 = new obj	Class; 
echo gettype($var1) // prints string 
echo gettype($var2) // prints boolean 
echo gettype($var3) // prints integer 
echo gettype($var4) // prints float 
echo gettype($var5) // prints array 
echo gettype($var6) //prints NULL 
echo gettype($var7) // prints resource 
echo gettype($var8) //prints object 

Q17. What is elvis operator in PHP?

Elvis operator functions as a ternary operator but reduces the code used in the ternary operator.

Statement1 ?: Statement2 //syntax for Elvis operator

The Elvis operator(?) returns the left operand (Statement1) if it is true or the right operand (Statement2) otherwise.

$var = null ?: “hello” //example for Elvis operator 

In the above example, the $var get the value of “hellos” as the left operand of the elvis operator is null.

Q18. What is Type hinting in PHP?

PHP is a dynamically typed language that is, you don’t have to declare data types. So, there is a higher chance of error in this type. But PHP introduced type hints in PHP 5. With type hint, you can specify the type expected by an argument of a function. It is optional, but when you specify type hint PHP checks for the associated type when calling the function. If not, an error will be raised.

Scalar data types like integer, string, float, etc are not supported in type hints. Valid types in type hints are class names and array.

<?php 
function enroll(Student $student) { 
echo "Enrolling ". $student->name; 
} 
$me = new Student("priyag"); 
$enroll = enroll($me); 
?> 

In the above example, enroll function only accepts arguments of class type Student. So, it is a type hint.

Q19. What is List in PHP?

PHP has an inbuilt function list() that is used to assign array values to multiple variables at a time. The first element in the array gets assigned to the first variable in the list and so on. Only numerical arrays can be assigned to the list. Also, the number of variables in the list must be equal to the length of the array.

list($variable1, $variable2....) //syntax for list 
//example 
<? php 
$arr = array(1, 2); 
list($var1, $var2) = $arr; 
?> 

Here, the variables in the list() function is assigned with the $arr array.

Note: before PHP 7, list() assigns array values from the right most parameter.

Q20. How to access standard error stream in PHP?

PHP provides constant stderr to access the error stream.

To open the error stream, type the following command,

$log = fopen("php://stderr", "a"); 

To write into the error stream, type the following command,

fwrite($log, "test message"); 

To close the stream,

fclose($log);

About Author :

  • Author of PHP 7 interview questions

    Arushi Pandey

    Arushi Pandey is senior PHP developer and have hand on experience on PHP Frameworks and tools.He loves to share and write programming content on internet.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.