利用这10个PHP函数轻松完成常见任务
1. echo:用于输出内容到页面,可以输出字符串、变量、数组等。
例子:echo "Hello, world!";
2. strlen:用于获取字符串的长度。
例子:$str = "Hello, world!";
echo strlen($str); // 输出:13
3. strtoupper:用于将字符串转换为大写。
例子:$str = "hello, world!";
echo strtoupper($str); // 输出:HELLO, WORLD!
4. strtolower:用于将字符串转换为小写。
例子:$str = "HELLO, WORLD!";
echo strtolower($str); // 输出:hello, world!
5. substr:用于获取字符串的子字符串。
例子:$str = "Hello, world!";
echo substr($str, 0, 5); // 输出:Hello
6. explode:用于将字符串分割成数组。
例子:$str = "apple,banana,orange";
$arr = explode(",", $str);
print_r($arr); // 输出:Array ( [0] => apple [1] => banana [2] => orange )
7. implode:用于将数组元素拼接成字符串。
例子:$arr = array("apple", "banana", "orange");
$str = implode(",", $arr);
echo $str; // 输出:apple,banana,orange
8. isset:用于检查变量是否已设置并且不为null。
例子:$name = "John";
if (isset($name)) {
echo "Name is set!";
} else {
echo "Name is not set!";
}
9. file_get_contents:用于读取文件内容并返回字符串。
例子:$content = file_get_contents("example.txt");
echo $content;
10. file_put_contents:用于将字符串写入文件。
例子:$content = "Hello, world!";
file_put_contents("example.txt", $content);
