Purple pattern background

PHP programming shortcuts: save time and lines of useless code.

Mohammed NaserMohammed Naser

Before I even started writing this, I am sure that the comments will be filled about how consolidating code makes it hard to understand, more braces make it easier to read, etc. This is for those who think it's better to have less code, such as yours truly. I like seeing less code; I find it more efficient and much better.

Short-hand mathematical operators

Those are known by most programmers however missed by a lot, the following can be shortened by its alternative next to it.

Counters

Here is another very simple one, but good to know for those who don't know about it.

Short-hand print statements

Ever find yourself have to write so much just to be able to echo a variable in HTML? No more!

Could just be

Ternary operator

My personal favourite saves a lot of time and I still think it makes readable code

Or why not...

You can even set variables using that method

Verifying zero values not being false

Sometimes we deal with a function that can return a number and we want to make sure it returns a number, but if it returns 0, PHP will consider it as false.

The number can return 0 and be as false, whereas the following will not consider the integer 0 false

Alternative control structure syntax

This is a great feature to do when you're trying to do a lot of PHP work with HTML, it would be hard to list all of them but you can refer to the alternative syntax.

Feel free to post any other ones in the comments section.. we all have that one trick we always use!

Share on social media

Virtual machines, Kubernetes & Bare Metal Infrastructure

Choose from Atmosphere Cloud, Hosted, or On-Premise.
Simplify your cloud operations with our intuitive dashboard.
Run it yourself, tap our expert support, or opt for full remote operations.
Leverage Terraform, Ansible or APIs directly powered by OpenStack & Kubernetes