Developer News, More

5 THINGS NOT TO DO IN PHP 7

php 7

PHP 7 has been released and it’s time  time to prepare and shed some of our bad habits.So in this article  I’d take a look at some of the bad patterns we should stop using as we switch to the lightning fast PHP 7.

6 THINGS NOT TO DO IN PHP 7

Do Not Write Wasteful Code

Code quality is very important for any project.In PHP 7 don’t waste your time by writing wasteful code. it will become increasingly important because the speed increases in PHP 7 may hide some of your issues. Don’t be content with your site speed simply because the switch to PHP 7 made it faster.

As developers you should always make sure to load scripts only when they are needed, concatenate them when possible, write efficient database queries, use caching when possible and so on.

 

See More:  8 BEST PHP CHEAT SHEETS FOR DEVELOPERS

Do Not Use mysql_ Functions

In PHP 7 it’s high time to stopped using  mysql_functions.PHP 7 will remove them altogether from core which means you’ll need to move to the far better mysqli_functions, or the even more flexible PDO implementation.

 

Do Not Perform Queries In A Loop

If you perform database queries in a loop it’s not very useful,it’s just a waste of time.It puts unnecessary strain on your systems and it is likely you can achieve the same result faster outside the loop.

Due to the way WordPress works there may be some exceptions to this. Whileget_post_meta()will grab a meta value from the database, you can use it in a loop if you’re looping through one specific post’s metadata. This is because when you use it for the first time WordPress actually retrieves all metadata and caches it. Subsequent calls use the cached data, not database calls.

The best way to work these things out is to read function documentations and to use something like the Query Monitor.

 

Do Not Trust User Input

It is not wise to trust user input. Always filter, sanitize, escape, check and use fallbacks. There are three issues with user data: we developers don’t take every possibility into account, it is frequently incorrect and it may be intentionally malicious.

A well thought out system can protect against all of these. Make sure to use built in functions likefilter_var() to check for proper values and escaping and other functions when working with databases.

 

See More: 10 BEST IMAGES AND MEDIA PHP SCRIPTS

Do Not Use * In SQL Queries

this one is more of a MySQL issue, but we tend to write our SQL code in PHP so I say it’s fair game. In any case, don’t use wildcards in SQL queries if you can avoid them, especially if you have a database with a lot of columns.Specify the exact columns you need and only retrieve those. This helps minimize your resource usage, protect your data and make things as clear as possible.

While on the subject of SQL, know your available functions and test for speed as much as possible. When calculating averages, sums or similar numbers use SQL functions instead of PHP functions. If you are unsure of the speed of a query test it and try some other variations – use the best one.

You Might Also Like

5 Comments

  1. 2
    • 3

      Hi Karol Baczewski , I just advice to avoid the THINGS NOT TO DO IN PHP 7 because it cab be risk or bad practice to do those things but its not a mandatory that all the framework will follow those things. But most of the framework will avoid those things.
      Thanks

Leave a Reply