Introduction to the custom functions of PHP functions _php Tutorial

Source: Internet
Author: User
In PHP, more than 700 built-in functions are available. PHP functions are divided into user-defined functions and system-built functions. Built-in functions can be used directly, and user-defined functions need to be defined using the keyword function.

Custom functions
function, which can be seen as a separate set of program statements to implement a function. Once we've written a function into a function, we can use it conveniently wherever we need it. Reasonable use of functions, can make our PHP program more concise and easy to read, more scientific.
Grammar

The code is as follows Copy Code

function function_name (arg1,arg2,......)
{
function function code
}


Creating PHP Functions
A function is a block of code that can be executed at any time when it is needed.

To create a PHP function:
All functions use the keyword "function ()" to start
Named function-the name of the function should indicate its function. The function name begins with a letter or underscore.
Add "{"-the section after the opening curly brace is the code of the function.
inserting function code
Add a "}"-the function ends by closing the curly braces.
Parameters of the function
The function of a parameter is to pass information to a function.
Example


Now, we're going to use this function in our PHP script:

The code is as follows Copy Code


function Writemyname ()
{
echo "David Yang";
}

echo "Hello world!
";
echo "My name is";
Writemyname ();
echo ".
That's right, ";
Writemyname ();
echo "is my name.";
?>


The output of the above code:

Hello world!
My name is David Yang.
That's right, David Yang is my name.


Example:

The code is as follows Copy Code

function City_name ($city)
{
echo "City name is:". $city;
}
City_name ("Shanghai"); Executes the function, and the result is output "city Name: Shanghai" string
?>
You can specify a default value for the parameters of the function so that the parameter defaults are taken when no parameter values are specified.

function City_name ($city = "Beijing")
{
echo "City name is:". $city;
}
$name = "Shanghai";
City_name (); Execution result is output "city name: Beijing"
City_name ($name); Execution result is output "city name: Shanghai"
?>

http://www.bkjia.com/PHPjc/629078.html www.bkjia.com true http://www.bkjia.com/PHPjc/629078.html techarticle in PHP, more than 700 built-in functions are available. PHP functions are divided into user-defined functions and system-built functions. Built-in functions can be used directly, user-defined functions need to be used ...

  • 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.