Presented By: Nirmal V,
Date: 25/07/2017
Location: OpenSource Kerala 2017, Kochi
PHP: Hypertext Preprocessor
22 years history (Since 1995)
The most popular server side language: PHP is used by 82.3% of all the websites (Source: w3techs.com)
Used by Facebook, Wikipedia, Yahoo, Etsy, Flickr, Digg etc.
Latest Version is PHP 7.1
PHP
Released Date: 3rd December 2015
Improved Performance
Lower Memory Consumption
Uses Zend Engine 3.0
Consistent 64-bit support
New Parser
Improved Exception Hierarchy
Many fatal errors converted to Exceptions
Deprecated SAPI's and Extensions removed
Anonymous Classes
Unicode support at the core language level
Not Released, Project is abandoned
PHP 7 - Speed
PHP 7 - Speed
PHP 7 - Speed
PHP 7 - Speed
PHP 7 - Null Coalescing Operator
<?php
// fetch the value of $_GET['user'] and returns 'not passed'
// if username is not passed
$username = $_GET['username'] ?? 'not passed';
print($username);
print("<br/>");
// Equivalent code using ternary operator
$username = isset($_GET['username']) ? $_GET['username'] : 'not passed';
print($username);
print("<br/>");
// Chaining ?? operation
$username = $_GET['username'] ?? $_POST['username'] ?? 'not passed';
print($username);
?>
PHP 7 - Spaceship Operator
<?php
//integer comparison
print( 1 <=> 1);print("<br/>");
print( 1 <=> 2);print("<br/>");
print( 2 <=> 1);print("<br/>");
print("<br/>");
//float comparison
print( 1.5 <=> 1.5);print("<br/>");
print( 1.5 <=> 2.5);print("<br/>");
print( 2.5 <=> 1.5);print("<br/>");
print("<br/>");
//string comparison
print( "a" <=> "a");print("<br/>");
print( "a" <=> "b");print("<br/>");
print( "b" <=> "a");print("<br/>");
?>
0
-1
1
0
-1
1
0
-1
1
It produces the following browser output −
PHP 7 - use Statement
<?php
// Before PHP 7
use com\tutorialspoint\ClassA;
use com\tutorialspoint\ClassB;
use com\tutorialspoint\ClassC as C;
use function com\tutorialspoint\fn_a;
use function com\tutorialspoint\fn_b;
use function com\tutorialspoint\fn_c;
use const com\tutorialspoint\ConstA;
use const com\tutorialspoint\ConstB;
use const com\tutorialspoint\ConstC;
// PHP 7+ code
use com\tutorialspoint\{ClassA, ClassB, ClassC as C};
use function com\tutorialspoint\{fn_a, fn_b, fn_c};
use const com\tutorialspoint\{ConstA, ConstB, ConstC};
?>
PHP 7 - Removed Extensions
Following Extensions have been removed from PHP 7 onwards −
ereg
mssql
mysql
sybase_ct
PHP 7 - Constant Arrays
<?php
//define a array using define function
define('animals', [
'dog',
'cat',
'bird'
]);
print(animals[1]);
?>
PHP 7 - Integer Division
<?php
$value = intdiv(10,3);
var_dump($value);
print("<br />");
print($value);
?>
It produces the following browser output −
int(3) 3
PHP 7 - Return Type Declarations
<?php
declare(strict_types = 1);
function returnIntValue(int $value): int {
return $value; // Valid Return Type
?>
}
print(returnIntValue(5));
?>
<?php
declare(strict_types = 1);
function returnIntValue(int $value): int {
return $value + 1.0; //Invalid Return Type
}
print(returnIntValue(5));
?>
PHP 7 - Error Handling
<?php
class MathOperations {
protected $n = 10;
// Try to get the Division by Zero error object and display as Exception
public function doOperation(): string {
try {
$value = $this->n % 0;
return $value;
} catch (DivisionByZeroError $e) {
return $e->getMessage();
}
}
}
$mathOperationsObj = new MathOperations();
print($mathOperationsObj->doOperation());
?>
PHP 7 - CSPRNG
<?php
$bytes = random_bytes(5);
print(bin2hex($bytes));
?>
<?php
print(random_int(100, 999));
print("");
print(random_int(-1000, 0));
?>
PHP 7 - IntlChar
<?php
printf('%x', IntlChar::CODEPOINT_MAX);
print (IntlChar::charName('@'));
print(IntlChar::ispunct('!'));
?>
PHP 7 - Anonymous classes
<?php
/**
* Anonymous classes
*/
$foo = new class {
public function foo() {
return "bar";
}
};
var_dump($foo,$foo->foo());
PHP 7 - Scalar Type Declarations
<?php
/**
* Scalar type declarations
*/
//declare(strict_types=1);
function add(int $a, int $b) {
return $a + $b;
}
print add(1,2);
print add("1","2");
PHP 7 - Class Constant Visibility
class Post
{
protected const PUBLISHED = 1;
protected const DRAFT = 2;
protected const TRASHED = 3;
// ...
}
PHP 7 - Void return type
function dump($object): void
{
var_dump($object);
}
A void function can omit the return statement or add an empty one (return;).
PHP 7 - ArgumentCountError Exception
// PHP 5.6
function sum($a, $b)
{
return $a + $b;
}
sum();
// Warning: Missing argument 1 for sum()
// Warning: Missing argument 2 for sum()
sum(3);
// Warning: Missing argument 2 for sum()
sum(3, 4);
// PHP 7.1
function sum($a, $b)
{
return $a + $b;
}
sum();
// Fatal error: Uncaught ArgumentCountError: Too few arguments
to function sum(), 0 passed in /vagrant/index.php on line 18
and exactly 2 expected in /vagrant/index.php:13
sum(3); // skipped
sum(3, 4); // skipped
PHP 7 - Invalid String Arithmetics
var_dump("1" + "2");
var_dump("1" + "a string");
var_dump("1" + "2 with a string");
in PHP 7.1, we get a warning if a non numeric value is found, and a notice if the value is not well formatted (2 a string = 2).
PHP 7 - Deprecated Features
<?php
// PHP 4 style constructors
class A {
function A() {
print('Style Constructor');
}
}
?>
<?php
//Static calls to non-static methods
class A {
function b() {
print('Non-static call');
}
}
A::b();
?>
nirmalkamath@gmail.com
http://codenirmal.com