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.
You can use the echo function to output a string variable. For example:
$name = 'John'; echo 'Hello,'.$name;
Output:
Hello,John
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
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
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:
成年人
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
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.
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.
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".
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.
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!