7 Ways PHP Code is Misused by Developers

  • Home
  • Blog
  • 7 Ways PHP Code is Misused by Developers
image
image
image
7 Ways PHP Code is Misused by Developers

7 Ways PHP Code is Misused by Developers

If you are a developer, you will be very well aware of the challenge that every developer faces while improving their coding style.

Today we will talk about the seven ways both beginner, and advanced developers misuse the PHP code.

PHP stands for the Hypertext Preprocessor and is a widely-used general-purpose scripting language.

This language is mainly used for web development after embedding it into HTML.

Let's talk about how PHP code can be misused:

Losing all your customers:

PHP comes with a lot of functions that can be dangerous if used without proper knowledge.

Developers should be aware of the hazardous data breach your code can cause and should write code responsibly.

For example;

DROP FROM Customers; where id = 1

In the above example, the semicolon is where you can risk your entire customer table set rather than just deleting the customer with the ID of 1.

Developers need to take care of all the functions and the methods available, based on which ones are required and which ones are not required.

If a programmer uses a function that can wipe off the complete data, it is not worth taking that risk.

Functions with vague names:

Sometimes programmers get distracted from coding only and generate some functions with incomprehensible names.

Even experienced developers and programmers make these mistakes.

For example, a function name as "doSomething" is too vague to get hold of and causes a problem when another developer works on it.

It lowers the readability of the code and also makes it challenging to maintain.

Instead, if you are naming a function or a variable, use 2-3 words to make them more detailed and accurate.

The magic number and Magic Strings:

A magic number is any such number that has no name or explanation for its existence in a particular piece of code.

Similarly, a string is an array of characters that is followed by a null character.

In this example, 5 is the magic number.

for($i = 0; $i < 5; $i++) { //any executable set of statement }

The code doesn't explain why it needs to run five times.

for($i = 0; $i < $checksencounter; $i++) { //any executable set of statement }

Since we have replaced the number 5 with a variable name check encounter, it will contain how many checks it counts.

Now that variable is not a magic number because it has a strong reason why it exists there.

Functions that Multitask?

A function is only practical if it's successful in executing only one thing and performs it efficiently.

If a developer assigns a function with more than one thing to do and later on you want to change that and keep one of the tasks, you can't make that change quickly.

For example in this code,

function validatePhoneNumberThenWrapInHtml($string) { // replaces the non digit entries $onlyDigits = preg_replace('/[^0-9]/','',$string); // checks the length of the the remaining string if its 7-14 long or not $onlyDigitsLen = strlen($onlyDigits); $isValidPhoneNumber = ($onlyDigitsLen >= 7 && $onlyDigitsLen <= 14); // prints a warning if its not a number if(!$isValidPhoneNumber) $onlyDigits = "Not a phone number"; // returns the phone number return "{$onlyDigits}";

In the above example, the Function validatePhoneNumberThenWrapInHtml is performing two jobs,

  1. It checks whether the input is a string or a number
  2. Outputs the phone number which the Function got, or it just prints out the warning.

So, to overcome this menace, we can introduce two different functions which will be performing the same functions as the previous Function was trying to accomplish,

Duplicate of code

If you repeat yourself or the code you are writing, it means you are breaking the essential rule of coding, which is Don't Repeat Yourself(DRY).

The best way to avoid these kinds of mistakes is to wrap the code in functions and then use the necessary functions when needed.

Not using built-in PHP functions.

Sometimes we programmers try to make out their functions instead of using the built-in functions that might provide us with the functionality we need.

It is always recommended not to "Reinvent the wheel."

You can use the existing functions which have been prevalent for a while now and offer more stability and advantages.

Because if someone else has to continue working on it, they can easily do that.

Not using Functions:

It happens that developers prefer writing dozens of lines of code and don't prefer using functions.

Developers should use functions because besides providing the ability to wrap the data, it also prevents coders from writing long codes, which is time-efficient.

Since the functions are pre-compiled, it makes the job easier.

These were some of the mistakes which developers/ prommers make while working with PHP.

So if you are going to work with PHP language, keep these things in your mind.

You may also like:

Why is Custom PHP Development Preferred 70% Of The Time?
8 Fundamental Stages in Web App Development
RESO Vs RETS