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);
```