21个实用PHP代码
1.获取当前时间戳:$timestamp = time(); 2.获取当前日期:$date = date('Y-m-d'); 3.获取当前时间:$time = date('H:i:s'); 4.获取当前日期和时间:$datetime = date('Y-m-d H:i:s'); 5.获取客户端IP地址:$ip = $_SERVER['REMOTE_ADDR']; 6.检查变量是否设置或不为空:if(isset($var) && !empty($var)) 7.文件上传:move_uploaded_file($_FILES['file']['tmp_name'], '/path/to/uploaded/file'); 8.字符串替换:$new_str = str_replace('old_string', 'new_string', $str); 9.字符串分割:$arr = explode(',', $str); 10.数组排序:sort($arr); 11.遍历数组:foreach($arr as $key => $value) 12.获取数组长度:$length = count($arr); 13.判断字符串是否包含某个子字符串:if(strpos($str, 'sub_string') !== false) 14.获取URL参数(GET请求):$param = $_GET['param']; 15.获取表单参数(POST请求):$param = $_POST['param']; 16.数据库连接:$conn = mysqli_connect('host', 'username', 'password', 'database'); 17.执行SQL语句:mysqli_query($conn, 'SQL statement'); 18.查询数据:$result = mysqli_query($conn, 'SELECT * FROM table'); 19.获取查询结果集中的一条数据:$row = mysqli_fetch_row($result); 20.获取查询结果集中的全部数据:$rows = mysqli_fetch_all($result, MYSQLI_ASSOC); 21.关闭数据库连接:mysqli_close($conn);