一、PHP常用的数组函数
PHP数组是一种用于存储多个值的数据结构,常用于对一组数据的操作和处理。PHP中提供了丰富的数组函数,以下是部分常用的数组函数:
//1. array_push(): 向数组尾部添加一个或多个元素
$array = ['apple', 'banana'];
array_push($array, 'orange', 'lemon');
print_r($array); //['apple', 'banana', 'orange', 'lemon']
//2. array_pop(): 弹出数组最后一个元素
$array = ['apple', 'banana', 'orange'];
$last = array_pop($array);
print_r($array); //['apple', 'banana']
echo $last; //orange
//3. in_array(): 判断元素是否在数组中
$array = ['apple', 'banana', 'orange'];
if (in_array('apple', $array)) {
echo '苹果在数组中';
}
//4. array_keys(): 获取数组所有键名
$array = ['name' => 'Tom', 'age' => 18];
$keys = array_keys($array);
print_r($keys); //['name', 'age']
//5. array_values(): 获取数组所有值
$array = ['name' => 'Tom', 'age' => 18];
$values = array_values($array);
print_r($values); //['Tom', 18]
二、PHP常用内置函数
PHP内置函数是指PHP语言本身提供的函数,可以直接在代码中使用。以下是部分常用的内置函数:
//1. var_dump(): 打印变量相关的信息
$array = ['apple', 'banana', 'orange'];
var_dump($array);
/*
array(3) {
[0]=>
string(5) "apple"
[1]=>
string(6) "banana"
[2]=>
string(6) "orange"
}
*/
//2. strlen(): 获取字符串长度
$str = 'hello world';
echo strlen($str); //11
//3. date(): 格式化时间
echo date('Y-m-d H:i:s'); //2021-06-15 12:00:00
//4. isset(): 判断变量是否存在
if (isset($variable)) {
echo '变量存在';
}
//5. empty(): 判断变量是否为空
if (empty($variable)) {
echo '变量为空';
}
三、PHP处理数组的常用函数
PHP处理数组有很多方式,以下是部分常用的数组处理函数:
//1. sort(): 数组升序排列 $array = [3, 1, 5, 2, 4]; sort($array); print_r($array); //[1, 2, 3, 4, 5] //2. rsort(): 数组降序排列 $array = [3, 1, 5, 2, 4]; rsort($array); print_r($array); //[5, 4, 3, 2, 1] //3. array_reverse(): 数组翻转 $array = [1, 2, 3, 4, 5]; $reversed = array_reverse($array); print_r($reversed); //[5, 4, 3, 2, 1] //4. array_slice(): 截取数组 $array = [1, 2, 3, 4, 5]; $slice = array_slice($array, 2, 2); print_r($slice); //[3, 4] //5. array_merge(): 合并数组 $array1 = [1, 2]; $array2 = [3, 4]; $merged = array_merge($array1, $array2); print_r($merged); //[1, 2, 3, 4]
四、PHP常用函数手册
PHP官方提供了非常详细的函数手册,包含了所有PHP内置函数以及它们的使用方法、参数、返回值等等。访问地址为:http://php.net/manual/en/funcref.php
五、PHP常用函数大全 面试
在PHP面试中,常常会问到一些PHP的基础知识和常用函数,以下是一些常见的PHP面试问题:
1. PHP中如何获取POST请求参数?
$name = $_POST['name']; $age = $_POST['age'];
2. PHP中如何获取GET请求参数?
$name = $_GET['name']; $age = $_GET['age'];
3. PHP中如何链接数据库?
$conn = mysqli_connect('localhost', 'username', 'password', 'database');
4. PHP中常用的字符串函数有哪些?
strlen()、substr()、strpos()、str_replace()、strtolower()
5. PHP中如何处理文件上传?
//1. 获取上传的文件 $file = $_FILES['file']; //2. 处理文件 move_uploaded_file($file['tmp_name'], 'uploads/' . $file['name']);
六、PHP常用的字符串函数
在PHP中,字符串操作是非常常见的。以下是部分常用的字符串函数:
//1. strlen(): 获取字符串长度
$str = 'hello world';
echo strlen($str); //11
//2. substr(): 截取字符串
$str = 'hello world';
$sub = substr($str, 0, 5);
echo $sub; //hello
//3. strpos(): 查找字符串位置
$str = 'hello world';
$pos = strpos($str, 'world');
echo $pos; //6
//4. str_replace(): 替换字符串
$str = 'hello world';
$newStr = str_replace('world', 'php', $str);
echo $newStr; //hello php
//5. strtolower(): 将字符串转为小写
$str = 'HELLO WORLD';
$newStr = strtolower($str);
echo $newStr; //hello world
七、PHP处理时间的常用函数
在PHP中,时间常常用于日志记录、定时任务等操作。以下是部分常用的时间函数:
//1. date(): 格式化时间
echo date('Y-m-d H:i:s'); //2021-06-15 12:00:00
//2. strtotime(): 将字符串转为时间戳
$timestamp = strtotime('2021-06-15');
echo $timestamp; //1623705600
//3. time(): 获取当前时间戳
echo time(); //1623721570
//4. mktime(): 获取指定时间戳
$timestamp = mktime(0, 0, 0, 6, 15, 2021);
echo $timestamp; //1623686400
//5. date_diff(): 计算两个日期之间的差值
$date1 = new DateTime('2021-06-15');
$date2 = new DateTime('2021-06-20');
$diff = date_diff($date1, $date2);
echo $diff->days; //5
八、PHP常用的文件操作函数
在PHP中,文件操作是非常常见的。以下是部分常用的文件操作函数:
//1. fopen(): 打开文件
$handle = fopen('example.txt', 'r');
//2. fgets(): 读取文件行
$line = fgets($handle);
echo $line;
//3. fwrite(): 写入文件
fwrite($handle, 'hello world');
//4. fclose(): 关闭文件
fclose($handle);
//5. file_get_contents(): 获取文件内容
$content = file_get_contents('example.txt');
echo $content;
九、用函数求1到n的和
以下是一种求1到n的和的函数:
function sum($n) {
if ($n == 1) {
return 1;
} else {
return $n + sum($n - 1);
}
}
echo sum(100); //5050
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/291081.html
微信扫一扫
支付宝扫一扫