Practical PHP Programming:Functions
From IpbWiki
Functions, both ones built into PHP and ones you define yourself, make coding much easier - they take away lots of hard work because you can reuse other people's code, and they allow you to keep your scripts shorter and easier to maintain. As PHP 5 includes more than 2,500 functions, you might assume it's a very easy language indeed, but the truth is that each function needs to be used in different ways and so needs to be learnt individually. In this chapter you will learn your first PHP functions, with the most helpful and easy first.
Rather than writing pieces of code time after time whenever you want to execute the same functionality, PHP allows you to encapsulate code into a named function that you can call from elsewhere in your script.
PHP comes with hundreds of predefined functions that perform all manner of tasks from reading files and manipulating strings up to querying databases and connecting to an IRC server. If you find something is missing, you can add your own functions on a script by script basis, and these are called user functions.
In this section we will be covering a variety of the most important basic functions in PHP - more specialised functions can be found spread throughout the book under various sections, and should be looked up using the index.
Topics covered in this chapter are:
- Working with date and time
- Mathematical functions
- String manipulation
- Creating data hashes
- Regular expressions
- Extension handling
- Writing your own functions
- Recursive, variable, and callback functions
Contents
- 3.1. Practical_PHP_Programming:Functions overview
- 3.2. Practical_PHP_Programming:How to read function prototypes
- 3.3. Practical_PHP_Programming:Working with variables
- 3.4. Practical_PHP_Programming:Controlling script execution
- 3.5. Practical_PHP_Programming:Working with date and time
- 3.6. Practical_PHP_Programming:Mathematics
- 3.6.1. Practical_PHP_Programming:Rounding numbers
- 3.6.2. Practical_PHP_Programming:Generating random numbers
- 3.6.3. Practical_PHP_Programming:Trigonometry functions
- 3.6.4. Practical_PHP_Programming:Other mathematical conversion functions
- 3.6.5. Practical_PHP_Programming:Base conversion
- 3.6.6. Practical_PHP_Programming:Mathematical constants
- 3.7. Practical_PHP_Programming:Playing with strings
- 3.7.1. Practical_PHP_Programming:Reading from part of a string
- 3.7.2. Practical_PHP_Programming:Replacing parts of a string
- 3.7.3. Practical_PHP_Programming:Converting characters to and from ASCII
- 3.7.4. Practical_PHP_Programming:Measuring strings
- 3.7.5. Practical_PHP_Programming:Finding a string within a string
- 3.7.6. Practical_PHP_Programming:Finding the first occurrence of a string
- 3.7.7. Practical_PHP_Programming:Trimming whitespace
- 3.7.8. Practical_PHP_Programming:Wrapping long strings over multiple lines
- 3.7.9. Practical_PHP_Programming:Changing the case of a string
- 3.7.10. Practical_PHP_Programming:Making a secure data hash
- 3.7.11. Practical_PHP_Programming:Alternative data hashing
- 3.7.12. Practical_PHP_Programming:Automatically escaping strings
- 3.7.13. Practical_PHP_Programming:Pretty-printing numbers
- 3.7.14. Practical_PHP_Programming:Removing HTML from a string
- 3.7.15. Practical_PHP_Programming:Comparing strings
- 3.7.16. Practical_PHP_Programming:Padding out a string
- 3.7.17. Practical_PHP_Programming:Complex string printing
- 3.7.18. Practical_PHP_Programming:Parsing a string into variables
- 3.8. Practical_PHP_Programming:Regular expressions
- 3.8.1. Practical_PHP_Programming:Basic string matching with regular expressions
- 3.8.2. Practical_PHP_Programming:Novice regular expressions
- 3.8.3. Practical_PHP_Programming:Advanced regular expressions
- 3.8.4. Practical_PHP_Programming:Guru regular expressions
- 3.8.5. Practical_PHP_Programming:Regular expression replacements
- 3.8.6. Practical_PHP_Programming:Regular expression syntax examples
- 3.8.7. Practical_PHP_Programming:The regular expressions coach
- 3.9. Practical_PHP_Programming:Checking whether a function is available
- 3.10. Practical_PHP_Programming:Extension functions
- 3.11. Practical_PHP_Programming:Pausing script execution
- 3.12. Practical_PHP_Programming:Executing external programs
- 3.13. Practical_PHP_Programming:Connection-related functions
- 3.14. Practical_PHP_Programming:Altering the execution environment
- 3.15. Practical_PHP_Programming:User functions
- 3.15.1. Practical_PHP_Programming:Return values
- 3.15.2. Practical_PHP_Programming:Accepting parameters
- 3.15.3. Practical_PHP_Programming:Passing parameters by reference
- 3.15.4. Practical_PHP_Programming:Returning parameters by reference
- 3.15.5. Practical_PHP_Programming:Default values for parameters
- 3.15.6. Practical_PHP_Programming:Accepting a variable number of parameters
- 3.16. Practical_PHP_Programming:Variable scope in functions
- 3.17. Practical_PHP_Programming:Overriding variable scope with the GLOBALS array
- 3.18. Practical_PHP_Programming:Recursive functions
- 3.19. Practical_PHP_Programming:Functions to handle functions
- 3.20. Practical_PHP_Programming:Callback functions
- 3.21. Practical_PHP_Programming:A brief introduction to Ticks
- 3.22. Practical_PHP_Programming:Handling non-English characters
- 3.23. Practical_PHP_Programming:Undocumented functions
