Comparison operators are an often overlooked aspect of PHP, which can lead to many unexpected outcomes. One such
problem stems from strict comparisons (the comparison of booleans as integers).
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.
When using namespaces, you may find that internal functions are hidden by functions you wrote. To fix this,
refer to the global function by using a backslash before the function name.
String types are a constant feature within the PHP community, but hopefully this section will explain the
differences between the string types and their benefits/uses.
Single quotes
Single quotes are the simplest way to define a string and are often the quickest. Their speed stems from PHP not
parsing the string (doesn’t parse for variables). They’re best suited for:
Double quotes are the Swiss army knife of strings, but are slower due to the string being parsed. They’re best
suited for:
Escaped strings
Strings with multiple variables and plain text
Condensing multi-line concatenation, and improving readability
While using double quotes that contain variables, it’s 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.
Nowdoc syntax was introduced in 5.3 and internally behaves the same way as single quotes except it’s suited toward the
use of multi-line strings without the need for concatenating.
Heredoc syntax internally behaves the same way as double quotes except it’s suited toward the use of multi-line
strings without the need for concatenating.
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.
To ‘return’ a value with ternary operators use the correct syntax.
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’s say
an example string of text contains 1MB worth of data, by copying the variable you’ve increased the scripts execution to
2MB.