Some evolution from PHP 5.3 to PHP 7.3

<?php echo 5.3;

<?php
class C {
public static $foo = 42;
}

$a = "C";
echo $a::$foo; //output 42

<?= 5.4;

<?php
$a = [1, 2, 3, 4];

$a = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4];
<?php
class Calculator {
public function add($a, $b) {
return $a + $b;
}
}
echo (new Calculator)->add(40, 2); //output 42

<?php echo(5.5);

<?php
$array = [
[1, 2],
[3, 4],
];

foreach ($array as list($a, $b)) {
echo "A: $a; B: $b\n";
}
//output
A: 1; B: 2
A: 3; B: 4
<?php
function always_false() {
return false;
}

if (empty(always_false())) {
echo "This will be printed.\n";
}
if (empty(true)) {
echo "This will not be printed.\n";
}
<?php
echo 'Array dereferencing: ';
echo [1, 2, 3][0];
echo "\n";

echo 'String dereferencing: ';
echo 'PHP'[0];
echo "\n";
//output
Array dereferencing: 1
String dereferencing: P
<?php
namespace Name\Space;
class ClassName {}

echo ClassName::class;
//output Name\Space\ClassName

<?= 5.6;

<?php
const ONE = 1;
const TWO = ONE * 2;

class C {
const THREE = TWO + 1;
const ONE_THIRD = ONE / self::THREE;
const SENTENCE = 'The value of THREE is '.self::THREE;

public function f($a = ONE + self::THREE) {
return $a;
}
}

echo (new C)->f(); //output 4
echo C::SENTENCE; //output The value of THREE is 3
<?php
function f($req, $opt = null, ...$params) {
// $params is an array containing the remaining arguments.
printf('$req: %d; $opt: %d; number of params: %d'."\n",
$req, $opt, count($params));
}

f(1);
f(1, 2);
f(1, 2, 3);
f(1, 2, 3, 4);
f(1, 2, 3, 4, 5);
<?php
function add($a, $b, $c) {
return $a + $b + $c;
}

$operators = [2, 3];
echo add(1, ...$operators);-
<?php
namespace Name\Space {
const FOO = 42;
function f() { echo __FUNCTION__."\n"; }
}

namespace {
use const Name\Space\FOO;
use function Name\Space\f;

echo FOO."\n";
f();
}
//output
42
Name\Space\f

<?= 7.0;

<?php
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1

// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
<?php 
define('ANIMALS', [
'dog',
'cat',
'bird'
]);

echo ANIMALS[1]; // outputs "cat"
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;

use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;

use const some\namespace\ConstA;
use const some\namespace\ConstB;
use const some\namespace\ConstC;

use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{ConstA, ConstB, ConstC};

<?= 7.1;

<?php

function testReturn(): ?string
{
return 'elePHPant';
}

var_dump(testReturn());

function testReturn(): ?string
{
return null;
}

var_dump(testReturn());

function test(?string $name)
{
var_dump($name);
}

test('elePHPant');
test(null);
test();
//output
string(10) "elePHPant"
NULL
string(10) "elePHPant"
NULL
Uncaught Error: Too few arguments to function test(), 0 passed in...
<?php
class ConstDemo
{
const PUBLIC_CONST_A = 1;
public const PUBLIC_CONST_B = 2;
protected const PROTECTED_CONST = 3;
private const PRIVATE_CONST = 4;
}
<?php
try {
// some code
} catch (FirstException | SecondException $e) {
// handle first and second exceptions
}

<?= 7.2;

<?php

function test(object $obj) : object
{
return new SplQueue();
}

test(new StdClass());
<?php

abstract class A
{
abstract function test(string $s);
}
abstract class B extends A
{
// overridden - still maintaining contravariance for parameters and covariance for return
abstract function test($s) : int;
}

<?= 7.3 (BETA);

<?php
function foo($a, $b,)
<?php
try {
json_decode("{", false, 512, JSON_THROW_ON_ERROR);
}
catch (\JsonException $exception) {
echo $exception->getMessage(); // echoes "Syntax error"
}

--

--

Fullstack Developer- certified Symfony 4,5 and certified AWS Solution Architect - Freelancer - Remote Worker

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Smaine Milianni

Fullstack Developer- certified Symfony 4,5 and certified AWS Solution Architect - Freelancer - Remote Worker