While using ‘if/else’ statements within a function or class method, 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.
Strings are a series of characters, which should sound fairly simple. That said, there are a few different types of
strings and they offer slightly different syntax, with slightly different behaviors.
Single quotes are used to denote a “literal string”. Literal strings do not attempt to parse special characters or
If using single quotes, you could enter a variable name into a string like so: 'some $thing', and you would see the
exact output of some $thing. If using double quotes, that would try to evaluate the $thing variable name and show
errors if no variable was found.
Double quotes are the Swiss Army Knife of strings. They will not only parse variables as mentioned above, but all sorts
of special characters, like \n for newline, \t for a tab, etc.
Double quotes can contain variables; this is called “interpolation”.
When using interpolation, it is often the case that the variable will be touching another character. This will result
in some confusion as to what is the name of the variable, and what is a literal character.
To fix this problem, wrap the variable within a pair of curly brackets.
It should be noted that multiline strings can also be formed by continuing them across multilines in a statement. e.g.
Which is quicker?
There is a myth floating around that single quote strings are fractionally quicker than double quote strings. This is
fundamentally not true.
If you are defining a single string and not trying to concatenate values or anything complicated, then either a single
or double quoted string will be entirely identical. Neither are quicker.
If you are concatenating multiple strings of any type, or interpolate values into a double quoted string, then the
results can vary. If you are working with a small number of values, concatenation is minutely faster. With a lot of
values, interpolating is minutely faster.
Regardless of what you are doing with strings, none of the types will ever have any noticeable impact on your
application. Trying to rewrite code to use one or the other is always an exercise in futility, so avoid this
micro-optimization unless you really understand the meaning and impact of the differences.
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
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
Another example is the snippet below which will return true if ($a != 3 AND $b != 4) OR $c == 5.
Since PHP 5.3, it is possible to leave out the middle part of the ternary operator.
Expression “expr1 ?: expr3” returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.