<?php
declare(strict_types=1);
class Car
{
public string $color;
public function __construct(string $color = 'green')
{
$this->color = $color;
}
public function whatColor(): string
{
return $this->color;
}
}
function captureDump(mixed $value): string
{
ob_start();
var_dump($value);
return trim(str_replace("\r\n", "\n", (string) ob_get_clean()));
}
$cases = [];
// Case 1: String (single/double quotes) and Boolean types
$sDouble = "Hello world!";
$sSingle = 'Hello world!';
$bTrue = true;
$bFalse = false;
$sbActual = [
'strings_identical' => $sDouble === $sSingle,
'string_type' => gettype($sDouble),
'bool_true_dump' => captureDump($bTrue),
'bool_false_dump' => captureDump($bFalse),
'branch_result' => $bTrue && !$bFalse ? 'yes' : 'no',
];
$sbExpected = [
'strings_identical' => true,
'string_type' => 'string',
'bool_true_dump' => 'bool(true)',
'bool_false_dump' => 'bool(false)',
'branch_result' => 'yes',
];
$cases[] = [
'name' => 'string_and_boolean_types',
'passed' => $sbActual === $sbExpected,
'expected' => $sbExpected,
'actual' => $sbActual,
];
// Case 2: Integer bases (dec, neg, hex, octal, PHP 8.1+ 0o) and Float notation
$numActual = [
'dec' => captureDump(5985),
'neg' => captureDump(-345),
'hex' => captureDump(0x8C),
'oct_legacy' => captureDump(047),
'oct_php81' => captureDump(0o47),
'float_dec' => captureDump(10.365),
'float_exp_pos' => captureDump(2.4e3),
'float_exp_neg' => captureDump(8E-5),
];
$numExpected = [
'dec' => 'int(5985)',
'neg' => 'int(-345)',
'hex' => 'int(140)',
'oct_legacy' => 'int(39)',
'oct_php81' => 'int(39)',
'float_dec' => 'float(10.365)',
'float_exp_pos' => 'float(2400)',
'float_exp_neg' => 'float(8.0E-5)',
];
$cases[] = [
'name' => 'integer_bases_and_float_notation',
'passed' => $numActual === $numExpected,
'expected' => $numExpected,
'actual' => $numActual,
];
// Case 3: Array and Object types ($this binding in Car class)
$cars = ["Volvo", "BMW", "Toyota"];
$defaultCar = new Car();
$blueCar = new Car('blue');
$aoActual = [
'array_count' => count($cars),
'array_type' => gettype($cars),
'first_car' => $cars[0],
'object_type' => gettype($defaultCar),
'default_color' => $defaultCar->whatColor(),
'custom_color' => $blueCar->whatColor(),
];
$aoExpected = [
'array_count' => 3,
'array_type' => 'array',
'first_car' => 'Volvo',
'object_type' => 'object',
'default_color' => 'green',
'custom_color' => 'blue',
];
$cases[] = [
'name' => 'array_and_object_types',
'passed' => $aoActual === $aoExpected,
'expected' => $aoExpected,
'actual' => $aoActual,
];
// Case 4: NULL clearing vs empty string
$val = "Hello world!";
$val = null;
$nullActual = [
'dump' => captureDump($val),
'type' => gettype($val),
'is_null' => is_null($val),
'strict_not_empty_str' => $val !== '',
];
$nullExpected = [
'dump' => 'NULL',
'type' => 'NULL',
'is_null' => true,
'strict_not_empty_str' => true,
];
$cases[] = [
'name' => 'null_clearing_vs_empty_string',
'passed' => $nullActual === $nullExpected,
'expected' => $nullExpected,
'actual' => $nullActual,
];
// Case 5: Resource stream handle, get_resource_type(), TypeError on non-resource, and PHP 8 GdImage object
$fp = fopen('php://memory', 'r+');
$isRes = is_resource($fp);
$resType = get_resource_type($fp);
fclose($fp);
$invalidThrown = false;
try {
/** @noinspection PhpParamsInspection */
get_resource_type('not-a-resource');
} catch (TypeError) {
$invalidThrown = true;
}
$gd = imagecreatetruecolor(4, 4);
$resActual = [
'stream_is_resource' => $isRes,
'stream_resource_type' => $resType,
'non_resource_throws_type_error' => $invalidThrown,
'gd_is_object_in_php8' => $gd instanceof GdImage && !is_resource($gd),
];
$resExpected = [
'stream_is_resource' => true,
'stream_resource_type' => 'stream',
'non_resource_throws_type_error' => true,
'gd_is_object_in_php8' => true,
];
$cases[] = [
'name' => 'resource_stream_and_php8_objects',
'passed' => $resActual === $resExpected,
'expected' => $resExpected,
'actual' => $resActual,
];
echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、PHP 8.4 本地 CLI 环境。
入口文件:projects/php-datatypes-lab/explore-datatypes.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-datatypes.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1236.zip