PHP function Basic syntax and usage examples of passing parameters

Source: Internet
Author: User
This article describes the function invocation and function definition syntax, and explains the functions of theVariable and passing numerical methods to the function. as follows:

First, the basis of the function

PHP provides a number of functions, and allows the user to customize the function, the PHP function definition instance code is as follows:

<?php function MyCount ($inValue 1, $inValue 2) {   $AddValue = $inValue 1+ $inValue 2;   return $AddValue;     Return the result of the calculation} $Count = MyCount (59,100); echo $Count;     Output 159?>

function One but is defined can be used anywhere.

Second, the function passes the parameter

PHP function parameters are defined in the definition of a function, the function can have any number of parameters, the most common method of application is passed by value, or by reference and default parameter values are relatively less. The instance code is as follows:

<?php function MyColor ($inColor = "Blue") {     return "My favorite color: $inColor.";} echo MyColor (); Echo MyColor ("Pink");?>

The values that are normally passed are not changed by the internal changes of the function, unless it is a global variable or a reference to the PHP function reference instance, the code is as follows:

<?php function Str_unite (& $string) {     $string. = ' also like blue. ';} $str = ' like red, '; Str_unite ($STR); Echo $str;    Output: ' Like red, also like blue. '?>

Global variables, the code is as follows:

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

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.