--- Source: boolean.xml ---
--- Source: boolean.xml ---
\n";
}
// ...because this can be used with exactly the same meaning:
if ($show_separators) {
echo "
\n";
}
?>
--- Source: boolean.xml ---
--- Source: integer.xml ---
--- Source: integer.xml ---
decimal : [1-9][0-9]*(_[0-9]+)*
| 0
hexadecimal : 0[xX][0-9a-fA-F]+(_[0-9a-fA-F]+)*
octal : 0[oO]?[0-7]+(_[0-7]+)*
binary : 0[bB][01]+(_[01]+)*
integer : decimal
| hexadecimal
| octal
| binary
--- Source: integer.xml ---
--- Source: integer.xml ---
--- Source: integer.xml ---
--- Source: integer.xml ---
--- Source: callable.xml ---
--- Source: callable.xml ---
$a * 2;
// Using Closure::fromCallable
$double4 = Closure::fromCallable('double_function');
// Use the closure as a callback here to
// double the size of each element in our range
$new_numbers = array_map($double1, range(1, 5));
print implode(' ', $new_numbers) . PHP_EOL;
$new_numbers = array_map($double2, range(1, 5));
print implode(' ', $new_numbers) . PHP_EOL;
$new_numbers = array_map($double3, range(1, 5));
print implode(' ', $new_numbers) . PHP_EOL;
$new_numbers = array_map($double4, range(1, 5));
print implode(' ', $new_numbers);
?>
--- Source: callable.xml ---
--- Source: float.xml ---
--- Source: float.xml ---
LNUM [0-9]+(_[0-9]+)*
DNUM ({LNUM}?"."{LNUM}) | ({LNUM}"."{LNUM}?)
EXPONENT_DNUM (({LNUM} | {DNUM}) [eE][+-]? {LNUM})
--- Source: float.xml ---
--- Source: enumerations.xml ---
--- Source: array.xml ---
"bar",
"bar" => "foo",
);
// Using the short array syntax
$array2 = [
"foo" => "bar",
"bar" => "foo",
];
var_dump($array1, $array2);
?>
--- Source: array.xml ---
"a",
"1" => "b",
1.5 => "c",
true => "d",
);
var_dump($array);
?>
--- Source: array.xml ---
"bar",
"bar" => "foo",
100 => -100,
-100 => 100,
);
var_dump($array);
?>
--- Source: array.xml ---
--- Source: array.xml ---
"c",
"d",
);
var_dump($array);
?>
--- Source: array.xml ---
'a',
'1' => 'b', // the value "a" will be overwritten by "b"
1.5 => 'c', // the value "b" will be overwritten by "c"
-1 => 'd',
'01' => 'e', // as this is not an integer string it will NOT override the key for 1
'1.5' => 'f', // as this is not an integer string it will NOT override the key for 1
true => 'g', // the value "c" will be overwritten by "g"
false => 'h',
'' => 'i',
null => 'j', // the value "i" will be overwritten by "j"
'k', // value "k" is assigned the key 2. This is because the largest integer key before that was 1
2 => 'l', // the value "k" will be overwritten by "l"
);
var_dump($array);
?>
--- Source: array.xml ---
--- Source: array.xml ---
"bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
);
var_dump($array["foo"]);
var_dump($array[42]);
var_dump($array["multi"]["dimensional"]["array"]);
?>
--- Source: array.xml ---
--- Source: array.xml ---
1, 12 => 2);
$arr[] = 56; // This is the same as $arr[13] = 56;
// at this point of the script
$arr["x"] = 42; // This adds a new element to
// the array with key "x"
unset($arr[5]); // This removes the element from the array
var_dump($arr);
unset($arr); // This deletes the whole array
var_dump($arr);
?>
--- Source: array.xml ---
$value) {
unset($array[$i]);
}
print_r($array);
// Append an item (note that the new key is 5, instead of 0).
$array[] = 6;
print_r($array);
// Re-index:
$array = array_values($array);
$array[] = 7;
print_r($array);
?>
--- Source: array.xml ---
--- Source: array.xml ---
--- Source: array.xml ---
--- Source: array.xml ---
1, 'bar' => 2, 'baz' => 3];
// Assign the element at index 'baz' to the variable $three
['baz' => $three] = $source_array;
echo $three, PHP_EOL; // prints 3
$source_array = ['foo', 'bar', 'baz'];
// Assign the element at index 2 to the variable $baz
[2 => $baz] = $source_array;
echo $baz, PHP_EOL; // prints "baz"
?>
--- Source: array.xml ---
--- Source: array.xml ---
'one', 2 => 'two', 3 => 'three');
/* will produce an array that would have been defined as
$a = array(1 => 'one', 3 => 'three');
and NOT
$a = array(1 => 'one', 2 =>'three');
*/
unset($a[2]);
var_dump($a);
$b = array_values($a);
// Now $b is array(0 => 'one', 1 =>'three')
var_dump($b);
?>
--- Source: array.xml ---
--- Source: array.xml ---
--- Source: array.xml ---
'apple', 'veggie' => 'carrot');
// Correct
echo $arr['fruit'], PHP_EOL; // apple
echo $arr['veggie'], PHP_EOL; // carrot
// Incorrect. This does not work and throws a PHP Error because
// of an undefined constant named fruit
//
// Error: Undefined constant "fruit"
try {
echo $arr[fruit];
} catch (Error $e) {
echo get_class($e), ': ', $e->getMessage(), PHP_EOL;
}
// This defines a constant to demonstrate what's going on. The value 'veggie'
// is assigned to a constant named fruit.
define('fruit', 'veggie');
// Notice the difference now
echo $arr['fruit'], PHP_EOL; // apple
echo $arr[fruit], PHP_EOL; // carrot
// The following is okay, as it's inside a string. Constants are not looked for
// within strings, so no error occurs here
echo "Hello $arr[fruit]", PHP_EOL; // Hello apple
// With one exception: braces surrounding arrays within strings allows constants
// to be interpreted
echo "Hello {$arr[fruit]}", PHP_EOL; // Hello carrot
echo "Hello {$arr['fruit']}", PHP_EOL; // Hello apple
// Concatenation is another option
echo "Hello " . $arr['fruit'], PHP_EOL; // Hello apple
?>
--- Source: array.xml ---
--- Source: array.xml ---
--- Source: array.xml ---
--- Source: array.xml ---
--- Source: array.xml ---
{1} = null;
}
}
var_export((array) new A());
?>
--- Source: array.xml ---
--- Source: array.xml ---
'd']; // ['a', 'b', 'c' => 'd']
var_dump($arr1, $arr2, $arr3, $arr4, $arr5, $arr6);
?>
--- Source: array.xml ---
1];
$arr2 = ["a" => 2];
$arr3 = ["a" => 0, ...$arr1, ...$arr2];
var_dump($arr3); // ["a" => 2]
// integer key
$arr4 = [1, 2, 3];
$arr5 = [4, 5, 6];
$arr6 = [...$arr4, ...$arr5];
var_dump($arr6); // [1, 2, 3, 4, 5, 6]
// Which is [0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6]
// where the original integer keys have not been retained.
?>
--- Source: array.xml ---
4];
$arr3 = [...$arr1, ...$arr2];
// Fatal error: Uncaught Error: Cannot unpack array with string keys in example.php:5
$arr4 = [1, 2, 3];
$arr5 = [4, 5];
$arr6 = [...$arr4, ...$arr5]; // works. [1, 2, 3, 4, 5]
?>
--- Source: array.xml ---
'red',
'taste' => 'sweet',
'shape' => 'round',
'name' => 'apple',
4 // key will be 0
);
$b = array('a', 'b', 'c');
var_dump($a, $b);
// . . .is completely equivalent with this:
$a = array();
$a['color'] = 'red';
$a['taste'] = 'sweet';
$a['shape'] = 'round';
$a['name'] = 'apple';
$a[] = 4; // key will be 0
$b = array();
$b[] = 'a';
$b[] = 'b';
$b[] = 'c';
// After the above code is executed, $a will be the array
// array('color' => 'red', 'taste' => 'sweet', 'shape' => 'round',
// 'name' => 'apple', 0 => 4), and $b will be the array
// array(0 => 'a', 1 => 'b', 2 => 'c'), or simply array('a', 'b', 'c').
var_dump($a, $b);
?>
--- Source: array.xml ---
4,
'OS' => 'Linux',
'lang' => 'english',
'short_tags' => true
);
var_dump($map);
// strictly numerical keys
// this is the same as array(0 => 7, 1 => 8, ...)
$array = array( 7,
8,
0,
156,
-10
);
var_dump($array);
$switching = array( 10, // key = 0
5 => 6,
3 => 7,
'a' => 4,
11, // key = 6 (maximum of integer-indices was 5)
'8' => 2, // key = 8 (integer!)
'02' => 77, // key = '02'
0 => 12 // the value 10 will be overwritten by 12
);
var_dump($switching);
// empty array
$empty = array();
var_dump($empty);
?>
--- Source: array.xml ---
--- Source: array.xml ---
--- Source: array.xml ---
'January', 'February', 'March');
print_r($firstquarter);
?>
--- Source: array.xml ---
--- Source: array.xml ---
--- Source: array.xml ---
array ( "a" => "orange",
"b" => "banana",
"c" => "apple"
),
"numbers" => array ( 1,
2,
3,
4,
5,
6
),
"holes" => array ( "first",
5 => "second",
"third"
)
);
var_dump($fruits);
// Some examples to address values in the array above
echo $fruits["holes"][5]; // prints "second"
echo $fruits["fruits"]["a"]; // prints "orange"
unset($fruits["holes"][0]); // remove "first"
// Create a new multi-dimensional array
$juices["apple"]["green"] = "good";
var_dump($juices);
?>
--- Source: array.xml ---
--- Source: type-juggling.xml ---
42 // exact type
"42" --> "42" // exact type
new ObjectWithToString --> "Result of __toString()"
// object never compatible with int, fall back to string
42.0 --> 42 // float compatible with int
42.1 --> 42 // float compatible with int
1e100 --> "1.0E+100" // float too large for int type, fall back to string
INF --> "INF" // float too large for int type, fall back to string
true --> 1 // bool compatible with int
[] --> TypeError // array not compatible with int or string
// int|float|bool
"45" --> 45 // int numeric string
"45.0" --> 45.0 // float numeric string
"45X" --> true // not numeric string, fall back to bool
"" --> false // not numeric string, fall back to bool
"X" --> true // not numeric string, fall back to bool
[] --> TypeError // array not compatible with int, float or bool
?>
--- Source: type-juggling.xml ---
--- Source: type-juggling.xml ---
--- Source: type-juggling.xml ---
--- Source: type-juggling.xml ---
--- Source: type-juggling.xml ---
--- Source: declarations.xml ---
--- Source: declarations.xml ---
--- Source: declarations.xml ---
--- Source: declarations.xml ---
--- Source: declarations.xml ---
--- Source: declarations.xml ---
--- Source: declarations.xml ---
--- Source: declarations.xml ---
--- Source: declarations.xml ---
--- Source: declarations.xml ---
--- Source: declarations.xml ---
--- Source: declarations.xml ---
id = $id;
$this->username = $username;
}
}
?>
--- Source: declarations.xml ---
--- Source: declarations.xml ---
--- Source: declarations.xml ---
--- Source: object.xml ---
do_foo();
?>
--- Source: object.xml ---
'foo');
var_dump(isset($obj->{'1'})); // outputs 'bool(true)'
// Deprecated as of PHP 8.1
var_dump(key($obj)); // outputs 'string(1) "1"'
?>
--- Source: object.xml ---
scalar; // outputs 'ciao'
?>
--- Source: null.xml ---
--- Source: iterable.xml ---
--- Source: string.xml ---
--- Source: string.xml ---
foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}
$foo = new foo();
$name = 'MyName';
echo <<foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
?>
--- Source: string.xml ---
--- Source: string.xml ---
--- Source: string.xml ---
--- Source: string.xml ---
foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}
$foo = new foo();
$name = 'MyName';
echo <<<'EOT'
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should not print a capital 'A': \x41
EOT;
?>
--- Source: string.xml ---
--- Source: string.xml ---
--- Source: string.xml ---
string-variable::
variable-name (offset-or-property)?
| ${ expression }
offset-or-property::
offset-in-string
| property-in-string
offset-in-string::
[ name ]
| [ variable-name ]
| [ integer-literal ]
property-in-string::
-> name
variable-name::
$ name
name::
[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*
--- Source: string.xml ---
--- Source: string.xml ---
--- Source: string.xml ---
"purple");
echo "He drank some $juices[0] juice.";
echo PHP_EOL;
echo "He drank some $juices[1] juice.";
echo PHP_EOL;
echo "He drank some $juices[string_key] juice.";
echo PHP_EOL;
class A {
public $s = "string";
}
$o = new A();
echo "Object value: $o->s.";
?>
--- Source: string.xml ---
--- Source: string.xml ---
'Indexed value',
'const-key' => 'Key with minus sign',
'foo' => ['foo1', 'foo2', 'foo3']
];
// Won't work, outputs: This is { fantastic}
echo "This is { $great}";
// Works, outputs: This is fantastic
echo "This is {$great}";
class Square {
public $width;
public function __construct(int $width) { $this->width = $width; }
}
$square = new Square(5);
// Works
echo "This square is {$square->width}00 centimeters wide.";
// Works, quoted keys only work using the curly brace syntax
echo "This works: {$arr['key']}";
// Works
echo "This works: {$arr[3][2]}";
echo "This works: {$arr[DATA_KEY]}";
// When using multidimensional arrays, always use braces around arrays
// when inside of strings
echo "This works: {$arr['foo'][2]}";
echo "This works: {$obj->values[3]->name}";
echo "This works: {$obj->$staticProp}";
// Won't work, outputs: C:\directory\{fantastic}.txt
echo "C:\directory\{$great}.txt";
// Works, outputs: C:\directory\fantastic.txt
echo "C:\\directory\\{$great}.txt";
?>
--- Source: string.xml ---
--- Source: string.xml ---
getMessage(), PHP_EOL;
}
echo PHP_EOL;
}
?>
--- Source: bitwise.xml ---
--- Source: bitwise.xml ---
--- Source: bitwise.xml ---
> $places;
p($res, $val, '>>', $places, 'copy of sign bit shifted into left side');
$val = 4;
$places = 2;
$res = $val >> $places;
p($res, $val, '>>', $places);
$val = 4;
$places = 3;
$res = $val >> $places;
p($res, $val, '>>', $places, 'bits shift out right side');
$val = 4;
$places = 4;
$res = $val >> $places;
p($res, $val, '>>', $places, 'same result as above; can not shift beyond 0');
echo "\n--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---\n";
$val = -4;
$places = 1;
$res = $val >> $places;
p($res, $val, '>>', $places, 'copy of sign bit shifted into left side');
$val = -4;
$places = 2;
$res = $val >> $places;
p($res, $val, '>>', $places, 'bits shift out right side');
$val = -4;
$places = 3;
$res = $val >> $places;
p($res, $val, '>>', $places, 'same result as above; can not shift beyond -1');
echo "\n--- BIT SHIFT LEFT ON POSITIVE INTEGERS ---\n";
$val = 4;
$places = 1;
$res = $val << $places;
p($res, $val, '<<', $places, 'zeros fill in right side');
$val = 4;
$places = (PHP_INT_SIZE * 8) - 4;
$res = $val << $places;
p($res, $val, '<<', $places);
$val = 4;
$places = (PHP_INT_SIZE * 8) - 3;
$res = $val << $places;
p($res, $val, '<<', $places, 'sign bits get shifted out');
$val = 4;
$places = (PHP_INT_SIZE * 8) - 2;
$res = $val << $places;
p($res, $val, '<<', $places, 'bits shift out left side');
echo "\n--- BIT SHIFT LEFT ON NEGATIVE INTEGERS ---\n";
$val = -4;
$places = 1;
$res = $val << $places;
p($res, $val, '<<', $places, 'zeros fill in right side');
$val = -4;
$places = (PHP_INT_SIZE * 8) - 3;
$res = $val << $places;
p($res, $val, '<<', $places);
$val = -4;
$places = (PHP_INT_SIZE * 8) - 2;
$res = $val << $places;
p($res, $val, '<<', $places, 'bits shift out left side, including sign bit');
/*
* Ignore this bottom section,
* it is just formatting to make output clearer.
*/
function p($res, $val, $op, $places, $note = '') {
$format = '%0' . (PHP_INT_SIZE * 8) . "b\n";
printf("Expression: %d = %d %s %d\n", $res, $val, $op, $places);
echo " Decimal:\n";
printf(" val=%d\n", $val);
printf(" res=%d\n", $res);
echo " Binary:\n";
printf(' val=' . $format, $val);
printf(' res=' . $format, $res);
if ($note) {
echo " NOTE: $note\n";
}
echo "\n\n";
}
?>
--- Source: type.xml ---
--- Source: type.xml ---
--- Source: type.xml ---
--- Source: type.xml ---
--- Source: type.xml ---
--- Source: type.xml ---
--- Source: type.xml ---
--- Source: type.xml ---
--- Source: logical.xml ---
--- Source: arithmetic.xml ---
--- Source: array.xml ---
"apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");
$c = $a + $b; // Union of $a and $b
echo "Union of \$a and \$b: \n";
var_dump($c);
$c = $b + $a; // Union of $b and $a
echo "Union of \$b and \$a: \n";
var_dump($c);
$a += $b; // Union of $a += $b is $a and $b
echo "Union of \$a += \$b: \n";
var_dump($a);
?>
--- Source: array.xml ---
"banana", "0" => "apple");
var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false)
?>
--- Source: execution.xml ---
$output";
?>
--- Source: functional.xml ---
strlen(...);
echo $result, PHP_EOL;
$result = strlen("Hello World");
echo $result, PHP_EOL;
?>
--- Source: functional.xml ---
htmlentities(...)
|> str_split(...)
|> (fn($x) => array_map(strtoupper(...), $x))
|> (fn($x) => array_filter($x, fn($v) => $v != 'O'))
;
print_r($result);
$temp = "PHP Rocks";
$temp = htmlentities($temp);
$temp = str_split($temp);
$temp = array_map(strtoupper(...), $temp);
$temp = array_filter($temp, fn($v) => $v != 'O');
$result = $temp;
print_r($result);
?>
--- Source: comparison.xml ---
--- Source: comparison.xml ---
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
echo "a" <=> "aa", ' '; // -1
echo "zz" <=> "aa", ' '; // 1
// Arrays
echo [] <=> [], ' '; // 0
echo [1, 2, 3] <=> [1, 2, 3], ' '; // 0
echo [1, 2, 3] <=> [], ' '; // 1
echo [1, 2, 3] <=> [1, 2, 1], ' '; // 1
echo [1, 2, 3] <=> [1, 2, 4], ' '; // -1
// Objects
$a = (object) ["a" => "b"];
$b = (object) ["a" => "b"];
echo $a <=> $b, ' '; // 0
$a = (object) ["a" => "b"];
$b = (object) ["a" => "c"];
echo $a <=> $b, ' '; // -1
$a = (object) ["a" => "c"];
$b = (object) ["a" => "b"];
echo $a <=> $b, ' '; // 1
// not only values are compared; keys must match
$a = (object) ["a" => "b"];
$b = (object) ["b" => "b"];
echo $a <=> $b, ' '; // 1
?>
--- Source: comparison.xml ---
--- Source: comparison.xml ---
count($op2)) {
return 1; // $op1 > $op2
}
foreach ($op1 as $key => $val) {
if (!array_key_exists($key, $op2)) {
return 1;
} elseif ($val < $op2[$key]) {
return -1;
} elseif ($val > $op2[$key]) {
return 1;
}
}
return 0; // $op1 == $op2
}
?>
--- Source: comparison.xml ---
--- Source: comparison.xml ---
--- Source: comparison.xml ---
--- Source: comparison.xml ---
--- Source: comparison.xml ---
--- Source: comparison.xml ---
--- Source: precedence.xml ---
$a = 5, $b = 5
var_dump($a, $b);
?>
--- Source: precedence.xml ---
--- Source: precedence.xml ---
--- Source: precedence.xml ---
--- Source: precedence.xml ---
--- Source: errorcontrol.xml ---
--- Source: errorcontrol.xml ---
--- Source: string.xml ---
--- Source: increment.xml ---
--- Source: increment.xml ---
--- Source: increment.xml ---
--- Source: assignment.xml ---
--- Source: assignment.xml ---
--- Source: assignment.xml ---
--- Source: assignment.xml ---
--- Source: incompatible.xml ---
--- Source: incompatible.xml ---
getMessage(), "\n";
}
?>
--- Source: new-features.xml ---
--- Source: new-features.xml ---
'b'];
$arr2 = [...$arr1, 'c' => 'd']; //[1, 'a' => 'b', 'c' => 'd']
?>
--- Source: new-features.xml ---
$file]);
?>
--- Source: new-features.xml ---
42]);
echo $h, "\n";
?>
--- Source: new-features.xml ---
42]);
echo $h, "\n";
?>
--- Source: new-features.xml ---
"at least 136 bytes long secret here"]);
echo $h, "\n";
?>
--- Source: new-features.xml ---
prepare('INSERT INTO users(id, name) VALUES(?,?)');
$stmt->execute([1, $username]);
?>
--- Source: new-features.xml ---
query('SELECT username FROM users WHERE id = 123');
echo $result->fetch_column();
?>
--- Source: new-features.xml ---
--- Source: deprecated.xml ---
--- Source: deprecated.xml ---
--- Source: deprecated.xml ---
--- Source: deprecated.xml ---
--- Source: deprecated.xml ---
--- Source: incompatible.xml ---
1];
$obj = (object) $arr;
var_dump(
$obj,
$obj->{'0'}, // now accessible
$obj->{0} // now accessible
);
--- Source: incompatible.xml ---
{0} = 1;
}
};
$arr = (array) $obj;
var_dump(
$arr,
$arr[0], // now accessible
$arr['0'] // now accessible
);
--- Source: incompatible.xml ---
quote('über', PDO::PARAM_STR | PDO::PARAM_STR_NATL);
--- Source: new-features.xml ---
--- Source: incompatible.xml ---
--- Source: incompatible.xml ---
--- Source: incompatible.xml ---
--- Source: incompatible.xml ---
123;
}
foo(...gen());
?>
--- Source: new-features.xml ---
--- Source: new-features.xml ---
\w+)', '国', $matches);
// => [0 => "国", 1 => "国", "word" => "国"];
?>
--- Source: new-features.xml ---
\w+)\s*', "_\k_\k'word'_", ' foo ');
// => "_foo_foo_"
?>
--- Source: new-features.xml ---
--- Source: new-features.xml ---
$n * $factor, [1, 2, 3, 4]);
// $nums = array(10, 20, 30, 40);
?>
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
&1 on the shell
proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['redirect', 1]], $pipes);
// Like 2>/dev/null or 2>nul on the shell
proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['null']], $pipes);
?>
--- Source: deprecated.xml ---
--- Source: deprecated.xml ---
1 ? 2 ? 3 : 4 : 5 // ok
--- Source: deprecated.xml ---
--- Source: incompatible.xml ---
--- Source: incompatible.xml ---
--- Source: incompatible.xml ---
--- Source: incompatible.xml ---
ParentClass@anonymous
new class implements FirstInterface, SecondInterface {};
// -> FirstInterface@anonymous
new class {};
// -> class@anonymous
?>
--- Source: incompatible.xml ---
--- Source: incompatible.xml ---
--- Source: incompatible.xml ---
--- Source: incompatible.xml ---
--- Source: incompatible.xml ---
['protocol_version' => '1.0']]);
echo file_get_contents('http://example.org', false, $ctx);
?>
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
throw new Exception('Exception in arrow function');
$user = $session->user ?? throw new Exception('Must have user');
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: deprecated.xml ---
--- Source: deprecated.xml ---
--- Source: deprecated.xml ---
$a > $b);
// With
usort($array, fn($a, $b) => $a <=> $b);
?>
--- Source: deprecated.xml ---
statIndex($i); $i++) {
echo $entry['name'];
}
?>
--- Source: openssl.xml ---
[
'capture_session_meta' => TRUE
]]);
$html = file_get_contents('https://google.com/', FALSE, $ctx);
$meta = stream_context_get_options($ctx)['ssl']['session_meta'];
var_dump($meta);
?>
--- Source: openssl.xml ---
openssl dhparam -out /path/to/my/certs/dh-2048.pem 2048
--- Source: openssl.xml ---
[
'crypto_method' => STREAM_CRYPTO_METHOD_TLS_CLIENT,
],
]);
$html = file_get_contents('https://google.com/', false, $ctx);
// Requiring TLS 1.1 or 1.2:
$ctx = stream_context_create([
'ssl' => [
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT |
STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
],
]);
$html = file_get_contents('https://google.com/', false, $ctx);
// Connecting using the tlsv1.2:// stream socket transport.
$sock = stream_socket_client('tlsv1.2://google.com:443/');
?>
--- Source: openssl.xml ---
--- Source: openssl.xml ---
--- Source: openssl.xml ---
--- Source: openssl.xml ---
--- Source: openssl.xml ---
--- Source: incompatible.xml ---
'foo',
'bar',
'quux',
];
}
var_dump((new C)->array);
?>
--- Source: new-features.xml ---
f()."\n";
echo C::SENTENCE;
?>
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
prop = $val;
}
public function __debugInfo() {
return [
'propSquared' => $this->prop ** 2,
];
}
}
var_dump(new C(42));
?>
--- Source: deprecated.xml ---
f();
?>
--- Source: incompatible.xml ---
--- Source: incompatible.xml ---
--- Source: incompatible.xml ---
--- Source: incompatible.xml ---
1])));
--- Source: other-changes.xml ---
1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
// list() style
list("id" => $id1, "name" => $name1) = $data[0];
// [] style
["id" => $id1, "name" => $name1] = $data[0];
// list() style
foreach ($data as list("id" => $id, "name" => $name)) {
// logic here with $id and $name
}
// [] style
foreach ($data as ["id" => $id, "name" => $name]) {
// logic here with $id and $name
}
--- Source: new-features.xml ---
--- Source: new-features.xml ---
exposeFunction();
$privFunc('some value');
--- Source: new-features.xml ---
strlen(...);
print $result . PHP_EOL; // Prints "11"
--- Source: new-features.xml ---
", "?", "@", "A"], as of PHP 8.3.0
range('9', 'A'); // [9, 8, 7, 6, 5, 4, 3, 2, 1, 0], prior to PHP 8.3.0
?>
--- Source: new-features.xml ---
123";
$xml = simplexml_load_string($xmlString);
$nodes = $xml->a->b;
foreach ($nodes as $nodeData) {
echo "nodeData: " . $nodeData . "\n";
$xml = $nodes->asXml();
}
--- Source: new-features.xml ---
$this->firstName . ' ' . $this->lastName;
}
// All write operations go through this hook, and the result is what is written.
// Read access happens normally.
public string $firstName {
set => ucfirst(strtolower($value));
}
// All write operations go through this hook, which has to write to the backing value itself.
// Read access happens normally.
public string $lastName {
set {
if (strlen($value) < 2) {
throw new \InvalidArgumentException('Too short');
}
$this->lastName = $value;
}
}
}
$p = new Person();
$p->firstName = 'peter';
print $p->firstName; // Prints "Peter"
$p->lastName = 'Peterson';
print $p->fullName; // Prints "Peter Peterson"
--- Source: new-features.xml ---
name = $name;
}
}
--- Source: new-features.xml ---
__construct($data);
};
$reflector = new ReflectionClass(Example::class);
$object = $reflector->newLazyGhost($initializer);
--- Source: deprecated.xml ---
private()->for('purpose here')->with('username here');
?>
--- Source: new-features.xml ---
--- Source: new-features.xml ---
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
?>
--- Source: new-features.xml ---
--- Source: new-features.xml ---
logger;
}
public function setLogger(Logger $logger) {
$this->logger = $logger;
}
}
$app = new Application;
$app->setLogger(new class implements Logger {
public function log(string $msg) {
echo $msg;
}
});
var_dump($app->getLogger());
?>
--- Source: new-features.xml ---
--- Source: new-features.xml ---
x;};
$getXCB = $getX->bindTo(new A, 'A'); // intermediate closure
echo $getXCB();
// PHP 7+ code
$getX = function() {return $this->x;};
echo $getX->call(new A);
--- Source: new-features.xml ---
false]);
// converts all objects into __PHP_Incomplete_Class object except those of MyClass and MyClass2
$data = unserialize($foo, ["allowed_classes" => ["MyClass", "MyClass2"]]);
// default behaviour (same as omitting the second argument) that accepts all classes
$data = unserialize($foo, ["allowed_classes" => true]);
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
getReturn(), PHP_EOL;
--- Source: new-features.xml ---
--- Source: new-features.xml ---
--- Source: new-features.xml ---
'private',
'read_and_close' => true,
]);
?>
--- Source: deprecated.xml ---
--- Source: deprecated.xml ---
--- Source: other.xml ---
--- Source: other.xml ---
callNonStaticMethodOfA();
?>
--- Source: other.xml ---
--- Source: other.xml ---
--- Source: other.xml ---
--- Source: other.xml ---
--- Source: strings.xml ---
--- Source: strings.xml ---
--- Source: foreach.xml ---
--- Source: foreach.xml ---
--- Source: variable-handling.xml ---
bar;
// Valid in PHP 5 and 7.
global ${$foo->bar};
}
?>
--- Source: variable-handling.xml ---
--- Source: variable-handling.xml ---
--- Source: variable-handling.xml ---
--- Source: variable-handling.xml ---
--- Source: error-handling.xml ---
--- Source: integers.xml ---
> -1);
?>
--- Source: integers.xml ---
--- Source: examples.xml ---
--- Source: intl-get-error-message.xml ---
--- Source: intl-is-failure.xml ---
--- Source: intl-get-error-code.xml ---
' );
if( !$coll ) {
handle_error( intl_get_error_code() );
}
?>
--- Source: intl-error-name.xml ---
--- Source: examples.xml ---
output "as is"
echo $text;
break;
}
}
}
?>
--- Source: examples.xml ---
--- Source: examples.xml ---
--- Source: examples.xml ---
--- Source: examples.xml ---
varname = $url["host"];
$this->position = 0;
return true;
}
function stream_read($count)
{
$ret = substr($GLOBALS[$this->varname], $this->position, $count);
$this->position += strlen($ret);
return $ret;
}
function stream_write($data)
{
$left = substr($GLOBALS[$this->varname], 0, $this->position);
$right = substr($GLOBALS[$this->varname], $this->position + strlen($data));
$GLOBALS[$this->varname] = $left . $data . $right;
$this->position += strlen($data);
return strlen($data);
}
function stream_tell()
{
return $this->position;
}
function stream_eof()
{
return $this->position >= strlen($GLOBALS[$this->varname]);
}
function stream_seek($offset, $whence)
{
switch ($whence) {
case SEEK_SET:
if ($offset < strlen($GLOBALS[$this->varname]) && $offset >= 0) {
$this->position = $offset;
return true;
} else {
return false;
}
break;
case SEEK_CUR:
if ($offset >= 0) {
$this->position += $offset;
return true;
} else {
return false;
}
break;
case SEEK_END:
if (strlen($GLOBALS[$this->varname]) + $offset >= 0) {
$this->position = strlen($GLOBALS[$this->varname]) + $offset;
return true;
} else {
return false;
}
break;
default:
return false;
}
}
function stream_metadata($path, $option, $var)
{
if($option == STREAM_META_TOUCH) {
$url = parse_url($path);
$varname = $url["host"];
if(!isset($GLOBALS[$varname])) {
$GLOBALS[$varname] = '';
}
return true;
}
return false;
}
}
stream_wrapper_register("var", "VariableStream")
or die("Failed to register protocol");
$myvar = "";
$fp = fopen("var://myvar", "r+");
fwrite($fp, "line1\n");
fwrite($fp, "line2\n");
fwrite($fp, "line3\n");
rewind($fp);
while (!feof($fp)) {
echo fgets($fp);
}
fclose($fp);
var_dump($myvar);
?>
--- Source: zend-thread-id.xml ---
--- Source: examples.xml ---
My lists
My books
Title
Author
Language
ISBN
The Grapes of Wrath
John Steinbeck
en
0140186409
The Pearl
John Steinbeck
en
014017737X
Samarcande
Amine Maalouf
fr
2253051209
--- Source: examples.xml ---
edible = $edible;
$this->color = $color;
}
public function isEdible()
{
return $this->edible;
}
public function getColor()
{
return $this->color;
}
}
?>
--- Source: examples.xml ---
cooked = true;
}
public function isCooked()
{
return $this->cooked;
}
}
?>
--- Source: examples.xml ---
$val) {
echo "\t$prop = $val\n";
}
}
function printMethods($obj)
{
$arr = get_class_methods(get_class($obj));
foreach ($arr as $method) {
echo "\tfunction $method()\n";
}
}
function objectBelongsTo($obj, $class)
{
if (is_subclass_of($obj, $class)) {
echo "Object belongs to class " . get_class($obj);
echo ", a subclass of $class\n";
} else {
echo "Object does not belong to a subclass of $class\n";
}
}
// instantiate 2 objects
$veggie = new Vegetable(true, "blue");
$leafy = new Spinach();
// print out information about objects
echo "veggie: CLASS " . get_class($veggie) . "\n";
echo "leafy: CLASS " . get_class($leafy);
echo ", PARENT " . get_parent_class($leafy) . "\n";
// show veggie properties
echo "\nveggie: Properties\n";
printProperties($veggie);
// and leafy methods
echo "\nleafy: Methods\n";
printMethods($leafy);
echo "\nParentage:\n";
objectBelongsTo($leafy, Spinach::class);
objectBelongsTo($leafy, Vegetable::class);
?>