While using ‘if/else’ statements within a function or class, there is a common misconception that ‘else’ must be used
in conjunction to declare potential outcomes. However if the outcome is to define the return value, ‘else’ is not
necessary as ‘return’ will end the function, causing ‘else’ to become moot.
Double quotes are the Swiss army knife of strings, but are slower due to the string being parsed. They are best
Strings with multiple variables and plain text
Condensing multi-line concatenation, and improving readability
While using double quotes that contain variables, it is often the case that the variable will be touching another
character. This will result in PHP not parsing the variable due to the variable being camouflaged. To fix this problem,
wrap the variable within a pair of curly brackets.
Ternary operators are a great way to condense code, but are often used in excess. While ternary operators can be
stacked/nested, it is advised to use one per line for readability.
In comparison, here is an example that sacrifices all forms of readability for the sake of reducing the line count.
To ‘return’ a value with ternary operators use the correct syntax.
It should be noted that you do not need to use a ternary operator for returning a boolean value. An example of this would be.
This can also be said for all operations(===, !==, !=, == etc).
Utilising brackets with ternary operators for form and function
When utilising a ternary operator, brackets can play their part to improve code readability and also to include unions within blocks of statements. An example of when there is no requirement to use bracketing is:
Bracketing also affords us the capability of creating unions within a statement block where the block will be checked as a whole. Such as this example below which will return true if both ($a == 3 and $b == 4) are true and $c == 5 is also true.
Another example is the snippet below which will return true if ($a != 3 AND $b != 4) OR $c == 5.
At times, coders attempt to make their code “cleaner” by declaring predefined variables with a different name. What
this does in reality is to double the memory consumption of said script. For the example below, let us say
an example string of text contains 1MB worth of data, by copying the variable you’ve increased the scripts execution to