php

40 PHP Tips

1. Do not use relative paths, instead define a ROOT path

2. Don’t use require , include , require_once or include_once

3. Maintain debugging environment in your application

4. Propagate status messages via session

5. Make your functions flexible

6. Omit the closing php tag if it is the last thing in a script

7. Collect all output at one place, and output at one shot to the browser

8. Send correct mime types via header when outputting non-html content

9. Set the correct character encoding for a mysql connection

10. Use htmlentities with the correct characterset option

11. Do not gzip output in your application , make apache do that

12. Use json_encode when echoing JavaScript code from php

13. Check if directory is writable before writing any files

15. Don’t check submit button value to check form submission

16. Use static variables in function where they always have same value

17. Don’t use the $_SESSION variable directly

18. Wrap utility helper functions into a class

19. Bunch of silly tips

20. Process arrays quickly with array_map

21. Validate data with php filters

22. Force type checking

23. Write Php errors to file using set_error_handler()

24. Handle large arrays carefully

25. Use a single database connection, throughout the script

26. Avoid direct SQL query, abstract it

28. Store sessions in database

29. Avoid using globals

30. Use base url in head tag

31. Manage error reporting

32. Be aware of platform architecture

33. Dont rely on set_time_limit too much

34. Make a portable function for executing shell commands

35. Localize your application

36. Use a profiler like xdebug if you need to

37. Use plenty of external libraries

38. Have a look at phpbench for some micro-optimisation stats

39. Use an MVC framework

40. Read the comments on the php documentation website

41. Read open-source code

42. Develop on Linux


 

Resources

https://www.binarytides.com/35-techniques-to-enhance-your-php-code/

1 thought on “40 PHP Tips”

Leave a Comment

Your email address will not be published. Required fields are marked *