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

php (7) PHP function

黄舟
Release: 2023-03-04 11:18:01
Original
1060 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

The output of the above code:

4+26=30
Copy after login


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


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template