DEV Community


Discussion on: PHP Tips and Tricks

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.

mychi_darko profile image
Michael Darko Author

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🤭