Step by Step Learning PHP (4) PHP function Supplemental 2_php Foundation

Source: Internet
Author: User

1. Addressing scope issues

In the previous section, I talked about the scope of functions in PHP, and if the class is not enough to explain the problem, then the scope example in this article might be more convincing to you.

So how can we access external variables in PHP's functions?

In PHP, there is a concept called global scope, that is, if you do not use a function (future will have the concept of Class), then you create variables can be accessed anywhere on the page. So, how do we solve the problem in the body of the function?

We can use the Global keyword:

  ? php $nameglobal $echo ($     

In this case, we are given a reference to the global variable $name without having to recreate a $name variable.

The word in the PHP manual is that using global variables is equivalent to creating a reference to a variable in a $globals variable. So we can mimic this semantics and write code like this:

  ? php $namefunction Test () {$temp=& $GLOBALS ["name       

In fact, these two are equivalent code. It seems cumbersome to use variables like this, but this actually avoids a lot of side effects, such as java,c# is an object-oriented language, but in PHP, everything is in a page, or even multiple pages, then if you do not use such a method, it is easy to produce the side effects of the function (mistakenly modified).

2. Default parameters

The first time I heard this concept should be in C + +, after learning C #, there is no contact with this concept, but the individual still very much like this feature.

So you don't have to write a bunch of cumbersome overloaded functions for a function.

Write one of the simplest examples:

  ?  function Test ($nameEcho ($Test (     

Is it not necessary to write an overloaded function?

But here are two points to note:

A. When you set a default value for a parameter, you can set only the simplest constants, not the complex expressions.

B. Parameters that need to be set to the default value must be last. This is similar to a variable parameter in C #.

3. Variable parameters

In C #, there is a concept called variable parameters, and write a simple example:

protected void LinkButton1_Click (publicint getsum, for(returnsum;}  

Before I write an example, let me introduce three functions related to variable parameters.

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

Func_get_arg () returns a specific parameter from the parameter

Func_num_args () returns the number of parameters.

Well, then write a PHP version corresponding to the C # version.

  ?   if (Func_num_args () return0;} $sum for($i=0; $i <Func_num_args (); $ i++) {$sum+ return$echo (getsum (1,2,3,4));?          

4. Variable function

I do not know why to translate this name, in fact, according to the name of the variable to invoke the corresponding function.

This is similar to the eval in JS, as follows:

<scriptfunction Execute (functionname) {eval (functionname+function Test () {alert ("111");} Execute (</script>      

This is to pass in a function name and then to perform the corresponding function. So in PHP, it's the same, and it's simpler.

<?php 
  function Test (function A-I() {echo ("a");} Test (?>    

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.