5 mistakes that makes you a noob in PHP

rahul g
4 min readApr 19, 2022

--

Avoid These PHP Mistakes

Web Development is a huge industry itself in IT, And most of the time people start with PHP as a web developer. Teachers in colleges teach in a way such that students can understand the code and basics of scripting that how things work?

But as we grow, most of us not trying to learn the design pattern in PHP language, which makes the code base sucks! So, today I am going to show you what people think and what actually should happen while writing and managing code.

#1 Using Lower Version of PHP

Out-dated version of PHP
Outdated version of PHP

The first version was available sometime in early 1995 and was known as the Personal Home Page Tools. Later on 1st July 2004, PHP 5 was released. With this update, a lot of changes have been introduced. A lot of websites started to teach coding of PHP and features as well. But the most important thing in the history of programming is NULL safety, which destroyed and made a loss of millions of dollars.

As many developers were just newbies and never tried to figure out the other way + web development changes are being ignored due to high maintenance work, so these kinds of problems made developers switch to new projects without taking a break or upskilling, which cause a lot of outdated code in the world!

PHP 8 has been introduced, but people still using lower versions like PHP 5. There are two reasons here:-

  1. People are lazy enough to upgrade the skills
  2. They’re kind of stuck with the legacy codebase

But trust me PHP 7 introduced a lot of cool features, from PHP 7.3 (??) has introduced null safety through which we can avoid a lot of boilerplate conditions.

Fun Fact: MySql has been changed to Mysqli from PHP 5 and if(isset()) is changed to (??) for null safety in PHP 7, but people are still living in an imaginary world

#2 No MVC Pattern in Core PHP

People often have a complaint that they’re not able to structure Design Pattern out of the box and many articles and documentation don’t promote it. This makes confusion in the community about how to write code, which creates unstable code and creates conflicts.

People include the connection.php file in every file and write queries on top of the PHP tag. Then starts while loop in HTML by opening and closing PHP tags in between. This is the traditional approach to writing code in PHP but can be improved by introducing class and object.

The code itself tells the story, how clean it looks by adding helper class and how much maintainable it becomes.

#3 Handling Form Submission

if(isset()) is used every time when we submit a form using the POST method. This is used to validate if the form really submitted or not, but this can be improved as well.

#4 Using old syntax in newer versions

Null safety is provided by almost every programming language nowadays, but people are not aware of it. They still use the old syntax of language which makes them write a lot of boilerplate code. This can be easily improved by following trends in working programming language and by referring latest answers in StackOverflow instead of the accepted ones.

We can use traits, constants, Inheritance, and Elvis Operator for null safety (??).

Null Safety Short-way in PHP

#5 Not Using Laravel Framework

If you often feel stuck with core PHP, then it’s time to move to a new Powerful Framework called Laravel. It provides a lot of cool libraries and features out of the box. The framework makes life a lot easy and teaches us how to write well-maintained code.

# Using Nested MySQL queries in PHP Loop ~ Very Dangerous (Bonus Mistake)

Suppose we have to fetch the result from the database and according to that result we fetch the child results using a while loop using a nested MySQL query.

Trust me this practice is very harmful.

Here I touched the point in more detail and in a very interesting way.

So, these were my observations and point of view regarding the subject. I have literally seen people repeating the same thing again and again, without even thinking about why I am doing a lot of stuff. They don’t even think about how to improve the code and make it work in a few lines in a debug-friendly way.

Test-Driven Development is also very popular, but with core PHP it’s difficult to implement. So, as a developer, our duty is to write maintainable code and choose the right framework so that in the future the project can be well maintained. People already following the rat race, so be upgraded.

Thanks!

--

--