DEV Community

Cover image for PHP Tips and Tricks

PHP Tips and Tricks

Michael Darko on March 16, 2021

Working with PHP has been one of the most fun experiences for me, working on both major and minor projects and learning something new on every jour...
Collapse
 
phantas0s profile image
Matthieu Cneude

Oh. An article about PHP. I thought Tailwind / React / JS were eating them all.

I've been advocating the first tip for years. Now, I can't see why it's better anymore. The if else make the code clearer in fact: it's less easy to miss than the return statement.

I think it just pleases our... sense of beauty? Code formatting is 99% subjective IMHO.

Collapse
 
nicolus profile image
Nicolus

I keep going back and forth on the "return early" style.

The main advantage for me is that you get back 4 precious spaces at the beginning of every line in your "main" path. The disadvantage as you said is it's easier to see a return statement at the end of the function and miss the fact that it could have returned something else entirely.

At the end of the day, I guess if either of those is a real issue, it just means your method is too complicated and it's time to refactor by adding a new simpler method.

Collapse
 
leob profile image
leob

"Early return" FTW ;)

Collapse
 
nicolus profile image
Nicolus • Edited

no, this is not a templating engine, this is just PHP making things simple for us.

insert obligatory "But PHP is a templating engine" remark here. It hasn't been used like one in years, but originally it was a templating engine, which is why we can do this, and why to this day every php file has to start with <?php

Collapse
 
mychi_darko profile image
Michael Darko

๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚ Good one. However, PHP has moved on from that little extension it used to be.

Collapse
 
goodevilgenius profile image
Dan Jones • Edited

The first tip is a really important one, although, with that particular example, with something so simple, a ternary would be more appropriate.

function output_gender(bool $user_is_male) {
    return $user_is_male ? "User is male" : "User is female";
}
Enter fullscreen mode Exit fullscreen mode

Also, one more tip related to this example: careful with how you name stuff. This function is called output_gender, but it doesn't actually output anything. It simply returns a string indicating the gender. Only call something output, or print if it actually does that.

All in all, I would rewrite this function as:

function getGender(bool $userIsMale): string {
    return $userIsMale ? "male" : "female";
}
Enter fullscreen mode Exit fullscreen mode

I also prefer camelCase, but that's just a personal preference, and doesn't really affect readability or anything.

There's also a whole other discussion about whether it's appropriate to make assumptions about binary gender in your code. But that's not exactly the point of this discussion.

Collapse
 
mychi_darko profile image
Michael Darko

Thanks๐Ÿ˜Š I agree that ternary would be better for that example, but I was just trying to make a point. A little secret, I prefer camel case too๐Ÿคญ

Collapse
 
ones66790 profile image
ones66790 • Edited

Yes, PHP is a very fun programming language, this one of my last which I started to learn, the most favorite by far, and nice article by the way, I enjoyed reading it. These tips are very useful, because a lot of beginners get stuck in situations like this one, and have no idea what to do. I became a fan of coding, after I got A/B testing service getparthenon.com, for my website on which I was working. I was a beginner at that moment and know almost nothing, and the guys helped me to set up everything and get it running.

Collapse
 
sznroe profile image
Sujan Rai

Loved It

Collapse
 
brnathanlima profile image
Nathanael Lima

Great tips. Somo of them I already use and the others I'll try to use for now on.

Collapse
 
aalphaindia profile image
Pawan Pawar

Nice one!!

Collapse
 
koas profile image
Koas

Very nice article! I didnโ€™t know about the endif, thanks!

Collapse
 
torstendittmann profile image
Torsten Dittmann

Interesting read and great article ๐Ÿ‘

Collapse
 
mychi_darko profile image
Michael Darko

Thanks๐Ÿ˜Š

Collapse
 
abidkhairyak profile image
AbidKhairyAK

nice article! I think this is enough for my dinner haha...