DPC2020: Top 10 PHP coding traps - Damien Seguy

PHP developers, beware of common coding traps and improve your code quality with expert insights and best practices from Damien Seguy.

Key takeaways
  • Be aware of the different types of comparisons, such as loose and strict equality, and avoid using loose equality (==) when possible.
  • Use parentheses to clarify the order of operations when using complex expressions.
  • Avoid using the array operator ([]) with a function call, as it can lead to unexpected behavior.
  • Use trim() and strip_tags() to cleanse input data before processing it.
  • Be mindful of the difference between isset() and array_key_exists(), as they behave differently.
  • Avoid using echo to concatenate strings, as it can lead to unexpected behavior.
  • Use . to concatenate strings, and avoid using + to concatenate strings, as it can lead to unexpected behavior.
  • Be aware of the memory implications of using array_concat() and implode().
  • Consider using constants instead of static variables.
  • Be aware of the limitations of define() and consider using const instead.
  • Avoid using strpos() without specifying the second argument, as it can lead to unexpected behavior.
  • Be mindful of the difference between Officers and () when using function calls.
  • Avoid using eval() and create_function() when possible, as they can lead to security vulnerabilities.
  • Consider using a code analyzer, such as PHPStan, to detect errors and improve code quality.
  • Be aware of the different memory allocation strategies in PHP, and consider using clearstatcache() to clear the cache.
  • Avoid using include() with a bare path, as it can lead to security vulnerabilities.
  • Consider using namespace to organize code and avoid naming conflicts.
  • Be aware of the differences between PHP versions, and consider upgrading to a newer version if possible.
  • Consider using a JavaScript runtime, such as Node.js, to run PHP code.
  • Avoid using passthru() with an undefined variable, as it can lead to unexpected behavior.