Step by step learning PHP (4) -- Functions

Source: Internet
Author: User

I believe that everyone has at least a certain degree of C language, or C ++, Java, and C #, before learning PHP, let's just talk about the uniqueness of the PHP method.

1. Solve the scope Problem

In the previous section, I talked about the Function scope in PHP. If the class is not enough, the scope example in this article may be more convincing to you.

So, how can we access external variables in PHP functions?

In PHP, there is a concept called global scope. That is to say, if you do not use a function (there will be a class concept in the future), the variables you create can be accessed anywhere on the page. So how can we solve the problems in the function body?

We can use the global keyword:

 
<?PHP
$Name= "Kym";
Function Test()
{
Global$Name;
Echo($Name);
}
Test();
?>

In this way, we get a reference to the global variable $ name without creating a new $ name variable.

In the PHP manual, using a global variable is equivalent to creating a reference to the variable in the $ globals variable. Then we can imitate this semantic to write thisCode:

 
<?PHP
$Name= "Kym";
Function Test()
{
$Temp= & $ Globals ["Name"];
Echo ($ temp );
}
Test ();
?>

In fact, these two are equivalent codes. It seems very troublesome to use variables like this, but this actually avoids many side effects, such as Java and C # Are object-oriented languages, but in PHP, everything is on a page, if this method is not used on multiple pages, the side effects of the function are easily generated (by mistake ).

2. Default Parameters

I have heard of this concept for the first time in C ++. After I learned C #, I have never touched this concept. However, I still like this feature very much.

In this way, you do not need to write a bunch of troublesome overload functions for a function.

Write a simple example:

 
<?PHP
Function Test($Name= "Kym")
{
Echo($Name);
}
Test();
Test("Others");
?>

 

Isn't it necessary to write an overload function?

However, pay attention to the following two points:

A. When you set the default value for a parameter, you can only set the simplest constant, but cannot contain complex expressions.

B. The default value must be placed at the end. This is similar to variable parameters in C.

3. variable parameters

In C #, there is such a concept called variable parameters. Write a simple example:

Protected voidLinkbutton#click (ObjectSender,EventargsE)
{
Response. Write (getsum (1, 2, 3, 4, 5 ));
}
Public intGetsum (Params int[] Elements)
{
IntSum = 0;
For(IntI = 0; I <elements. length; I ++)
{
Sum + = elements [I];
}
ReturnSUM;
}

Before writing an example, I will introduce three functions related to variable parameters.

Func_get_args () returns the array of all parameters of the function.

Func_get_arg () returns a specific parameter from the Parameter

Func_num_args () returns the number of parameters.

Okay, then write a PHP version corresponding to C.

     <? PHP
Function getsum ()
{
If ( Func_num_args () = = 0)
{
Return 0;
}
$ Sum = 0;
For ($ I = 0; $ I < Func_num_args (); $I ++)
{
$ Sum + = Func_get_arg ($ I );
}
Return $ Sum ;
}
Echo ( Getsum (1, 2, 3, 4 ));
? >

4. Variable Functions

I don't know why to translate it into this name. In fact, it is to call the corresponding function based on the variable name.

It is similar to the eval in JS, as shown below:

<Script>
FunctionExecute (functionname)
{
Eval (functionname +"()");
}
FunctionTest ()
{
Alert ("111");
}
Execute ("Test");
</Script>

This is to pass in a function name and then execute the corresponding function. In PHP, it is actually the same and easier.

<? PHP
FunctionTest ($ Func)
{
$ Func();
}
FunctionFirst (){Echo("First");}
Test ("First");
?>

 

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.