Home >Backend Development >PHP Tutorial >10 Useful PHP Code Snippets

10 Useful PHP Code Snippets

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2023-06-23 10:04:551625browse

PHP is currently one of the most popular server-side Web programming languages, and it has powerful functions and wide applications. In this article, we will introduce you to 10 useful PHP code snippets that you can use in web development.

  1. Variable output

You can use the echo function to output a string variable. For example:

$name = 'John';
echo 'Hello,'.$name;

Output:

Hello,John
  1. Loop

PHP has many looping methods, such as for, foreach, while and do-while. The following is an example of a for loop:

for ($i = 0; $i < 10; $i++) {
    echo $i;
}

Output:

0123456789
  1. Array

Array is one of the most basic data structures in PHP. You can use arrays to hold multiple values. Here is a simple array example:

$colors = array('red', 'blue', 'green');
echo $colors[0];

Output:

red
  1. Conditional Statement

You can use if and else statements to condition based on certain conditions Execute different codes. The following is an example of an if and else statement:

$age = 25;
if ($age > 18) {
    echo '成年人';
} else {
    echo '未成年人';
}

Output:

成年人
  1. Function

The function encapsulates the relevant code into a separate code Structure to use in blocks. Here is an example of a function:

function add($num1, $num2) {
    return $num1 + $num2;
}
echo add(2, 3);

Output:

5
  1. Data filtering

In web development, you often need to filter and filter input from the user verify the data. Here is an example of filtering data:

$username = $_POST['username'];
$username = filter_var($username, FILTER_SANITIZE_STRING);

In this example, we use the filter_var function and FILTER_SANITIZE_STRING to filter the username string entered by the user.

  1. File Operations

PHP can handle file operations easily. Below is a simple example of opening, reading and closing a file:

$file = fopen('test.txt', 'r');
while (!feof($file)) {
    echo fgets($file);
}
fclose($file);

In this example, we open a file named test.txt and read its contents using feof and fgets functions.

  1. Time and Date

PHP provides many functions to handle time and date. The following is an example of getting the current time:

echo date('Y-m-d H:i:s');

In this example, we use the date function to get the current time in the format of "year-month-day hour:minute:second".

  1. Database Operations

PHP allows you to easily access and manipulate databases. Below is an example of a simple MySQL query:

$conn = mysqli_connect('localhost', 'username', 'password', 'database');
$sql = 'SELECT * FROM users';
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
    echo $row['username'];
}
mysqli_close($conn);

In this example, we use the mysqli function to connect to the MySQL database and use the mysqli_query function to perform a SELECT query. We then use the mysqli_fetch_array function to iterate through the result set and output each username.

  1. Regular Expressions

Regular expressions are powerful tools for processing text pattern matching. Here is an example of matching email addresses:

$email = 'user@example.com';
if (preg_match('/^[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,}$/', $email)) {
    echo '有效的电子邮件地址';
} else {
    echo '无效的电子邮件地址';
}

In this example, we use the preg_match function and the regular expression ^[a-z0-9._% -] @[a-z0-9. -] .[a-z]{2,}$ to check if the given string is a valid email address.

The above is the detailed content of 10 Useful PHP Code Snippets. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn