When we start our journey with programming, we don’t really think about coding styles and best practices. We just copy style of our teachers/mentors
You know this mindset “It works. Don’t touch it”, but let’s see how we can improve some of often-written lines, with much shorter and better-looking code
1. If-else statements
Let’s take a look at this code
<?php
if(empty($results)){
return $results;
}else{
return "Error 123";
}
It works, but we see that we have 2 return statments, just diffrent values
So let’s re-write it
return !empty($results) ? $results : "Error 123";
Looks better, doesn’t it?
Here we used ternary operator It’s useful, especially when we assign value to variable, like this
$role = is_admin($id) ? "Admin" : "User";
Checking for null values
Ok, now we know how to simplify our if-else statement. So let’s consider this scenario
We have login form with email, assigned to account, and some additional one, for security
With knowledge from last trick, our code looks like this:
$backupEmail = is_null($securityEmail) ? $securityEmail : $email;
Quite nice, but we can still improve it using null coalescing operator It’s similar to ternary, but it checks, whether value is null
So, this is our code now
$backupEmail = $securityEmail ?? $email;
And we have beautiful, simple code
again..
2. Prepared statements
In today’s back-end development with php, the safest method to run query’s are prepared statements. For binding parameters to query, we often use bindParam function.
//some code...
$stmt = $db->prepare($sql);
$stmt->bindParam(":user",$user , PDO::PARAM_STR);
$stmt->execute();
But here are some problems with it
- It requires to pass a variable
- If we have multiple variables to pass to the query, we must use bindParam a lot, which makes mess in our code
Let’s take a look into docs, more specifically, execute method parameters
An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as PDO::PARAM_STR.
So we don’t have to add new variables and binds! Let’s re-write it
$stmt = $db->prepare($sql);
$stmt->execute([$user]);
It may not make a big difference here but if we want to have some constant parameter, this method looks better and is faster to write
But it works, if we label parameters like in this query
SELECT * FROM `users` WHERE `nickname` = ?
But for this one, not really
SELECT * FROM `users` WHERE `nickname` = :user
So, how to fix it? Using associative arrays!
$stmt = $db->prepare($sql);
$stmt->execute([":user"=>$user]);
Now everything works fine.
3. match() function
In php 8.0 creators added a cool alternative for switch case, match() function
The advantage of match over switch-case is comparison method. Switch compares values using loose comparison (==), but in match we can see strict comparison (===), so in this code:
switch($age){
case "5": echo "wrong"; break;
case 5: echo "right"; break;
}
where $age = 5
, switch will match value for first case
but using match() like this
echo match ($age) {
"5" => "wrong",
5 => "right"
}
We will see “right” displayed in browser.
TL:DR
In this article I showed you
- Ternary operator
- Null coalescing operator
- Passing binds for query as parameter for execute()
- match() as an alternative for switch-case
I hope that this article helped you with writing better code. You can always leave some feedback in the comments, I appreciate that