Step by step learning PHP (4) php function supplement 2_PHP tutorial-php Tutorial

Source: Internet
Author: User
Step by step learning PHP (4) php function Supplement 2. 1. Solve the scope problem. I talked about the function scope in PHP in the previous section. if the class is not enough to explain the problem, the scope example in this article may be more convincing to you. 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 write such code by imitating this syntax:

  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 void LinkButton1_Click(object sender, EventArgs e){  Response.Write(GetSum(1, 2, 3, 4, 5));}public int GetSum(params int[] elements){  int sum = 0;  for (int i = 0; i < elements.Length; i++)  {    sum += elements[i];  }  return sum;}

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>  function Execute(functionName)  {    eval(functionName+"()");  }  function Test()  {    alert("111");  }  Execute("Test");

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

  function Test($func)  {    $func();  }  function First(){echo("first");}  Test("First");?>

Http:// solve the problem of scope in the last section talked about the scope of the function in PHP, if the class is not enough to explain the problem, then the scope example in this article, maybe more convincing to you...

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