Php function and passing parameter instance analysis, php function instance Analysis _ PHP Tutorial

Source: Internet
Author: User
Analysis of php functions and passing parameter instances, and analysis of php function instances. Php function and transfer parameter instance analysis, php function instance analysis this article describes the function call and function definition syntax, this article also explains how to analyze the variables in the function, transfer numeric values to the function, and transfer parameter instances.

This article describes the function call and function definition syntax, explains the variables in the function, and the numeric method for passing values to the function. 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.

Examples in this article describes the function call and function definition syntax, and explains the variables in the function and the transfer of values to the function...

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.