PHP functions use methods and functions define methods

  • 2020-03-31 20:40:07
  • OfStack

The syntax for a function is:
Function definition method
 
function "function_name" (arg1, arg2...) 
{ 
[code to execute] 
return [final_result]; 
} 

Where [final_result] usually returns a variable value from a function.
Let's look at an example
 
function double_this_number($input_number) 
{ 
return $input_number*2; 
} 

A method is called
 
$x = 10; 
$y = double_this_number($x); 
print $y; 

The output value of
10
Ok, so let's look at a slightly more complicated way of using a function
 
function safePost($v=0) 
{ 
if( $v==0 ) 
{ 
$protected = array("_GET", "_POST", "_SERVER", "_COOKIE", "_FILES", "_ENV", "GLOBALS"); 
foreach($protected as $var) { 
if(isset($_REQUEST[$var]) || isset($_FILES[$var])) 
{ 
die("Access denied"); 
} 
} 
} 
} 

A method is called
SafePost ();
This can be left undefined because $v==0 is set as an argument by default, which is good for extending the function.

Related articles: