Home >Backend Development >PHP Tutorial >php (7) PHP function

php (7) PHP function

黄舟
黄舟Original
2016-12-27 10:12:311075browse

1. Custom function

A function is a block of code that can be executed whenever needed.

Function Declaration:

All functions begin with the keyword "function()"

Name the function - the name of the function should suggest its function. Function names start with a letter or underscore.

Add "{" - The part after the opening curly brace is the code of the function.

Insert function code

Add a "}" - the function ends by closing the curly brace.

Example:

[php]  
<html>  
<body>  
  
<?php  
function writeMyName()  
  {  
  echo "jeremyLin";  
  }  
  
writeMyName();  
?>  
  
</body>  
</html>

2. Function - Adding Parameters

Our first function is a very simple one function. It can only output a static string.

We add more functionality to functions by being able to add parameters. A parameter is like a variable.

Example:

[php]  
<html>  
<body>  
  
<?php  
function writeMyName($fname)  
  {  
  echo $fname . "jeremy.<br />";  
  }  
  
echo "My name is ";  
writeMyName("lin");  
  
echo "My name is ";  
writeMyName("kobe");  
  
echo "My name is ";  
writeMyName("John");  
?>  
  
</body>  
</html>

3. Function - return value

Functions can also be used to return values.

[php] 
<html>  
<body>  
  
<?php  
function add($x,$y)  
  {  
  $total = $x + $y;  
  return $total;  
  }  
  
echo "4 + 26 = " . add(4,26);  
?>  
  
</body>  
</html>

The output of the above code:

4+26=30


The above is the content of php (7) PHP function. For more related information, please Follow the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php (6) PHP constantsNext article:php (6) PHP constants