Functions
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
Chapter contents
- 4.1. Functions overview
- 4.2. How to read function prototypes
- 4.3. Working with variables
- 4.4. Controlling script execution
- 4.5. Working with Date and Time
- 4.5.1. Reading the current time
- 4.5.2. Converting from a string
- 4.5.3. Converting to a string
- 4.5.4. Converting from components
- 4.6. Mathematics
- 4.6.1. Rounding
- 4.6.2. Randomisation
- 4.6.3. Trigonometrical conversion
- 4.6.4. Other mathematical conversion functions
- 4.6.5. Base conversion
- 4.6.6. Mathematical constants
- 4.7. Playing with strings
- 4.7.1. Reading from part of a string
- 4.7.2. Replacing parts of a string
- 4.7.3. Converting to and from ASCII
- 4.7.4. Measuring strings
- 4.7.5. Finding a string within a string
- 4.7.6. Returning the first occurrence of a string
- 4.7.7. Trimming whitespace
- 4.7.8. Wrapping your lines
- 4.7.9. Changing string case
- 4.7.10. Making a secure data hash
- 4.7.11. Alternative data hashing
- 4.7.12. Automatically escaping strings
- 4.7.13. Pretty-printing numbers
- 4.7.14. Removing HTML from a string
- 4.7.15. Comparing strings
- 4.7.16. Padding out a string
- 4.7.17. Complex string printing
- 4.7.18. Parsing a string into variables
- 4.8. Regular expressions
- 4.8.1. Basic regexps with preg_match() and preg_match_all()
- 4.8.2. Novice regexps
- 4.8.3. Advanced regexps
- 4.8.4. Guru regexps
- 4.8.5. Regular expression replacements
- 4.8.6. Regular expression syntax examples
- 4.8.7. The regular expressions coach
- 4.9. Checking whether a function is available
- 4.10. Extension functions
- 4.11. Pausing script execution
- 4.12. Executing external programs
- 4.13. Connection-related functions
- 4.14. Altering the execution environment
- 4.15. User functions
- 4.15.1. Return values
- 4.15.2. Parameters
- 4.15.3. Passing by reference
- 4.15.4. Returning by reference
- 4.15.5. Default parameters
- 4.15.6. Variable parameter counts
- 4.16. Variable scope in functions
- 4.17. Overriding scope with the GLOBALS array
- 4.18. Recursive functions
- 4.19. Variable functions
- 4.20. Callback functions
- 4.21. The declare() function and ticks
- 4.22. Handling non-English characters
- 4.23. Undocumented functions
- 4.24. Summary
- 4.25. Exercises
- 4.26. Further reading
- 4.27. Next chapter



Copyright 2012 Future Publishing Limited (company
registered number 2008885), a company registered
in England and Wales whose registered office is at
Beauford Court, 30 Monmouth Street, Bath, BA1 2BW, UK