X
    Categories: PHPTips

5 Mistakes that make you look like a noob in PHP

Web Development is a huge sector in IT and most of the time, people begin using PHP as web developers. Teachers at colleges instruct in a manner that students understand the language and the fundamentals of scripting. How do things function?

However, as we age the majority of us are not seeking to master the design pattern of PHP programming language. This means that the code base is unreadable! This is why I will demonstrate the way people think and what is actually required to happen when making and maintaining code.

#1 Using the Lower version of PHP

The first version of PHP was released around the beginning of 1995 and was called The Personal Homepage Page Tools. Then, on July 1st, 2004, PHP 5 was released. This update brought many modifications have been made. Many websites have begun to teach programming of PHP and other features. The most significant thing that programming has ever seen is its NULL security which caused the destruction of millions of dollars.

Many developers were novices who never attempted to find out an alternative method. Web development updates aren’t being taken into consideration because of high maintenance which is why these issues caused developers to switch to new projects, without stopping or retraining to create a large amount of obsolete code all over the world!

PHP 8.8 was introduced yet many people are using lower versions such as PHP 5. There are two reasons There are two reasons:

  1. It is not enough for lazy people to learn new skills.
  2. They’re pretty stuck with the codebase of the past.

However, I can assure you that PHP 7 brought a variety of exciting features. PHP 7.3 (? ) introduced null safety by which we can stay clear of many boilerplate scenarios.

A Fun Fakt: MySql has been changed to Mysqli from PHP 5. if(isset()) was changed (??) to ensure null security in PHP 7, however, we remain in a fictional world.

#2 No MVC pattern in PHP Core

There are many who complain that they’re unable to create a Design Pattern out of the box, and a lot of publications and documents don’t encourage it. This creates confusion in the community regarding the best way to code which leads to unsteady code and causes conflicts.

Is PHP dead or still has a chance for Web Development?

Many people add their connection.php file in every file and then write queries using that PHP tag. Then it begins looping through HTML with the opening and closing of PHP tags between. This is the conventional method of creating code using PHP but it is possible to improve it by introducing classes and objects.

<?php

require_once 'constant.php';

class ProductHelper

{

   private $conn;

   private $limit;



   public function __construct()

   {

      $this->conn = mysqli_connect(server, user, password, db) or die("Couldn't connect");

      $this->limit = 1;

   }





   public function fetchLinks()

   {

      $total_page_query = "SELECT * FROM `product`";

      $exe = mysqli_query($this->conn, $total_page_query);

      $total = mysqli_num_rows($exe);



      // How many pages will there be

      return ceil($total / $this->limit);

   }



   public function fetchProducts($page = 1)

   {

      $limit = $this->limit;



      // Calculate the offset for the query

      $offset = ($page - 1) * $limit;

      $query = "SELECT * FROM `product` LIMIT $offset,$limit";

      return $this->conn->query($query);

   }

}

This code’s own code depicts how clean it appears with the addition of helper classes and how easily maintainable it gets.

<?php

	include 'helper/ProductHelper.php';

	

	$helper = new ProductHelper();

	$totalPages = $helper->fetchLinks();

	$page = $_GET['page'] ?? "1"; // Default value if page key not exist in query string

	$products = $helper->fetchProducts($page);

	?>

	

	<!-- Loop Starts -->

	<?php

	while ($product = $products->fetch_object()) { ?>

	

	<!-- Random HTML Div -->

#3 Handling Form Submission

if(isset()) is utilized every time you submit a form with the method of POST. This method checks whether the form has been submitted, or not. However, this could be improved.

<form action="submit.php" method="post">

	<input type="text" name="message" placeholder="Enter Message" required><br>

	<input type="email" name="email" placeholder="Enter Email" required><br>

	<button name="submit" value="Submit">Submit</button><br>

</form>
<?php

	

	include 'mail.php';

	

	$data = $_POST; // No need to create variable of each field

	

	if(empty($data)){ // check wheather form has data or not

	header("Location: form.php");

	return;

	}

	

	$message = $data['message'] ?? null;

	$email = $data['email'] ?? null;

#4 Using the old syntax in the newer versions

Null security is a feature of nearly every programming language today however, people aren’t conscious of it. They continue to use outdated syntax that creates a lot of code that is boilerplate. This can be improved by keeping track of the latest developments in the working language and by looking up the latest responses on StackOverflow instead of the standard ones.

We can make use of traits constants, constants, inheritance, and Elvis Operator to provide non-existent security ( What do you think? ).

<?php

	$a = null;

	// Old Way

	if(!$a){

	echo "Hello World";

	}

	else{

	echo $a;

	}

	// New Way

	echo $a ?? "Hello World"; // New Text if variable is NULL

#5 Not Utilizing Laravel Framework

If you find yourself stuck in basic PHP It’s the right time to upgrade to a brand new Powerful Framework called Laravel. It offers a wide range of awesome options and libraries right from the beginning. This framework can make life easier and shows us how to write clean code.

# Use of Nested MySQL queries within the PHP Loop – Very Risky (Bonus mistake)

If we need to retrieve the results from the database. Then, according to that result, we get the child results by using an in-loop using nesting MySQL queries.

I guarantee you that this method is very dangerous.

These are my thoughts and points of view on the matter. I’ve witnessed people do the same mistake over and over repeatedly, without considering the reason behind many things. They don’t even consider ways to enhance the code to get it to work in just only a couple of lines in a way that is debug-friendly.

Test-Driven Development is also popular, however, when it comes to the core PHP it’s hard to implement. As a developer, it is our responsibility to create code that can be maintained and then select the appropriate framework to ensure that the project will be well-maintained. Anyone who is already in the race for death should upgrade your software.

Thanks!

Huzoor Bux: I am a PHP Developer