Introduction to PHP Functions Tutorial

Source: Internet
Author: User
Tags function examples learn php php script

PHP function Introduction Tutorial is every one to learn PHP must master the basis to know oh, okay, let's talk about PHP function examples plus tutorials.

The real power of PHP comes from its functions.

In PHP-there are more than 7 00 built-in features.


-------------------------------------------------- ------------------------------

PHP functions
In this tutorial, we will show you how to build your own functions.

References and examples of built-in functions, please visit our PHP reference.


Create a PHP function
A function is a block of code that can be executed when we need it.

To create a PHP function:

All features start with "function ()"
The function of the name-it should be able to understand the feature not by its name. The name can begin with letters or underscores (not a number)
Add a "("-Functional code open after curly support
The ability to insert code
Add a ")"-this function is mainly completed by the closing of the curved brace
For example
A simple feature that writes my name when it is called:

<?php
function Writemyname ()
  {
  echo "Kai Jim refsnes";
  }
Writemyname ();
? >
  
  

Using PHP functions
Now, we will use the PHP script for the function:

<?php
function Writemyname ()
  {
  echo "Kai Jim refsnes";
  }
echo "Hello world!<br/>";
echo "My name is";
Writemyname ();
echo ". <br/>that ' s right,";
Writemyname ();
echo "is my name.";
? >
Output is.
Hello world!
My name is Kai Jim refsnes.
That's right, Kai Jim refsnes are my name
    
    

PHP functions-Add parameters
Our first function (Writemyname ()) is a very simple feature. It just writes a static string.

To add more function functions, we can add parameters. parameter is like a variable.

You may have noticed the function name after parentheses, such as: Writemyname (). In parentheses specified by the parameter.

Example 1
The following example writes the first name differently, but the same last name:

 

 <?php function Writemyname ($fname) {echo $fname.
  "Refsnes.<br/>";
The 
 echo "My name is";

 Writemyname ("Kai Jim"); 
 echo "My name is";

 Writemyname ("Hege"); 
 echo "My name is";

 Writemyname ("stale"); 
 outputs the results. 
   
The
 
 my name is Kai Jim refsnes.
The My name is Hege refsnes.
The My name is stale refsnes. 
   
 See the function return value below. 
   
 instance. 
   
 
 <?php function Add ($x, $y) {$total = $x + $y;
  return $total; 
 echo "1 + 16 =".
Add (1,16); 
   
 output results. 
 
 1 + = 

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.