Home > Article > Backend Development > Code optimization for PHP7 message board development
php7The column introduces code optimization for message board development
Recommendation (free): PHP7
Outline of this step:
1. Optimize the code
It is said to be code optimization, but it is actually to organize the common parts of the code that are used multiple times. , use include (require) on the page that needs to be called, which not only facilitates future modifications, but also makes the code clean and readable.
Then let’s start to be an elegant programmer.
a. First find the public part of the code. Usually the same code is used multiple times on the application page (of course it can be extracted once). For example, the database account, password, database name and other configuration information are extracted and saved in a new config.php (this is how it is named, you can also use other file names in actual operation)
file. The text description looks very boring, as shown in the red box below, are they all the same code:
b, The red box in the above two pictures is the public part of the code we are looking for. Copy it and paste it into the new file config.php. Insert the config.php file into the page you need to use, such as the config.php database configuration information file, which must be in A. Used in php pages, this time you need to use the php function include (include and require statements are used to insert useful codes written in other files into the execution flow.
), that is, in the A.php file Headerinclude 'config.php'
<?php $localhost = '127.0.0.1'; // 数据库地址 $user = 'root'; // 数据库用户名 $password = 'root'; // 数据库密码 $dbname = 'php_course'; // 数据库名 $mysqli = mysqli_connect($localhost, $user, $password, $dbname); if(mysqli_connect_errno()){ echo '连接数据库失败:'.mysqli_connect_error(); exit; } mysqli_query($mysqli, "SET NAMES UTF8");
<?php include 'config.php'; // 这里很重要,插入配置信息的文件 // 下面的逻辑代码 $sql = 'INSERT INTO feedback (name, contact, content, addtime) VALUES ("测试", "qq1000", "留言内容", '.$time.')'; $result = mysqli_query($mysqli, $sql); // 返回一个资源标识符,通常是数字 $insert_id = mysqli_insert_id($mysqli); // 返回数据表的自增长ID,比如新用户注册返回用户ID echo $insert_id; // 当你在调试的时候,你会发现echo是很好的帮手。 if($insert_id > 0){ // 如果入库成功,可以做什么 } ……
Code picture:
##It will be clear at a glance. See if the code is smaller and neater, and when modification is needed, only one file needs to be modified, which greatly reduces the error rate. Hurry up and try it!1. Pay attention to the cultivation of thinking;
Everything is difficult at the beginning. The first time you come into contact with programming, you will definitely encounter a lot of questions. Just think about asking questions. Others, I think it is wrong; if you want to become a qualified programmer, when you encounter a problem, you should first think about how to solve the problem and analyze the problem, instead of asking someone immediately. When you have the ability to analyze and solve problems, and continue to accumulate, you will form your unique thinking. Remember!2. Theory application (unity of knowledge and action).
Isn’t there a saying: knowledge, if you read it, you can master 30%; after reading it, practice it, you can master 50%; after reading it, practice it and explain it clearly to others, you will master it 90%.Okay, that’s it for today. If there is anything wrong with the explanation or something you don’t understand, please leave a message! Oh, I almost forgot to remind you. In the future, the message board tutorial series will include paging, javascript (non-refresh verification form), jquery (asynchronous loading) and other related sharing, so remember to pay attention.
The above is the detailed content of Code optimization for PHP7 message board development. For more information, please follow other related articles on the PHP Chinese website!