Top 10 tips to get PHP developers to do more with less

  • 2020-03-31 20:35:42
  • OfStack

What happens if you use a big mirror as a surfboard? You may conquer the waves in a short time, but you must know deep down that this is not the right way to surf. The same applies to PHP programming, although the analogy may sound a bit odd. We often hear of people trying to learn PHP over the course of a weekend, but with all due respect, this is a terrible way to learn the programming language.

Why is the process of learning PHP different from any other language?
By its very nature, if you have mastered the ways of "doing things" in the PHP language, you will be comfortable using it, so it's worth the effort to understand these ways. In PHP, it's often the wrong way to solve a problem just by thinking your way through it. This is not because you are a bad programmer, but because if you want to write good, maintainable code, there are some standard techniques you must use. 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
The index page is one of the first things to do when creating each website. If you're new to PHP, the typical way to write an index page is to program only what the index page needs and create another page with other links. However, if you want to learn a more efficient way to implement PHP programming, you can use "index.php? Page =home mode, which is used by many websites.

2. Fetch data using Request Global Array
There's actually no reason to use the $_GET and $_POST arrays to fetch values. $_REQUEST is a global array that lets you get a get or form request. Therefore, in most cases, the more efficient code for parsing data looks like this:
$action = isset ($_REQUEST [' action '])? $_REQUEST [' action '] : 0;

3. Debug PHP code by var_dump
If you are looking for PHP debugging techniques, I must say that var_dump should be your target. This command will do everything you need to display PHP information. Most cases of debugging code have to do with getting a value in PHP.

4. PHP handles the code logic, while Smarty handles the presentation layer
Smarty is a templating engine written in PHP. It is one of the most famous PHP templating engines in the industry. It separates the logical code from the external content, providing an easy to manage and use method to separate the PHP code logic from the HTML code. In short, the purpose is to separate PHP programmers from front-end staff, so that the programmer changes the logical content of the program will not affect the front-end staff's page design, front-end staff re-modify the page will not affect the program's logic, which is particularly important in multi-person cooperation projects.

5. When you do need to use global values, create a Config file
Creating global values is a bad idea, but it is sometimes necessary. It's a good idea to use global values for database tables or database connection information, but don't use them too often in your PHP code. Also, it's better to store your global variables in a config.php file.

6, if not defined, do not access!
If you created the page correctly, there is no reason for anyone else to visit the index.php page other than index.php or home.php. Once index.php is accessed, you can get the variables to open the required page. Your index page should contain the following code:
Define (' yourPage ', 1);
Then, other pages should include:
If (! Defined (' yourPage) die (' Access Denied ');
The purpose of this is to prevent direct access to your other PHP pages. This way, anyone who tries to access other web pages without using index.php will get an "access denied" message.

Create a database class
If you're doing database programming (a very common task in PHP), a good idea is to create a database class that handles any database administration functionality. 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 takes only one query and executes it. It also handles any errors that may occur. You can also include auditing code here, but I prefer to use a similar auditing function:
 
// 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. One PHP file handles input and one class.php file handles specific functions
An important way to keep your code from getting messy is to take user input and redirect it to another function for processing. The principle is simple: the PHP file takes whatever input we need and then redirects its execution to a function in the class file. For example, suppose you have an "index.php? Page = profile&action = display "URL. The address is retrieved by profile.php and the action is "display". Then, using a simple switch function, we perform the actual display function:
 
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 started the switch check. $message is just an object used by the calling function in the class.

9. Understand your SQL statements and always Sanitize them
As I mentioned before, 99% of the time the most important part of any PHP website is probably the 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 you an example of a function that USES MySQL and review it using the seventh function 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.

Use singleton mode when you only need one object
In a fairly common scenario 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, once initialized, can be used anywhere. 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 case, it is initialized in config.php), and if it has a value of 0, we will create a new smarty object. Otherwise, it means the object has been created and we just need to return it.

Related articles: