0. php version

最後更新: 2024-07-03

目錄

  • EOL(End of life)
  • PHP 5.6

EOL(End of life)

 

  • 8.3          # 31 Dec 2027
  • 8.2          # 31 Dec 2026
  • 8.1          # 31 Dec 2025

已 EOL

  • 8.0          # 2023-11-26
  • 7.4

 


PHP 5.6

 

Constant scalar expressions

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

=========================

Variadic functions via ..

<?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);
?>

=========================

Argument unpacking via ...

<?php
function add($a, $b, $c) {
    return $a + $b + $c;
}

$operators = [2, 3];
echo add(1, ...$operators);
?>

=========================

Exponentiation via **

2 ** 3

=========================

use function and use const

The use operator has been extended to support importing functions and constants in addition to classes. This is achieved via the use function and use const constructs, respectively.

<?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();
}
?>

=========================

 

PHP 5.5

  • Zend Optimizer+ is to be integrated
  • array_column()

Generators added

=============

finally keyword added

=============

foreach now supports list()

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

foreach ($array as list($a, $b)) {
    echo "A: $a; B: $b\n";
}
?>

=============

Class name resolution via ::class

<?php
namespace Name\Space;
class ClassName {}

echo ClassName::class;

?>

Name\Space\ClassName

=============

array and string literal dereferencing

<?php
echo 'Array dereferencing: ';
echo [1, 2, 3][0];
echo "\n";

echo 'String dereferencing: ';
echo 'PHP'[0];
echo "\n";
?>

 

PHP 5.4

  • shortened array syntax ( "[]" )
  • Traits ( "use" )
  • built-in web server

use

<?php
trait ezcReflectionReturnInfo {
    function getReturnType() { /*1*/ }
    function getReturnDescription() { /*2*/ }
}

class ezcReflectionMethod extends ReflectionMethod {
    use ezcReflectionReturnInfo;
    /* ... */
}

class ezcReflectionFunction extends ReflectionFunction {
    use ezcReflectionReturnInfo;
    /* ... */
}
?>

 

PHP 5.3

  • Dynamic access to static methods
  • Late Static Bindings
  • Exceptions can now be nested
  • mail() function now supports logging of sent email via the mail.log
  • const keyword
  • goto keyword
  • Namespaces (solve problems: collisions) [use]

 


不同版本的不同

 

PHP 5.4.0

 

<1>

Call to undefined function session_unregister() in /usr/share/squirrelmail/functions/global.php on line 271

# DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.

所以 PHP 5.4 的 session 寫法有改變

PHP 5.4 移除了 session_is_registered(), session_register(), session_unregister()

<2>

    // On PHP 5.1
    $data = array("foo", "bar");

    // On PHP 5.4+
    $data = ["foo", "bar"];

 

 

 

Creative Commons license icon Creative Commons license icon