隨機整數或者字節
random_int()
random_bytes()
當隨機的熵不夠的時候會拋出異常,能夠根據異常自定一些處理。php
try {
$bytes = random_bytes(42);
} catch (Exception $e) {
$cryptoStrong = false;
$bytes = openssl_random_pseudo_bytes(42, $cryptoStrong);
}
function add(int $left, int $right) {
return $left + $right;
}
try {
echo add('left', 'right');
} catch (Exception $e) {
// Handle exception
} catch (TypeException $e) {
// Appears to descend from Exception
// Log error and end gracefully
}
$code = preg_replace_callback_array(
array(
"/EXECUTE_DATA/m" =>
function($matches) {
return "execute_data";
},
"/ZEND_VM_DISPATCH_TO_HANDLER\(\s*([A-Z_]*)\s*\)/m" =>
function($matches) use ($spec, $prefix, $op1, $op2) {
return "return " . $matches[1] . ($spec?"_SPEC":"") . $prefix[$op1] . $prefix[$op2] . "_HANDLER(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU)";
},
"/ZEND_VM_DISPATCH_TO_HELPER\(\s*([A-Za-z_]*)\s*\)/m" =>
function($matches) use ($spec, $prefix, $op1, $op2) {
return "return " . helper_name($matches[1], $spec, $op1, $op2) . "(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU)";
},
"/ZEND_VM_DISPATCH_TO_HELPER_EX\(\s*([A-Za-z_]*)\s*,\s*[A-Za-z_]*\s*,\s*(.*)\s*\);/m" =>
function($matches) use ($spec, $prefix, $op1, $op2) {
return "return " . helper_name($matches[1], $spec, $op1, $op2) . "(" . $matches[2]. ", ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);";
},
), $code);
php -r '$a = [1,2,3]; foreach($a as $v) {echo $v . " - " . current($a) . "\n";}'
echo json_encode(10.0); // Output 10
echo json_encode(10.1); // Output 10.1
echo json_encode(10.0, JSON_PRESERVE_ZERO_FRACTION); // Output 10.0
echo json_encode(10.1, JSON_PRESERVE_ZERO_FRACTION); // Output 10.1
var_dump(json_decode(json_encode(10.0, JSON_PRESERVE_ZERO_FRACTION))); // Output double(10)
var_dump(10.0 === json_decode(json_encode(10.0, JSON_PRESERVE_ZERO_FRACTION))); // Output bool(true)
$username = $_GET['user'] ?? 'nobody';
$ php -r 'list($a,$b) = "aa";var_dump($a,$b);'
NULL
NULL
$ php -r '$a[0]="ab"; list($a,$b) = $a[0]; var_dump($a,$b);'
string(1) "a"
string(1) "b"
set_error_handler(function($code, $message) {
var_dump($code, $message);
});
$x= null;
var_dump($x->method());
echo "Alive\n";
//反序列全部的東西
$data = unserialize($foo);
//反序列化全部對象
$data = unserialize($foo, false);
//反序列化除了這兩個對象之外的
$data = unserialize($foo, array("MyClass", "MyClass2"));
// Current use syntax:
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion as Choice;
use Symfony\Component\Console\Question\ConfirmationQuestion;
// Proposed group use syntax:
use Symfony\Component\Console\{
Helper\Table,
Input\ArrayInput,
Input\InputInterface,
Output\NullOutput,
Output\OutputInterface,
Question\Question,
Question\ChoiceQuestion as Choice,
Question\ConfirmationQuestion,
};
function call_method($obj) {
$obj->method();
}
try {
call_method(null); // oops!
} catch (EngineException $e) {
echo "Exception: {$e->getMessage()}\n";
}
echo 3%%2;
//輸出1