PHP functions and passing parameters_PHP tutorial

WBOY
Release: 2016-07-20 11:01:21
Original
809 people have browsed it

This tutorial will introduce the syntax of function calling and function definition, and then talk about variables in functions and methods of passing numerical values ​​to functions. ​

This tutorial will introduce the syntax of function calling and function definition, and then talk about variables in functions and methods of passing numerical values ​​to functions.

1. Basics of functions

php tutorial provides a large number of functions and allows users to customize functions,

php function definition example

function myCount($inValue1,$inValue2)
{
$AddValue = $inValue1+$inValue2;
Return $AddValue; //Return the calculation result
}
$Count = myCount(59,100);
echo $Count; //Output 159
?>

Once a function is defined, it can be used anywhere.

2. Function parameters

php function The parameters are declared and defined when the function is defined. The function can have any number of parameters. The most commonly used transfer method is to pass by value. Or applied relatively rarely via references and default parameter values.

Example

function myColor ($inColor = "Blue")
{
Return "The color I like: $inColor.n";
}
echo myColor();
echo myColor("pink");
?>

Generally the value passed will not change due to internal changes in the function. Unless it is a global variable or reference, let’s look at php function reference instance

function str_unite (&$string)
{
$string .= 'I also like blue.';
}
$str = 'Like red,';
str_unite ($str);
echo $str; // Output result: 'I like red and I also like blue.'
?>

Global variables

$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445450.htmlTechArticleThis tutorial will introduce the syntax of function calling and function definition, and then talk about variables in functions and functions Pass numeric methods. This tutorial will introduce about function calling and function definition...
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