Step by step learn PHP of 3 PHP functions

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

1. Method overview

First, write a simple function that you can look at:

< Html> < Head> < Title> HelloPHP< / title> < / head> < Body> < ? PHP The function CustomPrint ($STR) { For ($I = 0; $i< 5; $i++) { Echo ($STR); Echo (' < Br / > '); } } CustomPrint (" Hello "); ? > < / body> < / html>

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

With this example, I'm sure you've all learned a little bit about how functions are written in PHP, but as far as syntax is concerned, it's not that different from other c-like languages. It's all while,for,if, and so on.

OK, so let me summarize the main points of this method:

A. PHP's method is declared with function, which is similar to the familiar Javascript.

B. Variables must begin with a dollar character ($).

2. Parameter reference passing and value passing

Parameter value transfer and reference transfer, I believe that everyone has been exposed to C language when learning, here use C# to write an example:

Public void Swap(int a, int b) { Int temp = a; A = b; B = temp. } Public void Swap(ref int a, ref int b) { Int temp = a; A = b; B = temp. }

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

So let's write a PHP version here.

< ? PHP The function Swap1 ($a, $b) { $temp = $a; $a = $b; $b = $temp; } The function Swap2 (& $a, & $b) { $temp = $a; $a = $b; $b = $temp; } The function CustomPrint ($STR) { Echo ($STR); Echo (" < Br / >" ); } $a = 1; $b = 2; Swap1 ($a, $b); CustomPrint(" result of value passed :"); CustomPrint (' $a = '$a); CustomPrint (' $b = '$b); $a = 1; $b = 2; Swap2 ($a, $b); CustomPrint(" result passed by reference :"); CustomPrint (' $a = '$a); CustomPrint (' $b = '$b); ? > (link: http://11011.net/software/vspaste)

In this example: there are two points I need to make:

A. The difference between value passing and reference passing is the "&" before the parameter.

B. CustomPrint (' $a = '$a); The only difference between single and double quotation marks is that the variable name can be resolved.

< ? PHP $a = 1; Echo (" $a "); Echo (" < Br / >" ); Echo (" $a "); ? > (link: http://11011.net/software/vspaste)

 

(link: https://www.jb51.net/upload/2010-2/20100215135132775.png)  

Finally, with respect to performance, PHP needs to copy and then pass when passing by value, so that those large objects or strings are not only time consuming, but also a waste of space. At this point, if you pass by reference, you save yourself the performance penalty of copying. Good for performance.

3. Scope issues

In C#, because variables must be declared before they can be used, there is a concept of scope and subscope, which is not available in PHP.

Let's take a look at some C# code:

Public class Student { Private string name; Public void SayHello () { HttpContext. Current. The Response. The Write (" Hello, I am "+ name); } }

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

That is, you can access variables declared by an external class in a method, but not in PHP:

< ? PHP $name = "kym"; The function SayHello () { If (isset ($name)) { Echo (" Hello $name "); } The else { Echo (" $name is undefined "); } } SayHello (); ? >

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

(link: https://www.jb51.net/upload/2010-2/20100215135132951.png)

Here is a function called "isset" that detects whether a variable is defined or whether it is an empty string.

This result indicates that the external variable $name cannot be accessed in the body of the function.

Here is a bit more: a function corresponding to unset: unset. This function removes the value of a variable.

Write a simple example:

< ? PHP $name = "kym"; If (isset ($name)) { Echo (" Yes "); } The else { Echo (" No "); } The unset ($name); If (isset ($name)) { Echo (" Yes "); } The else { Echo (" No "); } ? > (link: http://11011.net/software/vspaste)

 

(link: https://www.jb51.net/upload/2010-2/20100215135132413.png)

These will be mentioned in more detail in the garbage collection later.


Related articles: