
 Hello everyone, Maxim Ivanov with you, and today we will talk about the innovations of PHP 7, which Guillaume Dievart will tell us in more detail in his guide, made in the form of exercises. But before starting, I want to point out one moment. I will not provide a complete guide to this programming language in this review, just leave here a link to the latest and most accurate information. Josh Lockhart (author of the PHP Guide: The Right Way, developer of the Slim Framework) wrote this book to help newbies, he said: reliable information on the PHP language, so my guide is designed to solve this problem. " What exactly? You know that a huge amount of PHP content is scattered around the Internet, but much has become outdated or does not lead to writing high-quality code. This book contains basic current information with links to trusted resources. If anyone is interested, this is in JavaScript . Now back to the exercises and get started.
 Hello everyone, Maxim Ivanov with you, and today we will talk about the innovations of PHP 7, which Guillaume Dievart will tell us in more detail in his guide, made in the form of exercises. But before starting, I want to point out one moment. I will not provide a complete guide to this programming language in this review, just leave here a link to the latest and most accurate information. Josh Lockhart (author of the PHP Guide: The Right Way, developer of the Slim Framework) wrote this book to help newbies, he said: reliable information on the PHP language, so my guide is designed to solve this problem. " What exactly? You know that a huge amount of PHP content is scattered around the Internet, but much has become outdated or does not lead to writing high-quality code. This book contains basic current information with links to trusted resources. If anyone is interested, this is in JavaScript . Now back to the exercises and get started.<?php $phpVersion = 6; echo PHP_MAJOR_VERSION === $phpVersion ? "Next step !" : "No !";  //  No !  <?php $phpVersion = 7; echo PHP_MAJOR_VERSION === $phpVersion ? "Next step !" : "No !"; //  Next step !  <?% echo "Next step !";  //  syntax error, unexpected '%', expecting end of file on line 1  <?php echo "Next step !"; //  Next step !  <?php if (ereg_replace("PHP([3-6])", "PHP7", "PHP6")) { echo "Next step !"; }  //  Uncaught Error: Call to undefined function ereg_replace() in /tmp/__hoa_6b3Hmf:3 Stack trace: #0 /tmp/__hoa_f8PIGz(52): require() #1 {main} thrown on line 3  <?php if (preg_replace("/PHP([3-6])/", "PHP7", "PHP6")) { echo "Next step !"; } //  Next step !  <?php class Foo { public function foo() { } } echo "Next step !";  //  Methods with the same name as their class will not be constructors in a future version of PHP; Foo has a deprecated constructor on line 3  <?php class Foo { public function __construct() { } } echo "Next step !"; //  Next step !  <?php switch ('') { default: echo "Doesn't pass here ..."; break; default: echo "Next step !"; }  //  Switch statements may only contain one default clause on line 7  <?php switch ('') { default: echo "Next step !"; } //  Next step !  <?php $users = ['Pierre', 'Paul', 'Next step !']; usort($users, function ($a, $b) { }); echo current($users);  //  Pierre  <?php $users = ['Pierre', 'Paul', 'Next step !']; usort($users, function ($a, $b) { return $a <=> $b; }); echo current($users); //  Next step !  <?php echo $_GET['query'] ? $_GET['query'] : "Next step !";  //  Undefined index: query on line 3  <?php echo $_GET['query'] ?? "Next step !"; //  Next step !  <?php $_GET['title'] = false; //    ,    $query = $_GET['title'] ?? '*'; // true //     if($query === '*') { echo "Next step !"; }  //   <?php $_GET['title'] = false; $query = $_GET['title'] ?? '*'; if($query === false) { echo "Next step !"; } //  Next step !  <?php $b = false; echo $a ?? $b ?? "Next step !";  //   <?php $b = null; echo $a ?? $b ?? "Next step !"; //  Next step !  <?php use Foo\Bar\Email; use Foo\Bar\Phone; use Foo\Bar\Address\Code; use Foo\Bar\Address\Number; echo "Next step !";  //  Next step !  <?php use Foo\Bar\{ Email, Phone, Address\Code, Address\Number }; echo "Next step !"; //  Next step !  <?php class Foo { //   public function getList() { return "Next step !"; } } echo (new Foo)->list();  //  Uncaught Error: Call to undefined method Foo::list() in /tmp/__hoa_FmqgZ0:12 Stack trace: #0 /tmp/__hoa_LbZpe8(52): require() #1 {main} thrown on line 12  <?php class Foo { public function list() { return "Next step !"; } } echo (new Foo)->list(); //  Next step !  <?php $randomInt = mt_rand(0, 42); echo $randomInt;  //  5  <?php $randomInt = random_int(0, 42); echo $randomInt; //  1  <?php // Use preg_replace_callback_array instead of preg_replace_callback echo preg_replace_callback( array( "/PHP6/", "/PHP7/" ), function($matches) { if(strpos($matches[0], 'PHP6') === 0) { return 'Ko !'; } else { return "Next step !"; } }, 'PHP7' );  //  Next step !  <?php // preg_replace_callback_array  preg_replace_callback echo preg_replace_callback_array( array( "/PHP6/" => function() { return "Ko !"; }, "/PHP7/" => function() { return "Next step !"; }, ), 'PHP7' ); //  Next step !  <?php class Logger { public static function write(Message $message) { echo $message->getText(); } } interface Message { public function getText(); } class MyMessage implements Message { public function getText() { return "Next step !"; } } Logger::write(new MyMessage());  //  Next step !  <?php class Logger { public static function write(Message $message) { echo $message->getText(); } } interface Message { public function getText(); } $message = (new class() implements Message { public function getText() { return "Next step !"; } }); Logger::write($message); //  Next step !  <?php function add($a, $b) { return $a + $b; } if(add(5.5, 5) === 10) { echo "Next step !"; }  //   <?php function add(int $a, int $b) { return $a + $b; } if(add(5.5, 5) === 10) { echo "Next step !"; } //  Next step !  <?php function add(int $a, int $b) { return $a + $b; } //    if(add(5.5, 5.5) === 11) { echo "Next step !"; }  //   <?php function add(int $a, int $b) { return $a + $b; } if(add(5.5, 5.5) === 10) { echo "Next step !"; } //  Next step !  <?php declare(strict_types = 1); function add(int $a, int $b) { return (int)($a + $b); } if(add(5.5, 5.5) === 11) { echo "Next step !"; }  //  Uncaught TypeError: Argument 1 passed to add() must be of the type integer, float given, called in /tmp/__hoa_FKwVHc on line 10 and defined in /tmp/__hoa_FKwVHc:5 Stack trace: #0 /tmp/__hoa_FKwVHc(10): add(5.5, 5.5) #1 /tmp/__hoa_rejBtd(52): require('/tmp/__hoa_FKwV...') #2 {main} thrown on line 5  <?php declare(strict_types = 1); function add(float $a, float $b) { return (int)($a + $b); } if(add(5.5, 5.5) === 11) { echo "Next step !"; } //  Next step !  <?php declare(strict_types = 1); strlen(42); echo "Next step !";  //  Uncaught TypeError: strlen() expects parameter 1 to be string, integer given in /tmp/__hoa_0HYwi9:3 Stack trace: #0 /tmp/__hoa_CjotyX(52): require() #1 {main} thrown on line 3  <?php declare(strict_types = 1); strlen('42'); echo "Next step !"; //  Next step !  <?php echo "Next step !"; declare(strict_types = 1);  //  strict_types declaration must be the very first statement in the script on line 5  <?php declare(strict_types = 1); echo "Next step !"; //  Next step !  <?php function reverse(string $string): type { return strrev($string); } echo reverse("! pets txeN");  //  Uncaught TypeError: Return value of reverse() must be an instance of type, string returned in /tmp/__hoa_ZcQkYd:5 Stack trace: #0 /tmp/__hoa_ZcQkYd(8): reverse('! pets txeN') #1 /tmp/__hoa_7JLJ4t(52): require('/tmp/__hoa_ZcQk...') #2 {main} thrown on line 5  <?php function reverse(string $string): string { return strrev($string); } echo reverse("! pets txeN"); echo "Next step !"; //  Next step !  <?php // : function foo(): int { return 5.5; } echo foo(); // 5 function add(float $a, float $b): int { return $a + $b; } //    if(add(5.3, 5.3) === 10.6) { echo 'Next step !'; }  //   <?php function add(float $a, float $b): int { return $a + $b; } // Modify the right operand. if(add(5.3, 5.3) === 10) { echo 'Next step !'; } //  Next step !  <?php class Child {} class ChildB extends Child {} abstract class A { abstract public function foo(): Child; } class B extends A { public function foo(): ChildB { return new ChildB; } } echo "Next step !";  //  Declaration of B::foo(): ChildB must be compatible with A::foo(): Child on line 17  <?php class Child {} class ChildB extends Child {} abstract class A { abstract public function foo(): Child; } class B extends A { public function foo(): Child { return new ChildB; } } echo "Next step !"; //  Next step !  <?php //    if("\?{26C4}" === '') { echo "Next step !"; }  //   <?php // Modify the left operand. if("\u{26C4}" === '') { echo "Next step !"; } //  Next step !  <?php class MyClass { } $myClassSerialized = serialize(new MyClass()); $myClass = unserialize( $myClassSerialized, ["allowed_classes" => ['']] ); if($myClass instanceOf MyCLass) { echo "Next step !"; }  //   <?php class MyClass { } $myClassSerialized = serialize(new MyClass()); $myClass = unserialize( $myClassSerialized, ["allowed_classes" => ['MyClass']] ); if($myClass instanceOf MyCLass) { echo "Next step !"; } //  Next step !  <?php function generator() { yield 1; // 'yield'  «           yield 2; //      } function subGenerator() { yield 3; yield 4; yield "Next step !"; } $generator = generator(); foreach($generator as $value) { echo $value.PHP_EOL; }  //  1 2  <?php function generator() { yield 1; yield 2; yield from subGenerator(); } function subGenerator() { yield 3; yield 4; yield "Next step !"; } $generator = generator(); foreach($generator as $value) { echo $value.PHP_EOL; } //  1 2 3 4 Next step !  <?php function foo() { return function() { echo "Next step !"; }; } //    foo     //   <?php function foo() { return function() { echo "Next step !"; }; } // : $foo = foo(); $foo(); foo()(); //  Next step !  <?php $a = ['b' => 'c']; $c = 'Next step !'; //    Next step !  PHP5 echo $$a['b'];  //  Undefined variable: Array on line 7  <?php $a = ['b' => 'c']; $c = 'Next step !'; echo ${$a['b']}; //  Next step !  <?php try { //    undefinedFunction(); } catch(Exception $e) { echo "Next step !"; }  //  Uncaught Error: Call to undefined function undefinedFunction() in /tmp/__hoa_PaBzqc:4 Stack trace: #0 /tmp/__hoa_SQZOZG(52): require() #1 {main} thrown on line 4  <?php try { undefinedFunction(); } catch(Error $e) { echo "Next step !"; } //  Next step !  <?php declare(strict_types = 1); function add(int $a, int $b): int { return $a + $b; } // Catch the TypeError try { echo add('1', '1'); } catch(Exception $e) { echo "Next step !"; }  //  Uncaught TypeError: Argument 1 passed to add() must be of the type integer, string given, called in /tmp/__hoa_FvS6wW on line 12 and defined in /tmp/__hoa_FvS6wW:5 Stack trace: #0 /tmp/__hoa_FvS6wW(12): add('1', '1') #1 /tmp/__hoa_kfhSdT(52): require('/tmp/__hoa_FvS6...') #2 {main} thrown on line 5  <?php declare(strict_types = 1); function add(int $a, int $b): int { return $a + $b; } // Catch the TypeError try { echo add('1', '1'); } catch(TypeError $e) { echo "Next step !"; } //  Next step !  <?php //    CallErrorAndException try { if(random_int(0, 1) === 1) { throw new Exception(''); } undefined(); } catch(CallErrorAndException $e) { echo "Next step !"; }  //  Undefined offset: -1 on line 55  <?php try { if(random_int(0, 1) === 1) { throw new Exception(''); } undefined(); } catch(Throwable $e) { echo "Next step !"; } //  Next step !  <?php try { 10 % 0; } catch(CatchError $e) { echo "Next step !"; }  //  Uncaught DivisionByZeroError: Modulo by zero in /tmp/__hoa_46SBwZ:5 Stack trace: #0 /tmp/__hoa_UQ8f3n(52): require() #1 {main} thrown on line 5  <?php try { 10 % 0; } catch(DivisionByZeroError $e) { echo "Next step !"; } //  Next step !  <?php // var_dump(12 == "0xC"); // true  PHP 5 // var_dump(12 == "0xC"); // false  PHP 7 //    if('0x2A' == 42) { echo "Next step !"; }  //   <?php if(0x2A == 42) { echo "Next step !"; } //  Next step !  <?php //  Generator::getReturn()     function generator() { yield 21; yield 21; return true; } $generator = generator(); foreach($generator as $number) { } if($generator->callReturn() === true) { echo "Next step !"; }  //  Uncaught Error: Call to undefined method Generator::callReturn() in /tmp/__hoa_eOhq2B:14 Stack trace: #0 /tmp/__hoa_sa29ov(52): require() #1 {main} thrown on line 14  <?php function generator() { yield 21; yield 21; return true; } $generator = generator(); foreach($generator as $number) { } if($generator->getReturn() === true) { echo "Next step !"; } //  Next step !  <?php function generator() { yield 21; yield 21; return "Next step !"; } $generator = generator(); $generator->next(); echo $generator->getReturn();  //  // caught: Cannot get return value of a generator that hasn't returned  <?php function generator() { yield 21; yield 21; return "Next step !"; } $generator = generator(); $generator->next(); $generator->next(); echo $generator->getReturn(); //  Next step !  <?php function foo($a, $a) { return $a; } echo foo("Next ", "step !"); //  PHP5: step !  //  Redefinition of parameter $a on line 4  <?php function foo($a, $b) { return $a . $b; } echo foo("Next ", "step !"); //  Next step !  <?php $conf = [ 'user' => 'root', 'password' => 'my_password', 'step' => 'Next step !' ]; //  CONFIGURATION   echo CONFIGURATION['step'];  //  /* * <br /> * <b>Warning</b>: Illegal string offset 'step' in <b>/tmp/__hoa_dBF385</b> on line <b>11</b><br /> */  <?php $conf = [ 'user' => 'root', 'password' => 'my_password', 'step' => 'Next step !' ]; define('CONFIGURATION', $conf); echo CONFIGURATION['step']; //  Next step ! Source: https://habr.com/ru/post/302942/
All Articles