Common mistakes made by php developers

Common mistakes made by php developers

PHP is a server-side language widely used in static & dynamic web pages and web applications. No matter of the level of skill sets you have as a php or backend developer; there are tons of mistakes you are likely to make while coding. The errors in web development can do a lot of damage in terms of users experience and push the visitors away.

1.Getting a value in an Array without looping through: This kind of mistakes are common with newbies while code, and hence the need to understand that you have to loop through the variable of an array before getting the value of the item.



$posts = array(
    "title" => "post 1",
    "id" => "1",
    "author" => "john doe"
        "title" => "post 2",
        "id" => "2",
        "author" => "john doe"
		// this array of posts has two items  print_r($posts) ; this would return an array of posts 
echo $post[‘title’]; // this would either throw an error or return null, because the title is in an array item, and hence you need to loop through it to get the title 
foreach($posts as $post){
    // looping through each posts
    echo $post['title'].'<br>'; //gets only the title
    print_r($post) ; //list of posts in this array

2. Using query inside a loop: Most of the time we use to add queries inside the loop. There is nothing wrong when we see the logic of the code. But suppose there are 200 records are there and we are calling query for each of them, then we are requesting 200 resources from MySQL. 

This type of code will slow the execution of the code and lots of MySQL resources will be miss utilized.  We can create an array of the ids and pass at once in the query by using the "in" query option of MySQL to get the desired result. This can be solved by using the implode which will convert the array of values to comma-separated values.

                   $record= [];
                  $ids= array of ids you want to get their data
                  if (count($ids)) {
                     $result = $link->query("SELECT * FROM `users` WHERE `id` IN (" . implode(',', $ids));
                             while ($row = $result->fetch_row()) {
                           $record[] = $row;

This is an optimized method instead of querying each records nth times.

3. Single and double quotes: Double quotes are commonly used when concatenating strings because it parses everything neatly without having to deal with escaping characters and using dot values. The advantages of a double quote over a single one is that, it reads everything including the defined variable without having to concatenate.

$myquotes = 'everyone';
echo $string1 = 'hello $myquotes’;
echo  $string2 = "hello $myquotes ";

4. Semicolon: this little character can cause havoc to your code and leaves you with a tons of hours spent on debugging.

$i = 0 //without a closing semicolon after this code, it would throw an error.

5. Wrong Operators: this is a very common mistake we all make while using the comparison operator. Sometimes we use “=” instead of comparison “==”. This mistake can change the value and logic of your code.

            $i <= 4;
            $i = 4;
            The values of the above code are different
             Likewise the comparison
               $name = Jacob // this is used as definition of variable
                $name == Jacob // this compares if the name is jacob
                 $name === Jacob // this compares the item and also consider the strings and data type

6. Privileged access: Permitting users, groups and others are very important. You should not leak the control panel of an administrator to a mere user on a webpage. You should be very careful while adding permission to folders, files and database users.  This can be taking care of by adding sessions to pages and to check if the current user has that session then allow to view a particular page.

if ($_SESSION[‘email’] == $email) { // do sensitive things here

Another way of doing this is through cookies and cauche data (this is an insecure method)

With the advancement in technology, privileges and protected pages can be taken care of by middleware (this is commonly used in frameworks such as Laravel).

7. Not Escaping Entities: It is imperative to sanitize all data before it is stored in any type of storage, including a database. Escaping entities would protect your web pages against cyber attacks such as sql injection. You can validate the data by using htmlentities() (or htmlspecialchars()) as follows:

echo $_POST[‘username’];
       validate data with: echo htmlspecialchars($_POST [‘username’]);

Have you had an experience of a similar or other mistakes while coding? Let us hear your experience in the comment.


PHP is still a great choice for web development and its easily used in communicating with the server in the backend. While working as a backend developer its highly essential that we write our codes with best practices and avoid errors that can cause the web application to break or cause bad user experience.


Enjoy this article ? drop a comment below and add your own contribution.

Maye Jacob

Jacob Olorunmaye

Verified Expert
Passionate Backend Developer | Expert in PHP, Laravel, JavaScript, and Golang 🚀 | Crafting seamless, scalable solutions for the digital era

Leave a Reply

Your email address will not be published. Required fields are marked *