10 Things You Shouldn't Do In PHP 7 10 Things You Should Avoid In PHP 7 (r)

Apr 21, 2022
PHP 7 best practices

PHP 7 Top Practices and What Should You Avoid in PHP 7

  1.    Do not use mysql Functions
  2.    Do not write wasteful Code
  3.    Do Not Utilize PHP Close Tags
  4.    Do Not Use a Reference if Not Needed
  5.    Do Not Execute Queries In A Loop
  6.    Do Not Use * when querying SQL
  7.    Do Not Rely on User-Input
  8.    Do Not Try to be Clever
  9.    Do not reinvent the Wheel
  10.    Do Not Forget Other Languages

1. Avoid using mysql_ functions

The time has finally come where you'll not be told to quit making use of mysql's functions. PHP 7 will remove them altogether from core which will mean you'll have to change to the more efficient mysqli_ functions or an much more flexible PDO implementation.

2. Don't write code that is a waste of time.

It's an obvious choice, but it could get more significant as speed increases in PHP 7 may hide certain problems. Don't be content with your site's performance just because the switch to PHP 7 increased speed.

3. Do Not Use PHP Close Tags at the End of a file.

If you take a look at the majority of core WordPress documents do not include the PHP tag whenever a file is tagged with PHP code. In fact it is the Zend Framework specifically forbids the use of PHP tags. It is not required to be used by PHP and by not putting it at the end of a file , you're making sure there is no whitespace trailing be put in.

4. Do Not Pass by Reference If You Do Not Really Need It

Personally, I'm not a fan of passing by reference. It is true that in certain situations it can be beneficial, however in others it makes code harder to comprehend and follow, and, in particular, difficult to anticipate the result.

It seems that people believe that it speeds up their programming but according to respected PHP programmer does not hold true.

One example of why reference one reason is that they are bad for PHP that is built into the shuffle() or sort(). Instead of returning a shuffled or sortable array, they alter the source code, which is incongruous to my thinking.

5. Don't perform queries in a Loop

Making database queries in loops is a waste of time. It puts unnecessary strain on your systems and it is probable that you will get the same results more quickly outside the loop. In the event of an issue where this might be needed I can usually resolve the problem using two distinct queries that I employ to create an array of data. Then, I loop through the array without having to perform queries in the loop.

Do you want to know the ways we have increased traffic over 1000 percent?

Join the 20,000+ who receive our weekly newsletter that contains insider WordPress tricks!

6. Don't Use * in SQL Queries

This one is mostly problem with MySQL problem, however we tend to write our SQL code using PHP and I believe it's an acceptable game. In any case, don't make use of wildcards in SQL queries if you're able to be able to avoid them, particularly if you have a database with a lot of columns.

You should specify which columns that you require and then only take those columns. This can help limit your resource usage, protect your data , and help make the process so clear as they can be.

In the context of SQL be aware of your SQL functions and try to test their the speed you can get as high as is possible. When calculating aggregates, sums or other similar numbers, use SQL functions instead of PHP functions. If you are unsure of the speed of your query, do a test and then try other variations - use the best one.

7. Beware of User-Input from Users

It is not wise to put your trust in user input. Always filter, sanitize as well as escape, verify and utilize fallbacks. Three problems with user data: we programmers don't consider every possible scenario to account for, and it's often incorrect, and could have been maliciously crafted.

An organized security system will protect you from each of these. Be sure to utilize the built-in functions, such as filter_var() to check for the correct values, and escape and various functions when working with databases.

WordPress has a bunch of tools to help out. Look over the validation, escape and sanitising user data section for more details.

8. Don't Try to be Clever

Your goal should be to write code with elegance that conveys your goals with the most clarity. You may be able to shave off an extra 0.01 second off each page by reducing everything to one-letter variables by using multi-level ternary logic or various other tricks, but that will not be anything compared to the problems you'll be causing yourself and everyone who is in the vicinity.

Make sure you name your variables correctly and document your code. opt for clarity over conciseness. Also, make sure you use standard object-oriented programming that is more or less self-documenting without needing numerous inline comments.

9. Don't reinvent the wheel

From color-manipulation tools to loggers, instruments, from profilers to unit testing frameworks, from Mailchimp APIs and Twitter Bootstrap Everything is available at the push of a button (or typing of a command) Use these!

10. Do Not Neglect Other Languages

If you're doing good with these tools but what do you think of Ruby, Ruby on Rails application creation that works for Android, iPhone, Windows Phone? It's easy to think there's nothing to be gained from these since they fall out of your normal routine and requirements for work however that's not the point. Every language has something to share and more knowledge can never hurt. It's not a coincidence that the best PHP developers know a lot about other programming languages!

Save time, costs and maximize site performance with:

  • 24/7 help and support assistance from WordPress hosting specialists, 24 hours a day.
  • Cloudflare Enterprise integration.
  • Reaching a global audience with 29 data centers worldwide.
  • Optimization using our built-in Application to monitor performance.