Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to optimize PHP code

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to optimize the PHP code, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Preface

This is a search function for background user list.

Can the search conditions be parallel, whether the user name is required, whether the mobile phone number can be authenticated, whether the user's gender can be recently logged in, whether the account balance can be allowed or not.

Beginner code

Seeing these examples, you can't help but tremble and start building wheels again, aren't you?

Taking the native example as an example, you might write this at first (the following is pseudocode):

If (IS_POST) {$like ='; if (isset ($_ POST ['username'])) {$username = $_ POST [' username']; $like. = "username like'%". $username. "% 'and";} if (isset ($_ POST [' phone'])) {$phone = $_ POST ['phone']; $like. = "phone like'%". $phone. "% 'and";} if ($_ POST [' is_auth']) {$isAuth = $_ POST ['is_auth']; $like. = "is_auth like'%". $isAuth. "% 'and";} if ($_ POST [' sex']) {$sex = $_ POST ['sex']; $like. = "sex like'%". $sex. "% 'and";} if ($_ POST [' time']) {$time = $_ POST ['time']; $like. = "time like'%". $time. "% 'and";} if ($_ POST [' wallet']) {$wallet = $_ POST ['wallet']; $like. = "wallet like'%". $wallet. "% 'and";} $like = rtrim ($like,' and'); $sql = "SELECT * FROM `user` WHERE {$like}";} else {return view ('user');}

Encapsulation

Well, not bad, the structure is clear, the traditional beginner bar code, and then let's encapsulate a few pieces of code.

Function post ($param) {return isset ($_ POST [$param])? $_ POST [$param]: null;} if (IS_POST) {$like ='; if (post ('username')) {$username = post (' username'); $like. = "username like'%". $username. "% 'and";} if (post (' phone')) {$phone = post ('phone'); $like. = "phone like'%". $phone. "% 'and";} if (post (' is_auth')) {$isAuth = post ('is_auth'); $like. = "is_auth like'%". $isAuth. "% 'and";} if (post (' sex')) {$sex = post ('sex'); $like. = "sex like'%". $sex. "% 'and";} if (post (' time')) {$time = post ('time'); $like. = "time like'%". $time. "% 'and";} if (post (' wallet')) {$wallet = post ('wallet'); $like. = "wallet like'%". $wallet. "% 'and";} $like = rtrim ($like,' and'); $sql = "SELECT * FROM `user` WHERE {$like}";} else {return view ('user');}

Appropriate use of iterations

Well, at least we are free to control the post method, but this kind of procedural code is too low to maintain, so let's improve it:

Function post ($param) {return isset ($_ POST [$param])? $_ POST [$param]: false;} function postAll () {return $_ POST;} if (IS_POST) {$like =''; foreach (postAll () as $key = > $value) {if (post ($key) {$like. = "{$key} like'% {$value}% 'and" }} $like = rtrim ($like, 'and'); $sql = "SELECT * FROM `user`WHERE {$like}";} else {return view (' user');}

object-oriented

It looks neat to add iterative code. As a PHP programmer, writing code is not object-oriented and unreliable. Add class.

Function request ($param = null) {return new Request ($param);} class Request {public function _ construct (string $param = null) {return isset ($_ POST [$param])? $_ POST [$param]: false;} public function all () {return $_ POST }} class User {public function index () {if (IS_POST) {$like ='; foreach (request ()-> all () as $key = > $value) {if (request ($key)) {$like. = "{$key} like'% {$value}% 'and" }} $like = rtrim ($like, 'and'); $sql = "SELECT * FROM `user`WHERE {$like}";} else {return view (' user');}

The transformation of User

We are modifying the classes of User, making some judgments and screening.

Function request ($param = null) {return new Request ($param);} class Request {public function _ construct (string $param = null) {return isset ($_ POST [$param])? $_ POST [$param]: false;} public function all () {return $_ POST }} class User {public $request = ['username',' phone', 'is_auth',' sex', 'time',' wallet']; public function index () {if (IS_POST) {$like ='' Foreach (request ()-> all () as $key = > $value) {if (in_array ($key, $this- > request) & & request ($key)) {$like. = sprintf ("% s like% s and", $key, $value);}} $like = rtrim ($like, 'and') $sql = "SELECT * FROM `user`WHERE {$like}";} else {return view ('user');}

This is about it. There may be a long way to go compared to the real code. My purpose of writing this article is not to teach you how to write code, but to show that the code is not one-time and should be modified many times to make the code maintainable, extensible, and so on. All kinds of "sex".

Thank you for reading this article carefully. I hope the article "how to optimize PHP Code" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report