PHP函数库中十个常用函数的实例讲解
发布时间:2023-07-04 01:42:05
1. strlen(): 该函数返回字符串的长度。例如:
$str = "Hello World"; $length = strlen($str);
在上述例子中,$length的值将为11,因为$str中包含了11个字符。
2. explode(): 该函数将字符串拆分为数组。例如:
$str = "apple,banana,orange";
$arr = explode(",", $str);
在上述例子中,$arr将为一个包含三个元素的数组,分别为"apple"、"banana"和"orange"。
3. implode(): 该函数将数组元素连接成一个字符串。例如:
$arr = array("apple", "banana", "orange");
$str = implode(",", $arr);
在上述例子中,$str的值将为"apple,banana,orange"。
4. strtolower(): 该函数将字符串转换为小写字母。例如:
$str = "Hello World"; $str = strtolower($str);
在上述例子中,$str的值将为"hello world"。
5. strtoupper(): 该函数将字符串转换为大写字母。例如:
$str = "Hello World"; $str = strtoupper($str);
在上述例子中,$str的值将为"HELLO WORLD"。
6. date(): 该函数返回当前日期和时间。例如:
$date = date("Y-m-d H:i:s");
在上述例子中,$date的值将为当前的日期和时间,格式为"年-月-日 时:分:秒"。
7. substr(): 该函数返回字符串的子串。例如:
$str = "Hello World"; $sub = substr($str, 0, 5);
在上述例子中,$sub的值将为"Hello",表示取$str的前5个字符作为子串。
8. array_merge(): 该函数将多个数组合并成一个数组。例如:
$arr1 = array("apple", "banana");
$arr2 = array("orange", "grape");
$result = array_merge($arr1, $arr2);
在上述例子中,$result将为一个包含四个元素的数组,分别为"apple"、"banana"、"orange"和"grape"。
9. count(): 该函数返回数组中元素的个数。例如:
$arr = array("apple", "banana", "orange");
$count = count($arr);
在上述例子中,$count的值将为3,表示$arr中包含了三个元素。
10. file_get_contents(): 该函数将文件的内容读取为字符串。例如:
$content = file_get_contents("file.txt");
在上述例子中,$content的值将为文件file.txt的内容。
