Categories


Tags


21个实用PHP代码

1. 计算字符串长度: ```php $string = "Welcome to the PHP world!"; $length = strlen($string); echo $length; //输出:27 ``` 2. 字符串截取: ```php $string = "Welcome to the PHP world!"; $substring = substr($string, 0, 7); //从0开始截取,截取长度为7 echo $substring; //输出:Welcome ``` 3. 数组遍历: ```php $fruits = array("apple", "banana", "orange"); foreach($fruits as $fruit) { echo $fruit . "
"; } //输出: //apple //banana //orange ``` 4. 数组排序: ```php $numbers = array(5, 2, 8, 1, 3); sort($numbers); //升序排序 print_r($numbers); //输出:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 5 [4] => 8 ) ``` 5. 数组查找: ```php $fruits = array("apple", "banana", "orange"); $key = array_search("banana", $fruits); echo $key; //输出:1 ``` 6. 获取当前时间戳: ```php $timestamp = time(); echo $timestamp; //输出:1631771555 ``` 7. 将时间戳格式化为日期: ```php $timestamp = time(); $date = date("Y-m-d", $timestamp); echo $date; //输出:2021-09-16 ``` 8. 获取当前文件路径: ```php $path = $_SERVER['REQUEST_URI']; echo $path; //输出:/index.php ``` 9. 获取当前文件名: ```php $filename = basename($_SERVER['SCRIPT_FILENAME']); echo $filename; //输出:index.php ``` 10. 设置Cookie: ```php setcookie("username", "John"); ``` 11. 获取Cookie: ```php $username = $_COOKIE["username"]; echo $username; //输出:John ``` 12. 删除Cookie: ```php setcookie("username", "", time() - 3600); //设置过期时间为1小时前 ``` 13. 判断是否存在指定Cookie: ```php if(isset($_COOKIE["username"])) { echo "Cookie存在"; } else { echo "Cookie不存在"; } ``` 14. 获取客户端IP地址: ```php $ip = $_SERVER['REMOTE_ADDR']; echo $ip; //输出:127.0.0.1 ``` 15. 获取客户端浏览器信息: ```php $browser = $_SERVER['HTTP_USER_AGENT']; echo $browser; //例如:Mozilla/5.0 (Windows NT 10.0; … ``` 16. 发送电子邮件: ```php $to = "someone@example.com"; $subject = "Subject"; $message = "Hello! This is a test email."; $headers = "From: sender@example.com"; mail($to, $subject, $message, $headers); ``` 17. 加密字符串: ```php $password = "123456"; $encrypted_password = md5($password); echo $encrypted_password; //输出:e10adc3949ba59abbe56e057f20f883e ``` 18. 解码URL: ```php $url = "https%3A%2F%2Fwww.baidu.com%2F"; $url_decoded = urldecode($url); echo $url_decoded; //输出:https://www.baidu.com/ ``` 19. 编码URL: ```php $url = "https://www.baidu.com/"; $url_encoded = urlencode($url); echo $url_encoded; //输出:https%3A%2F%2Fwww.baidu.com%2F ``` 20. 向数据库插入数据: ```php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; $conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')"; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "
" . mysqli_error($conn); } mysqli_close($conn); ``` 21. 从数据库中查询数据: ```php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; $conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 results"; } mysqli_close($conn); ```

Public @ 2023-04-01 12:51:23

如何给超链接加入下划线?

通常来说CSS文件定义的a{text-decoration:underline;}意思是全站所有超链接均有下划线,如果打算单独某区域加入链接下划线,就可以这样定义,如:以我博客的内容页为例,博客通常的CSS定义是post标签,那么设定.post a{text-decoration:underline;}就表明,文章部分所有带链接的文字都加入下划线。

Public @ 2013-05-11 15:46:24

IIS7关于多域名多证书多IP的部署方案

在IIS 7中,可以使用多域名、多证书和多IP的部署方案来支持多个网站或应用程序。下面是一个基本的部署方案: 1. 获取并安装多个SSL证书:每个域名对应的网站需要使用独立的SSL证书。首先,从可信的证书颁发机构 (CA) 购买或获取每个域名的SSL证书。然后,将证书安装到服务器上。 2. 配置多个IP地址:为每个域名分配不同的IP地址。可以通过网络适配器设置静态IP地址或者使用网络设备(例如

Public @ 2023-07-22 12:00:38

php访问报错HTTP/1.1 200 OK Connection: close Data

由于这个问题描述过于简单和模糊,我无法对它做出明确的回答。但是,我建议您检查以下几个可能的原因: 1. 检查服务器是否正常运行,并确保通过正确的端口和URL访问。 2. 确保您的PHP代码正确配置了服务器设置,并且没有任何语法错误或逻辑错误。您可以使用PHP解释器来测试您的代码的正确性。 3. 检查您的Web服务器配置是否正确,特别是与PHP环境相关的设置。 4. 确保您的代码没有与其他应

Public @ 2023-06-17 11:50:14

使用Chroot解决PHP的运行安全问题

近些日子,小蒋的一篇博文引起了广大主机商的关注。其原因是小蒋运营的创智主机被提交了漏洞到乌云网上在这篇文章中小蒋给出了解决方法,就是采用禁用函数的方法来保证服务器环境的安全。但是,禁用函数真的是最好的选择么?不是的!AMH面板的开发者Amysql告诉我们,更好的选择是Chroot!AMH面板是一款LNMP面板,一方面,为了方便版本升级,另一方面,面板是开发者一个人开发的,所以AMH没有像kangl

Public @ 2016-05-02 15:46:38

更多您感兴趣的搜索

0.105504s