A detailed description of user-defined function usage (creation, call, variable, parameter, return value, etc.) in the PHP getting started tutorial.
This example describes the usage of PHP user-defined functions. We will share this with you for your reference. The details are as follows:
Demo1.php
<? Php // standard function, built-in function echo md5 ('000000'); echo '<br/>'; echo sha1 ('000000'); echo 'Who asked Jun Yi, the water is refreshing and refreshing. ';?>
Demo2.php
<? Php // create a function. do not duplicate the name with the built-in function of the system. // The function must be called before it can be executed. // No parameter indicates that () is empty, if no response is returned, the function program does not have return function functionName () {echo '. I am a function without parameters and does not return. ';} FunctionName () ;?>
Demo3.php
<? Php // a function that contains parameters that do not return values // in general, if you write a function, you do not need to modify it. // The change is generally the input parameter function functionArea ($ radius) {$ area = $ radius * pi (); echo 'radius is '. $ radius. 'area :'. $ area;} // call functionArea (10);?>
Demo4.php
<? Php // contains parameters. function functionArea ($ radius) {$ area = $ radius * pi (); return $ area;} is returned ;} // call // This greatly improves the flexibility of the function // functionArea (20); the overall value is obtained in the memory. Echo 'area with a radius of 20 is: '. functionArea (20);?>
Demo5.php
<? Php // contains parameters with returned values // This $ radius = 10, here 10 represents the default value of this parameter // If the called function does not pass a parameter to the function, enable the default function functionArea ($ radius = 10) {$ area = $ radius * pi (); return $ area ;} // call // This greatly improves the flexibility of the function // functionArea (20); the overall value is obtained in the memory. Echo 'area with a radius of 20 is: '. functionArea ();?>
Demo6.php
<? Php // write a function. Here, the function returns three data functions functionInfo ($ name, $ age, $ job) {// $ userInfo is an array // $ userInfo = array ($ name, $ age, $ job); $ userInfo [] = $ name; $ userInfo [] = $ age; $ userInfo [] = $ job; return $ userInfo;} // call the print_r function (functionInfo ('all-in-One website', 19, 'programmer '); // $ arr = functionInfo ('all-in-One website', 19, 'programmer'); // echo $ arr [0]; // list ($ name, $ age, $ job) = functionInfo ('all-in-One website', 19, 'programmer '); // echo $ name. 'Year '. $ age. 'years old, he is still '. $ j Ob;?>
Demo7.php
<? Php // previous parameters are passed by value $ prices = 50; $ tax = 0.5; // This function has no value, currently, passing parameters by value // the variables in the function and the variables outside the function do not have any relationship with function functionPrices (& $ prices, & $ tax) {// The variable $ prices is already 75 $ prices = $ prices + $ prices * $ tax; $ tax = $ tax * $ tax; echo $ prices; // 75 echo '<br/>'; echo $ tax; // 0.25 echo '<br/>';} functionPrices ($ prices, $ tax ); // reference this concept, which we cannot grasp at present. We will focus on echo $ prices in OOP; // pass by value 50, 75 echo '<br/>' by reference; echo $ tax;?>
Demo8.php
<? Php // understand global variables // you can set $ a to a global variable $ a = 5; function fa () {global $; // set $ a as the global variable $ a = 2;} fa (); echo $ a; // 2?>
Demo9.php
<? Php // use the super global variable $ GLOBALS ['a'] = 5; function fa () {$ GLOBALS ['a'] = 2;} fa (); // echo $ GLOBALS ['a']; print_r ($ GLOBALS ['a']);?>
Demo10.php
<? Php // to include the function in 'library/tool. library. php'; echo functionPi ();?>
Demo11.php
<? The php // include () statement contains and runs the specified file. // Include 'demo1. php'; // include 'demo1. php'; // The include_once () statement contains and runs the specified file during script execution. // This behavior is similar to the include () statement. The only difference is that if the code in the file has been included, it will not be included again. // Include_once 'demo1. php '; // include_once' Demo1. php '; // only contains the Referenced File once. // If the referenced file does not exist, two warnings will be given to you. Then, if you continue to execute the command // If require is not stored, an error will be reported directly, then stop running require 'demo1. php '; // we recommend that you use require // require 'demo1. php '; // require_once' Demo1. php '; // require_once' Demo1. php'; echo '<strong> who asked about the sounds of the water. </Strong> '?>
Demo12.php
<? Php // _ FILE _ // magic constant -- the constant here is a value. // $ file = _ FILE _; // C: \ AppServ \ www \ Basic7 \ Demo12.php // echo $ file; // Therefore, when a file is contained, we recommend that you use _ FILE _ to speed up echo dirname (_ FILE _); // C: \ AppServ \ www \ Basic7 require (dirname (_ FILE __). '\ demo1.php'); // C: \ AppServ \ www \ Basic7 echo _ LINE __; function ffff () {return _ FUNCTION __;} echo '