: This article mainly introduces php code reuse and functions. For more information about PHP tutorials, see.
'; Require ('reusable. php'); echo 'The script will end now.
'; // Function declaration function my_function () {echo 'My function was called';} my_function (); // use the function create_table ($ data) {echo"
"; Reset ($ data); $ value = current ($ data); while ($ value) {echo"
| ". $ Value ." |
\ N "; $ value = next ($ data);} echo"
";}$ My_array = array ('line one', 'line two', 'line three '); create_table ($ my_array); // scope keyword globalfunction fn () {global $ var; $ var = "contens 2"; echo "inside the function, \ $ var = ". $ var."
";}$ Var =" contents 1 "; fn (); echo" outside the function, \ $ var = ". $ var ."
"; // Parameter reference transfer and value transfer: address operator & function increment (& $ value, $ amount = 1) {$ value = $ value + $ amount ;} $ a = 10; echo $.'
'; Increment ($ a); echo $ .'
'; // Return keyword: terminate the function execution function larger ($ x, $ y) {if ((! Isset ($ x) | (! Isset ($ y) {echo "This function requires two numbers.
"; Return;} if ($ x >=$ y) {echo $ x ."
";} Else {echo $ y ."
";}}$ A = 1; $ B = 2.5; $ c = 1.9; larger ($ a, $ B); larger ($ d, $ ); // return keyword: return a value from the function. function smaller ($ x, $ y) {if ((! Isset ($ x) | (! Isset ($ y) {return false;} else if ($ x <= $ y) {return $ x ;}else {return $ y ;}} echo smaller ($ a, $ B ).'
'; Echo smaller ($ d, $ ).'
'; // Implement recursive function reverse_r ($ str) {if (strlen ($ str)> 0) {reverse_r (substr ($ str, 1 ));} echo substr ($ str, 0, 1); return;} function reverse_ I ($ str) {for ($ I = 1; $ I <= strlen ($ str ); $ I ++) {echo substr ($ str,-$ I, 1) ;}return;} reverse_r ('Hello'); echo'
'; Reverse_ I ('Hello'); // phpinfo ();
For more details, refer to the php Manual-functions
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces php code reuse and functions, including some content, and hopes to help those who are interested in PHP tutorials.