PHP 7 interview questions For Freshers & Experienced

  • Question 1) What new in PHP 7?
  • Question 2) What is Scalar type declaration?
  • Question 3) What is null coalescing operator?
  • Question 4) Enlist PHP features that deprecated in PHP7?
  • Question 5) What are Anonymous classes in PHP?
  • Question 6) What are traits in PHP?
  • Question 7) What is type checking in PHP?
  • Question 8) What is elvis operator in PHP?
  • Question 9) What is Type hinting in PHP?
  • Question 10) What is List in PHP?
  • Question 11) How to access standard error stream in PHP?
  • Anil Saini
  • 08th Jan, 2020
  • 490 Followers

PHP 7 interview questions
Updated ON

08th Jan, 2020

No. of Questions

11

Posted In

PHP

Author

Anil Saini

What is PHP7?

PHP7 is a new version of the PHP Programming language that has been delivered and developed for mobile to the cloud. PHP code incorporated in PH7 has makes it twice as fast as PH5 and uses fewer resources and low consumption of memory. Scalar type declarations like return and parameter types can be imposed. PH7 employs Zend Engine 3.0 to improve the performance of the application.

PH7 can serve concurrent users without using any extra hardware and is refactored keeping in mind about the workload of today’s generation. Old APIs and extensions are removed from PH7 and a new null coalescing operator has been added.

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 new in PHP 7?

PHP 7 is a major release following PHP 5, and it brought various exciting new features such as,

  • Speed

The code in PHP 7 has been refactored to increase performance and reduce memory consumption. So, PHP 7 brings about twice the performance boost of PHP 5.6.

  • Type declaration

PHP is considered as weakly typed language because it doesn’t require you to declare data types. But in PHP 7, you can do scalar type declarations for int, float, string, and Boolean. By default, it is a non-strict declaration, but you can do a strict declaration type. PHP 7 also supports the return type declaration.

  • Error Handling

PHP 7 now supports error handling functions. Now, exceptions can be thrown when an error occurs thus stopping fatal crash in the program.

  • New Operators

PHP 7 brings new operators such as spaceship operator and null coalescing operator.

  • CSPRING Functions

It is introduced with PHP 7, and it gives an easy and secured way of generation random integers and bytes for using it with cryptographic functions.

Q2. What is Scalar type declaration?

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.

Q3. 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. 

Q4. Enlist PHP features that deprecated in PHP7?

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.

Q5. What are Anonymous classes in PHP?

A new feature in PHP 7, an Anonymous class is a class definition that has no name. Users don’t need to define the name as it is internally generated. So, it allows creating objects without the need to name them. It is useful in creating simple, one-off objects or you can create an interface with these objects.

$obj = new class(‘’Priyag’’); 
public $name; 
Public function_name($arg){ 
$this->$name = $arg; 
}  
}; 

The above is an example of an anonymous class that has no name and creating an object for the anonymous class.

Q6. 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.

Q7. 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 

Q8. 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.

Q9. 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.

Q10. 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.

Q11. 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

    Anil Saini

    Anil Saini is a Software Professional and technical content writer. He like to write technical blogs on latest framework and Computer Programming Languages.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.