PHP Matters Needing Attention
Tips
Prefer static when possible
If a function can be made static, prefer to make it static.
The difference mainly shows up in memory handling: static methods allocate memory when the program starts, whereas instance methods allocate memory only when the object is instantiated.
Static methods can be called directly, while instance methods require you to first create an instance and then call through it.
Too many static methods will consume memory.
echo VS print
echo is faster than print, because echo has no return value while print returns an integer.
When echoing large strings, the corresponding configuration needs to be done on the server.
echo multiple strings
When echo-ing multiple strings, use
,instead of.to join them.@ error suppression
Using @ to suppress errors slows down the script. In particular, avoid using @ inside loops.
$row[‘id’] & $row[id] & $row[1]
'id'looks up the key'id'directly. Without quotes, a variable or constant is parsed by first determining its type and then resolving the value.isset() & empty()
isset() tests whether a variable has been assigned.
empty() tests whether an already-assigned variable is empty. Referencing a variable that hasn’t been assigned is allowed but produces a notice.If a variable is assigned an empty value
$t = ""; $t = 0; $t = false;, bothempty($t)andisset($t)return true.
To destroy a variable, useunset($t)or$t = NULL.Confirm the maximum count before looping
Determine the maximum count before entering a for loop; don’t recompute the maximum on every iteration.
1
2
3
4
5
6
7
8// Don't do this
for ($i=0;$i<=count($array);$i++){
}
// Do this instead
$len = count($array);
for ($i=0;$i<=$len;$i++){
}include() & include_once() & require() & require_once()
All four load a file. The
_oncevariants first check whether the file has already been loaded, to avoid loading it twice.
The main differences are:- Error handling
require()throws afatal errorand halts the script if the file doesn’t exist.include()throws awarningbut the script continues if the file doesn’t exist.
- Performance
require()processes the file only once (in effect, the file’s content replaces the require() statement).include()reads and evaluates the file on every execution.
If code containing one of these directives may run multiple times,require()is more efficient.
- Flexibility
require()is usually placed at the very top of a script; the specified file is loaded before execution, becoming part of the script.include()is usually placed inside control-flow blocks. The file is loaded only when execution reaches it. This can simplify the runtime flow.
- Error handling
Use full paths when including files, to reduce the time spent resolving paths.
‘ ‘ & “ “
PHP allows both single and double quotes to delimit string variables.
" "first reads the string contents, then looks for variables inside it and interpolates their values.Don’t copy variables carelessly
Copying one variable into another doubles the memory consumption.
if else & switch case
A switch/case is better than a long chain of if/else if statements, and the code is easier to read and maintain.
Not everything has to be object-oriented
Object orientation often carries significant overhead; every method call and object instantiation consumes memory.
Don’t split methods too finely
Every method call consumes memory.
Prefer PHP’s built-in functions where possible.
Don’t declare variables inside loops, especially large ones: objects.
Destroy variables to release memory, especially large arrays.
Use string functions instead of regular expressions.
split is faster than explode.