从现在开始,我们要努力学习啦!今天我给大家带来《PHP 数据类型》,感兴趣的朋友请继续看下去吧!下文中的内容我们主要会涉及到等等知识点,如果在阅读本文过程中有遇到不清楚的地方,欢迎留言呀!我们一起讨论,一起学习!

整数
整数表示整数,包括正数和负数,不带任何小数点。 php 支持十进制、十六进制、八进制和二进制整数。
示例:
$num1 = 42; // decimal $num2 = 0x1a; // hexadecimal $num3 = 0123; // octal $num4 = 0b101; // binary
php 提供了各种操作整数的操作,例如加法、减法、乘法和除法。它还提供二进制整数的按位运算。
布尔值
布尔值表示逻辑真或假值。常用于条件和控制流。
true 的计算结果为 1,false 的计算结果为空字符串或 0。
示例:
$isvalid = true; $isempty = false;
布尔值通常与比较运算符(如 ==、!=、===)和逻辑运算符(&&、||、!)一起使用。
无效的
空数据类型表示不存在值。任何设置为 null 的变量都不会分配任何值。
示例:
$value = null;
使用 unset() 转换为 null 已在 php 7.2 中弃用,并在 php 8.0 中删除,这意味着应该避免这种做法。
漂浮
php 中的浮点数(float)用于表示实数,包括带小数或科学计数法的数字。
语法:
$a = 1.234; // simple decimal $b = 1.2e3; // scientific notation $c = 7e-10; // small float in scientific notation $d = 1_234.567; // using underscores for readability (php 7.4+)
php 的浮点数与平台相关,但通常遵循 64 位 ieee 格式。这允许最大值约为 1.8e308,精度约为 14 位小数。
浮点精度问题:
浮点数的精度有限,例如:
地板((0.1 + 0.7) * 10); // 输出 7 而不是 8
出现这种情况是因为像 0.1 和 0.7 这样的数字无法用二进制精确表示,从而导致一些小误差。切勿依赖浮点数的精确比较;相反,使用 epsilon 值进行比较:
$a = 1.23456789;
$b = 1.23456780;
$epsilon = 0.00001;
if(abs($a - $b) < $epsilon) {
echo "true";
}
字符串
字符串是 php 中用于存储和操作文本的字符序列。可以使用单引号 (') 或双引号 (") 以多种方式定义字符串。
语法:
$str1 = 'hello, world!'; // single-quoted string $str2 = "hello, $name!"; // double-quoted string with variable interpolation
php 支持多行字符串的 heredoc 和 nowdoc。
heredoc 示例:
$text = <<<eod this is a string that spans multiple lines. eod;
nowdoc 示例:
$text = <<<'eod' this is a nowdoc example. eod;
常用操作:
连接: .运算符用于连接两个字符串。
$fullname = $firstname 。 ” “。 $lastname;
字符串函数:strlen()、strpos()、substr()等
数组
php 中的数组可以在单个变量中存储多个值,是最通用的数据类型之一。 php 数组可以是索引的或关联的。
语法:
// indexed array $arr = [1, 2, 3, 4]; // associative array $arrassoc = ["name" => "john", "age" => 25];
常用操作:
添加元素:
$arr[] = 5; // adds an element to the end of the array
访问元素:
echo $arr[0]; // outputs: 1 echo $arrassoc['name']; // outputs: john
数组函数:count()、array_merge()、in_array()、array_pop()等
对象
php 中的对象表示用户定义类的实例。类是创建具有属性和方法的对象的蓝图。
语法:
class person {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function greet() {
return "hello, my name is " . $this->name;
}
}
$person = new person("john", 30);
echo $person->greet(); // outputs: hello, my name is john
关键概念:
属性:类内的变量。
方法:类内的函数。
$this:指当前对象实例。
可调用
php 中的可调用对象是指任何可以作为函数调用的变量。它可以是包含函数名称的字符串、包含对象和方法的数组或匿名函数(闭包)。
语法:
function sayhello($name) {
return "hello, $name!";
}
$callable = 'sayhello';
echo $callable("world"); // outputs: hello, world!
可调用数组示例:
class greeter {
public function greet($name) {
return "hello, $name!";
}
}
$greeter = new greeter();
$callable = [$greeter, 'greet'];
echo call_user_func($callable, 'john'); // outputs: hello, john!
匿名函数(闭包):
$callable = function($name) {
return "hi, $name!";
};
echo $callable("jane"); // outputs: hi, jane!
资源
资源是保存对外部资源(例如数据库连接或文件处理程序)的引用的特殊变量。这些不是原始数据类型,而是指向 php 可以与之交互的外部资源的指针。
语法:
// file resource
$handle = fopen("file.txt", "r");
资源通常与返回和操作它们的函数一起使用,例如数据库连接 (mysqli_connect())、图像操作 (imagecreatefromjpeg()) 或文件操作 (fopen())。
示例:
$connection = mysqli_connect("localhost", "username", "password", "database");
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully!";
谢谢你......
