Analysis of php functions and passing parameter instances

Source: Internet
Author: User
This article mainly introduces the php function and the method for passing parameters, and analyzes the specific usage of function passing by value in the form of examples, which has good reference value, for more information about function calling and function definition syntax, see the examples in this article. share it with you for your reference. The details are as follows:

I. Basics of functions

Php provides a large number of functions and allows you to customize them. The php function definition instance code is as follows:

The code is as follows:

<? Php
Function myCount ($ inValue1, $ inValue2)
{
$ AddValue = $ inValue1 + $ inValue2;
Return $ AddValue; // return the calculation result
}
$ Count = myCount (59,100 );
Echo $ Count; // output 159
?>


Functions can be used anywhere once defined.

II. function parameters

Php function parameters are declarative during function definition. a function can have any number of parameters. The most common transfer method is passed by value, or the default parameter values are applied less by reference. the instance code is as follows:

The code is as follows:

<? Php
Function myColor ($ inColor = "blue ")
{
Return "my favorite color: $ inColor .";
}
Echo myColor ();
Echo myColor ("pink ");
?>


Generally, the passed value will not change because of internal changes of the function. unless it is a global variable or reference, let's look at the php function reference instance. the code is as follows:

The code is as follows:

<? Php
Function str_unite (& $ string)
{
$ String. = 'also like blue .';
}
$ Str = 'like red ,';
Str_unite ($ str );
Echo $ str; // output result: 'Like red or blue .'
?>


The code for global variables is as follows:

The code is as follows:

<? Php
$ A = 1;
$ B = 2;
Function Sum ()
{
Global $ a, $ B;
$ B = $ a + $ B;
}
Sum ();
Echo $ B;
?>

I hope this article will help you with PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.