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
Creating data hashes
Writing your own functions
Recursive, variable, and callback functions
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
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.
Trigonometrical conversion 4.6.4.
Other mathematical conversion functions 4.6.5.
Base conversion 4.6.6.
Mathematical constants 4.7.
Playing with strings
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.
Basic regexes with preg_match() and preg_match_all() 4.8.2.
Novice regexes 4.8.3.
Advanced regexes 4.8.4.
Guru regexes 4.8.5.
Regular expression replacements 4.8.6.
Regular expression syntax examples 4.8.7.
A regular expression assistant 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.
Return values 4.15.2.
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.
Further reading 4.27.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get
over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Functions overview >>
Functions Functions overview How to read function prototypes Working with variables Controlling script execution Working with Date and Time Reading the current time Converting from a string Converting to a string Converting from components Mathematics Rounding Randomisation Trigonometrical conversion Other mathematical conversion functions Base conversion Mathematical constants Playing with strings Reading from part of a string Replacing parts of a string Converting to and from ASCII Measuring strings Finding a string within a string Returning the first occurrence of a string Trimming whitespace Wrapping your lines Changing string case Making a secure data hash Alternative data hashing Automatically escaping strings Pretty-printing numbers Removing HTML from a string Comparing strings Padding out a string Complex string printing Parsing a string into variables Regular expressions Basic regexes with preg_match() and preg_match_all() Novice regexes Advanced regexes Guru regexes Regular expression replacements Regular expression syntax examples A regular expression assistant Checking whether a function is available Extension functions Pausing script execution Executing external programs Connection-related functions Altering the execution environment User functions Return values Parameters Passing by reference Returning by reference Default parameters Variable parameter counts Variable scope in functions Overriding scope with the GLOBALS array Recursive functions Variable functions Callback functions The declare() function and ticks Handling non-English characters Undocumented functions Summary Exercises Further reading Next chapter
Table of Contents
Copyright ©2015 Paul Hudson.
Follow me: @twostraws.