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...
For further actions, you may consider blocking this person and/or reporting abuse
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.
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.
"Early return" FTW ;)
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
๐๐๐ Good one. However, PHP has moved on from that little extension it used to be.
The first tip is a really important one, although, with that particular example, with something so simple, a ternary would be more appropriate.
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 somethingoutput
, orprint
if it actually does that.All in all, I would rewrite this function as:
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.
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๐คญ
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.
Loved It
Great tips. Somo of them I already use and the others I'll try to use for now on.
Nice one!!
Very nice article! I didnโt know about the endif, thanks!
Interesting read and great article ๐
Thanks๐
nice article! I think this is enough for my dinner haha...