Getting Started with PHP functions _php tutorial

Source: Internet
Author: User
Tags getting started with php
The real power of PHP comes from its functions.

In PHP, more than 700 built-in functions are available.

PHP functions

In this tutorial, we will explain how to create your own functions.

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.

Example

A simple function that can output my name when it is called:

     
     

Using PHP functions

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

     "; 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 and David Yang is my name.

PHP functions-Adding parameters

Our first function is a very simple function. It can only output a static string.

With the ability to add parameters, we add more functionality to the function. The parameter is similar to a variable.

You may notice that the function name is followed by a parenthesis, such as Writemyname (). The parameters are specified in parentheses.

Example 1

The following example tells the output a different name, but the last name is the same:

     ";  } echo "My name is"; Writemyname ("David"), echo "My name is", Writemyname ("Mike"), echo "My name is"; Writemyname ("John"); >

The output of the above code:

My name is David yang.my name was Mike yang.my name is John Yang.

Example 2

The following function has two parameters:

     ";  } echo "My name is"; Writemyname ("David", "."); echo "My name is"; Writemyname ("Mike", "!"); echo "My name is"; Writemyname ("John", "..."); >

The output of the above code:

My name is David yang.my name is Mike yang! My name is John Yang ...

PHP Functions-return values

Functions can also be used to return values.

Example

     

The output of the above code:

1 + 16 = 17


http://www.bkjia.com/PHPjc/446729.html www.bkjia.com true http://www.bkjia.com/PHPjc/446729.html techarticle the real power of PHP comes from its functions. In PHP, more than 700 built-in functions are available. PHP functions in this tutorial, we will explain how to create your own functions. ...

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