PHP初学者必掌握的10个常见PHP函数
1. echo()函数:用来向浏览器输出一段文本或变量的值。
示例代码:echo "Hello World!";
2. print()函数:用来向浏览器输出一段文本或变量的值,和echo()函数类似,但有一些差异。
示例代码:print "Hello World!";
3. strlen()函数:用来获取字符串的长度,返回字符串的字符个数。
示例代码:$str = "Hello World!";
echo strlen($str); // 输出:12
4. strtoupper()函数:用来将字符串转换为大写。
示例代码:$str = "Hello World!";
echo strtoupper($str); // 输出:HELLO WORLD!
5. strtolower()函数:用来将字符串转换为小写。
示例代码:$str = "Hello World!";
echo strtolower($str); // 输出:hello world!
6. substr()函数:用来获取字符串的子串,指定起始位置和长度。
示例代码:$str = "Hello World!";
echo substr($str, 0, 5); // 输出:Hello
7. explode()函数:用来将字符串通过指定的分隔符拆分成数组。
示例代码:$str = "Hello World!";
$arr = explode(" ", $str);
print_r($arr); // 输出:Array ( [0] => Hello [1] => World! )
8. implode()函数:用来将数组元素连接成字符串,通过指定的分隔符。
示例代码:$arr = array("Hello", "World!");
$str = implode(" ", $arr);
echo $str; // 输出:Hello World!
9. count()函数:用来获取数组的长度,返回数组元素的个数。
示例代码:$arr = array("Hello", "World!");
echo count($arr); // 输出:2
10. file_exists()函数:用来检查文件或目录是否存在,返回true或false。
示例代码:$file = "example.txt";
if(file_exists($file)) {
echo "File exists!";
} else {
echo "File does not exist!";
}
