Parameters
You can design your functions to accept parameters by modifying the definition to include as many as you want. You need to give each parameter the name you will be using to refer to it inside the function - when you later call that function, PHP will rename the values to fit your function definition, like this:
<?php
function multiply ( $num1 , $num2 ) {
$total = $num1 * $num2 ;
return $total ;
}
$mynum = multiply ( 5 , 10 ); ?>
After running that script, $mynum will be set to 50. The multiply() function could have been rewritten so that it was just one line: return $num1 * $num2, but it is good to show that you can make your functions as long as you want.
Next chapter: Passing by reference >>
Previous chapter: Return values
Jump to: 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 regexps with preg_match() and preg_match_all() Novice regexps Advanced regexps Guru regexps Regular expression replacements Regular expression syntax examples The regular expressions coach 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
Home: Table of Contents
Follow us on Identi.ca or Twitter