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

What is the skill for PHP developers to get twice the result with half the effort?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What is the skill for PHP developers to get twice the result with half the effort? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

What happens if you use a big mirror as a surfboard? Maybe you will conquer the waves in a short time, but you must know from the bottom of your heart that this is not the right choice for surfing. The same applies to PHP programming, although the analogy sounds odd. We often hear people trying to learn PHP in a little more time over the weekend, but with all due respect, this is a very bad way to learn the programming language.

Why is the process of learning PHP different from any other language?

In essence, if you have mastered the way to "do things" in PHP, you will be comfortable with it, so it is worth your effort to understand these ways. In PHP, it is often a wrong way to solve problems simply according to your own ideas. This is not because you are a bad programmer, but because if you want to write good maintainable code, there are some standard techniques that you have to use. Let's take a look at the top 10 tips you need to know.

1. How to create an Index page of a website correctly

When creating every site, creating an index page for the site is one of the first things to do. If you are new to PHP, the typical way to write an index page is to program only what the index page needs and other links to create another page. However, if you want to learn a more efficient way to implement PHP programming, you can use the "index.php?page=home" mode, which is adopted by many websites.

2. Use Request Global Array to grab data

In fact, we have no reason to use the $_ GET and $_ POST arrays to grab values. The global array $_ REQUEST allows you to get a get or form request. Therefore, in most cases, the more efficient code for parsing data is roughly as follows:

01.$action = isset ($_ REQUEST ['action'])? $_ REQUEST [' action']: 0

3. Use var_dump to debug PHP code

If you are looking for php debugging technology, I must say that var_dump should be your target. This command meets all your needs in terms of displaying php information. Most of the debugging code has to do with getting the values in PHP.

4. PHP handles the code logic, Smarty deals with the presentation layer

Smarty is a template PHP template engine written in PHP, and it is one of the PHP template engines in the industry. It separates logical code from external content, and provides an easy-to-manage and easy-to-use way to logically separate PHP code that would otherwise be mixed with HTML code.

To put it simply, the purpose is to separate the PHP programmer from the front-end staff, so that the programmer changes the logical content of the program will not affect the front-end staff's page design, and the front-end staff revising the page will not affect the program logic, which is particularly important in multi-person cooperation projects.

5. When you do need to use global values, create a Config file

It is a bad practice to create global values easily, but sometimes it is necessary to do so. It's a good idea to use global values for database tables or database connection information, but don't use global values frequently in your PHP code. In addition, a better approach is to store your global variables in a config.php file.

6. if it is not defined, access is prohibited!

If you create the page correctly, there is no reason for anyone else to visit an index.php page other than index.php or home.php. Once the index.php is accessed, you can open the page you need by getting variables. Your index page should contain code similar to the following:

Define ('yourPage',1)

Then, the other pages should contain:

If (! defined ('yourPage')) die (' Access Denied')

The purpose of this is to prevent direct access to your other php pages. In this way, anyone who tries not to access other web pages through index.php will get the message that "access is denied".

7. Create a database class

If you are doing database programming (a very common task in PHP), it is a good idea to create a database class to handle any database management function. The sample code is as follows:

Public function dbExec ($query) {$result = $this- > db- > exec ($query); if (PEAR::isError ($result)) errorRedirect ($result- > getMessage (), true); else return $result;}

This function receives only one query statement and executes it. It also handles any errors that may occur. You can also include the audit code here, but I prefer to use a similar audit function:

01. Has multiple arguments function sanitizeInput / checks if arguments given are integer values not less than 0-has multiple arguments function sanitizeInput () {$numargs = func_num_args (); $arg_list = func_get_args (); for ($I = 0; $I

< $numargs; $i++) { if (!is_numeric($arg_list[$i]) || $arg_list[$i] < 0) errorRedirect("Unexpected variable value", true); } } 8、一个php文件处理输入,一个class.php文件处理具体功能 不让代码变得混乱的一个重要方法是:获取用户输入后,将其重定向到其它函数来进行处理。原理非常简单,php文件获得我们需要的任何输入,然后将其执行重定向到类文件中的一个函数。举例来讲,假设有一个类似"index.php?page=profile&action=display"的 URL。由profile.php来检索该网址并得到操作是"display"。然后使用一个简单的switch函数,我们来执行真正的显示函数: require_once PROJECTROOT.'libs/messages.class.php'; $message = new Message(); switch ($action) { case 'display': $message->

Display (); break;...

As shown above, I used a message class and then started switch checking. $message is just an object used by the calling function in the class.

9. Know your SQL statements and always review them (Sanitize)

As I mentioned before, the most important part of any php site is likely to be a database. Therefore, you need to be very familiar with how to use sql correctly. Learn to associate tables and more advanced techniques. Next I'll show an example of a function that uses MySQL and review it using the function in Article 7 of this article.

Private function getSentMessages ($id) {$this- > util- > sanitizeInput ($id); $pm_table = $GLOBALS ['config'] [' privateMsg']; $users = $GLOBALS ['config'] [' users']; $sql = "SELECT PM.*, USR.username as name_sender FROM $pm_table PM, $users USR WHERE id_sender ='$id' AND sender_purge = FALSE AND USR.id = PM.id_receiver AND is_read = TRUE ORDER BY date_sent DESC"; $result = $this- > dbQueryAll ($sql); return $result;}

First, we check the user input (passing the message id through a GET variable), and then we execute our SQL command. Notice the use of SQL here. You need to know how to use aliases and associated tables.

10. Use singleton mode when you only need one object

In a fairly common situation in PHP, we only need to create an object once and then use it throughout our program. A good example is the smarty variable, which can be used anywhere once initialized. A good implementation of this scenario is the singleton pattern. The sample code is as follows:

Function smartyObject () {if ($GLOBALS ['config'] [' SmartyObj'] = 0) {$smarty = new SmartyGame (); $GLOBALS ['config'] [' SmartyObj'] = $smarty;} else $smarty = $GLOBALS ['config'] [' SmartyObj']; return $smarty;}

Note that we have a global smarty variable (in this example, it is initialized in config.php), and if its value is 0, we will create a new smarty object. Otherwise, it means that the object has been created, and we just need to return it.

After reading the above, have you mastered the technique of getting PHP developers to get twice the result with half the effort? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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