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

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

Next chapter: Functions overview >>

Previous chapter: Next chapter

Jump to:

 

Home: Table of Contents

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar