PHP Functions php function

Source: Internet
Author: User
Tags echo command

PHP functions PHP functions are important in PHP and are often used, so let me focus on the use of function. Let's take a look at some examples of Jane.

function is a name that we give a code block that can be executed when we need it. This may not seem to be the same as an idea, but please believe me when you understand and use features, you will be able to save a lot of time and write code more readable!

Advertising tizag.com
For example, you may have a company motto that you must show at least once on each page. If you don't, then you get fired! Well, it's shrewd PHP programmer you, you think yourself, "it sounds like the case I might need to function." ”

Tip: Although the function is often considered to be an advanced topic for programmers to start learning, if you can slow and stick to it, features can be just slightly speedbump in your career planning. So don't give up if your function confuses you first!

Create your first PHP function
When you create a feature, you first need to give it a name, such as Mycompanymotto. This is the function name, and you will be able to call the function so that it can be easily entered and understood.

The actual syntax to create a function is quite self-explanatory, but you can judge this. First, you have to tell PHP that you want to build a feature. You do this by typing the keyword function followed by your function name and some other things (we'll talk about later).

Here is how you can call a function a mycompanymotto. Note: We still have to fill in the Code Mycompanymotto.

<?php
function Mycompanymotto () {
}
?>

Note: Your function name can begin with a letter or underscore "_", but not a number!

With the right formatting features, we can now fill in the code and we want our functionality to execute. Do you see the braces in the above example "()"? These parentheses determine the code of our function without. Opening curly braces "(" tells the PHP function of the code is the start and end of the curly brace ")" tells the PHP, our duty is done!

We want our features to print out the company's motto every time it is called, so it sounds like an echo command for a job!

function Mycompanymotto () {
echo "We deliver quantity, not quality!<br/>";
}
?>

How the function is used.

<?php
echo "Welcome to tizag.com <br/>";
echo "OK, for stopping by! <br/> ";
echo "And remember ... <br/>";
?>
Instance two.

<?php
function Mycompanymotto () {
echo "We deliver quantity, not quality!<br/>";
}
echo "Welcome to tizag.com <br/>";
Mycompanymotto ();
echo "OK, for stopping by! <br/> ";
echo "And remember ... <br/>";
Mycompanymotto ();
?>

PHP function with parameters

Another useful function is that you can send information and their functions can be used. Our first feature Mycompanymotto is also not beneficial to everyone because it and ever will do so is to print out a single, unchanging string.

However, if we want to use parameters, then we will be able to add some extra functionality! The arguments show the parentheses "()" and look like a normal PHP variable. Let's create a new feature that builds on a custom greeting from a person's name.

Our parameters will be the name of the person and our function will concatenate the greeting string on this name. Here's what the code looks like.

<?php
function MyGreeting($firstName){
echo "Hello there". $firstName . "!<br/>";
}
?>
Instance.

The

<?php
function mygreeting ($firstName) {
    echo "Hello there". $firstName. "!<br/>";
}
MyGreeting ("Jack");
MyGreeting ("Ahmed");
MyGreeting ("Julie");
MyGreeting ("Charles");
? "

PHP Function-return value
In addition to the functionality information, you can also have them return a value. However, a function can only return one thing, although this thing can be any integer, floating-point number, array, string, etc you choose!

How does it return a value? Then, when the function is used to perform and complete, it sorts the changes from a function name to become a value. Grab this value and you can set a variable equal function. Like:

$ myVar = somefunction ();
Let's prove it. Returns the value with a simple function that returns the sum of two integers.

<?php
function MySum ($numX, $numY) {
$total = $numX + $numY;
return $total;
}
$myNumber = 0;
echo "Before the function, MyNumber =". $myNumber. " <br/> ";
$myNumber = MySum (3, 4); Store The result of MySum in $myNumber
echo "After the function, MyNumber =". $myNumber. " <br/> ";
?>
Output results:

Before the function, MyNumber = 0
After the function, MyNumber = 7

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.