Step by step learn PHP of 4 PHP function supplement 2

  • 2020-03-31 20:20:09
  • OfStack

1. Address scope issues

In the last section of the PHP function in the scope of the problem, if the class is not enough, then the scope in the (link: http://www.cnblogs.com/kym/archive/2010/01/06/1640043.html), for example, might be more convincing to you.

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

In PHP, there is a concept called global scope, which means that if you don't use functions (and there will be the concept of classes in the future), the variables you create can be accessed anywhere on the page. So, how do we solve the problem inside the function?

We can use the global keyword:

< ? PHP $name = "kym"; The function Test () { Global $name; Echo ($name); } The Test (); ? > (link: http://11011.net/software/vspaste)

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

There is a sentence in the PHP manual that says that using a global variable is equivalent to creating a reference to a variable in the $GLOBALS variable. Then we can write code that mimics the semantics:

< ? PHP $name = "kym"; The function Test () { $temp = & $GLOBALS (" name "), Echo ($temp); } The Test (); ? >

(link: http://11011.net/software/vspaste)

These two are actually equivalent code. It seems that using variables in this way is troublesome, but it actually avoids many side effects, such as Java,C# is an object-oriented language, but in PHP, everything is in one page, or even multiple pages, so if you do not use this method, it is easy to produce function side effects (mistakenly modified).

2. Default parameters

I think I first heard of this concept in C++, and then I learned C#, so I never touched on this concept, but I still like this feature very much.

This eliminates the need to write a bunch of cumbersome overloaded functions for a function.

Write a simple example:

< ? PHP The function Test ($name = "kym") { Echo ($name); } The Test (); Test (" others "); ? >

 

Is it not necessary to write an overloaded function?

But here are two things to note:

A. When setting default values for parameters, you can only set the simplest constants, not complex expressions.

B. Parameters that need to be set to default values must be placed last. This is similar to the mutable argument in C#.

3. Variable parameters

In C#, there is such a concept called variable parameter, 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++) { The sum + = elements [I]; } Return the sum. }

Before I write an example, let me introduce three functions with variable arguments.

Func_get_args () returns an array of all the arguments to the function

Func_get_arg () returns a specific parameter from the parameter

Func_num_args () returns the number of parameters.

Ok, so I'm going to write a version of PHP that corresponds to the C# version.

< ? PHP The 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)); ? >

(link: http://11011.net/software/vspaste)

4. Variable functions

I don't know why it's translated into this name, but it's just calling the function based on the name of the variable.

Very similar to this eval in JS, as shown below:

< Script> The function the Execute (functionName) { The eval (functionName + "()"); } The function Test () { Alert (" 111 "); } Execute (" Test "); < / script>

(link: http://11011.net/software/vspaste)

This is just passing in a function name and executing the corresponding function. So in PHP, it's the same thing, but much simpler.

< ? PHP The function Test ($func) { $func (); } The function First () {echo (" First "); } Test (" First "); ? >


Related articles: