模式切换
数据类型
PHP 支持以下几种数据类型:
- String(字符串)
- Integer(整型)
- Float(浮点型)
- Boolean(布尔型)
- Array(数组)
- Object(对象)
- NULL(空值)
- Resource(资源类型)
字符串
创建字符串
在 PHP 中,字符串可以用单引号或双引号括起来。
php
$str1 = 'Hello, world!';
$str2 = "Hello, world!";
字符串连接
可以使用 .
运算符将两个字符串连接在一起。
php
$str1 = "Hello, ";
$str2 = "World!";
echo $str1 . $str2; // 输出: Hello, World!
strlen
获取字符串的长度。
php
$str = "Hello, World!";
echo strlen($str); // 输出: 13
str_replace
替换字符串中的某些字符。
php
$text = "The quick brown fox jumps over the lazy dog.";
$newText = str_replace("fox", "cat", $text);
echo $newText; // 输出: The quick brown cat jumps over the lazy dog.
substr
截取字符串的一部分。
php
$text = "Hello, World!";
echo substr($text, 7, 5); // 输出: World
其他常用字符串函数
strpos
:查找子字符串首次出现的位置。strtolower
和strtoupper
:将字符串转换为小写或大写。trim
:移除字符串首尾的空白字符。
整型
整型是没有小数的数字。
整数规则:
- 整数必须至少有一个数字 (0-9)
- 整数不能包含逗号或空格
- 整数是没有小数点的
- 整数可以是正数或负数
- 整型可以用三种格式来指定:十进制, 十六进制( 以 0x 为前缀)或八进制(前缀为 0)
php
$int1 = 123;
$int2 = -123;
$int3 = 0x1A; // 十六进制
$int4 = 0123; // 八进制
浮点型
浮点型是带小数的数字,或是指数形式。
php
$float1 = 1.234;
$float2 = 1.2e3;
$float3 = 7E-10;
布尔型
布尔型表示真或假。布尔值是不区分大小写的。
php
$x = true;
$y = false;
数组
数组基础
数组在一个变量中存储多个值。
php
$cars = array("Volvo", "BMW", "Toyota");
索引数组
使用数字索引访问元素的数组,索引从 0
开始。
php
$fruits = ["Apple", "Banana", "Orange"];
echo $fruits[0]; // 输出: Apple
关联数组
使用键值对来存储数据,键可以是字符串或整数。
php
$person = [
"name" => "Alice",
"age" => 25,
"city" => "New York"
];
echo $person["name"]; // 输出: Alice
数组操作
增加
在数组末尾添加元素(索引数组):
php
$colors = ["Red", "Green"];
$colors[] = "Blue"; // 添加 "Blue" 到数组末尾
使用键值对添加元素(关联数组):
php
$person["email"] = "alice@example.com"; // 添加新的键值对
删除
使用 unset()
删除数组中的某个元素。
php
unset($fruits[1]); // 删除索引为 1 的元素 "Banana"
修改
直接通过索引或键修改元素的值。
php
$fruits[0] = "Grapes"; // 将索引为 0 的元素 "Apple" 改为 "Grapes"
$person["age"] = 26; // 更新年龄为 26
查询
通过索引或键访问数组元素。
php
echo $fruits[0]; // 输出: Grapes
echo $person["city"]; // 输出: New York
数组排序
sort()
:对索引数组进行升序排序。php$numbers = [4, 2, 8, 1]; sort($numbers); print_r($numbers); // 输出: [1, 2, 4, 8]
rsort()
:对索引数组进行降序排序。phprsort($numbers); print_r($numbers); // 输出: [8, 4, 2, 1]
asort()
:对关联数组根据值进行升序排序,保持键与值的关系。php$ages = ["Peter" => 35, "John" => 22, "Alice" => 28]; asort($ages); print_r($ages); // 输出: ["John" => 22, "Alice" => 28, "Peter" => 35]
ksort()
:对关联数组根据键进行升序排序。phpksort($ages); print_r($ages); // 输出: ["Alice" => 28, "John" => 22, "Peter" => 35]
多维数组
定义多维数组
多维数组是包含其他数组的数组,可以是二维、三维或更高维度。
php
$matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
访问多维数组元素
php
echo $matrix[1][2]; // 输出: 6
遍历多维数组
使用嵌套的 foreach
循环遍历。
php
foreach ($matrix as $row) {
foreach ($row as $value) {
echo $value . " ";
}
echo "\n";
}
常用数组函数
array_merge()
:合并一个或多个数组。php$array1 = ["a", "b"]; $array2 = ["c", "d"]; $result = array_merge($array1, $array2); print_r($result); // 输出: ["a", "b", "c", "d"]
array_push()
:向数组末尾添加一个或多个元素。php$stack = ["apple", "banana"]; array_push($stack, "orange", "grape"); print_r($stack); // 输出: ["apple", "banana", "orange", "grape"]
array_pop()
:弹出数组末尾的元素。php$fruit = array_pop($stack); echo $fruit; // 输出: grape
array_filter()
:使用回调函数过滤数组元素。php$numbers = [1, 2, 3, 4, 5]; $even = array_filter($numbers, function($num) { return $num % 2 === 0; }); print_r($even); // 输出: [2, 4]
in_array()
:检查数组中是否存在某个值。php$colors = ["red", "green", "blue"]; if (in_array("green", $colors)) { echo "Green is in the array."; }
array_key_exists()
:检查指定的键是否存在于数组中。php$person = ["name" => "Alice", "age" => 25]; if (array_key_exists("age", $person)) { echo "Age is set."; }
count()
:返回数组中的元素个数。phpecho count($colors); // 输出: 3
对象
对象是存储数据和有关如何处理数据的信息的数据类型。
$this 是一个指向对象自身的指针,用于访问对象的属性和方法。
php
class Car {
var $color;
function __construct($color = "green") {
$this->color = $color;
}
function what_color() {
return $this->color;
}
}
$myCar = new Car("red");
echo $myCar->what_color(); // 输出: red
NULL
NULL 表示变量没有值。NULL 是数据类型为 NULL 的值。
php
$x = null;
资源类型
资源是一种特殊变量,保存对外部资源的引用。资源是通过专门的函数来建立和使用的。
常见资源数据类型有打开文件、数据库连接、图形画布区域等。
由于资源类型变量保存有为打开文件、数据库连接、图形画布区域等的特殊句柄,因此将其它类型的值转换为资源没有意义。
使用 get_resource_type() 函数可以返回资源(resource)类型:
php
$fp = fopen("test.txt", "r");
echo get_resource_type($fp); // 输出: stream