What would happen if you used a large mirror as a surfboard? You may be able to conquer waves in a shorter period of time, but you surely know deep down that this is not the right choice for surfing. The same principle applies to PHP programming, although the analogy may sound a little weird. We often hear of people trying to learn PHP in just over a weekend, but IMHO this is a very poor way to learn this programming language.
Why is the process of learning PHP different from any other language?
By its nature, if you master the way of "doing things" in the PHP language, you will become more comfortable using it, so it is worth the investment to understand these ways. In PHP, simply solving problems according to your own ideas is often a wrong approach. This is not because you are a bad programmer, but because there are some standard techniques you must use if you want to write good maintainable code. Let’s take a look at the top 10 tips you need to know.
1. How to correctly create the Index page of a website
When creating every website, establishing the index page of the website is one of the first things to do. If you are new to PHP, a typical approach when writing an index page is to program only the content required for the index page, and create another page for other links. 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 used by many websites.
2. Use Request Global Array to capture data
In fact, we have no reason to use $_GET and $_POST arrays to capture values. The global array $_REQUEST allows you to obtain a get or form request. Therefore, in most cases a more efficient code for parsing the data would look like this:
$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 the goal you are looking for. This command can meet all your needs when it comes to displaying php information. Most cases of debugging code are related to getting values in PHP.
4. PHP handles the code logic and Smarty handles the presentation layer
Smarty is a template PHP template engine written in PHP. It is one of the most famous PHP template engines in the industry. . It separates logical code and external content, providing an easy-to-manage and use method to logically separate PHP code that is originally mixed with HTML code. Simply put, the purpose is to separate PHP programmers from front-end personnel, so that programmers change the logical content of the program without affecting the page design of the front-end personnel, and front-end personnel re-modify the page without affecting the program logic of the program. This is particularly important in projects involving multi-person collaboration.
5. When you really need to use global values, create a Config file
It is a bad practice to create global values frequently, but sometimes the actual situation does require this. It's a good idea to use global values for database table or database connection information, but don't use global values frequently in your PHP code. Alternatively, 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 access the index.php page outside of index.php or home.php. Once index.php is accessed, you can open the required page by obtaining the variables. Your index page should contain code similar to:
define('yourPage',1);
Then, 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 to access other web pages without going through index.php will get an "Access Denied" message.
7. Create a database class
If you are doing database programming (a very common task in PHP), a good idea is to create a database class to handle any database management functions. The sample code is as follows: