You want to pass a variable to a function and have it retain any changes made to its value inside the function. To instruct a function to accept an argument passed by reference instead of value, prepend an & to the argument name. You can think of a reference as a signpost that points to a variable. In working with the reference, you are manipulating the value to which it points.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php function Add(&$num1, $num2 = "3") { echo "$num1 + $num2 = ", $num1+$num2, "<br />"; } $val = 2; Add($val); function display_bold(&$string, $tag = 'b') { $string = "<$tag>$string</$tag>"; } $string="Hello World"; display_bold($string); echo $string; ?> </p> </body> </html> |