Php basic form of function (1)-php Tutorial

Source: Internet
Author: User
Php defines the basic form functions of function (1:
A function is a named independent code segment. it executes a specific task and returns a value to the program that calls it.

Advantages of functions:
1. improving program reusability 2. improving program maintainability 3. improving software development efficiency 4. Improving Software reliability 5. controlling program complexity

Notes for using functions:

1. a function must be called before execution. it can be called before declaration or after function declaration.
2. function names must be meaningful just like variables
3. the function name cannot be repeated during declaration.

1. function name (){
Function body
}
2. function name (parameter 1, parameter 2, parameter...) // list of parameters. if multiple parameters exist, separate them.
{
Function body
}
3. function name (){
Function body
Return value;
}
4. function body (parameter list ){
Function body
Return value
}

Type 1: no parameter function,Example:

Function a () {// function 1 without parameters, output a statement
Echo "this is the first function ";
}
Function table () {// no parameter function 2, which outputs a 10x10 table
Echo'














';Echo' ';For ($ I = 0; $ I <10; $ I ++ ){If ($ I % 2 = 0)$ Bg = "# ffffff ";Else$ Bg = "# cccccc ";Echo' ';For ($ j = 0; $ j <10; $ j ++ ){Echo' ';}Echo' ';} // This part is a for loop statement that controls the form of table output.Echo'
Table name
'. ($ I * 10 + $ j ).'
';
}
A (); // call 1
Echo"
";
Table (); // call 2

?> // No-argument function is a process to implement a function

Type 2: argument function. example:

Function activity ($ name, $ time, $ event) {// function 1 with parameters, output a statement
Echo "$ name". 'In '. "$ time". 'punctual'. "$ event ";
}

Function sum ($ x, $ y) {// output a value. after return is used to obtain the returned value, add echo
$ Sum = $ x * $ x + $ y * $ y;
Return $ sum;
}

Activity ("James", "", "dinner ");
Echo"
";
Echo sum (4, 5 );

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.