Home > Backend Development > PHP Tutorial > Learn PHP(3) php functions step by step_PHP tutorial

Learn PHP(3) php functions step by step_PHP tutorial

WBOY
Release: 2016-07-21 15:40:46
Original
1058 people have browsed it

1. Method Overview

First, write the simplest function, which everyone can take a look at:

<span style="COLOR: blue"><</SPAN><SPAN style="COLOR: maroon">html</SPAN><SPAN style="COLOR: blue">>
<</SPAN><SPAN style="COLOR: maroon">head</SPAN><SPAN style="COLOR: blue">>
  <</SPAN><SPAN style="COLOR: maroon">title</SPAN><SPAN style="COLOR: blue">></span>HelloPHP<span style="COLOR: blue"></</SPAN><SPAN style="COLOR: maroon">title</SPAN><SPAN style="COLOR: blue">>
</</SPAN><SPAN style="COLOR: maroon">head</SPAN><SPAN style="COLOR: blue">>
<</SPAN><SPAN style="COLOR: maroon">body</SPAN><SPAN style="COLOR: blue">>
  <?</SPAN><SPAN style="COLOR: maroon">php
    </SPAN><SPAN style="COLOR: red">function CustomPrint</SPAN>($<SPAN style="COLOR: red">str</SPAN>)
    {
      <SPAN style="COLOR: red">for</SPAN>($<SPAN style="COLOR: red">i</SPAN><SPAN style="COLOR: blue">=0;$i</SPAN><5;$<SPAN style="COLOR: red">i</SPAN>++)
      {
        <SPAN style="COLOR: red">echo</SPAN>($<SPAN style="COLOR: red">str</SPAN>);
        <SPAN style="COLOR: red">echo</SPAN>(<SPAN style="COLOR: blue">'<br/>'</span>);
      }
    }
    
    <span style="COLOR: red">CustomPrint</span>(<span style="COLOR: blue">"Hello"</span>);
  <span style="COLOR: blue">?>
</</SPAN><SPAN style="COLOR: maroon">body</SPAN><SPAN style="COLOR: blue">>
</</SPAN><SPAN style="COLOR: maroon">html</SPAN><SPAN style="COLOR: blue">>
</span>
Copy after login

Through this example, I believe everyone understands the general writing method of functions in PHP. As for the syntax, it is not much different from other C-like languages. They are also while, for, if, etc. As for other differences , will be discussed gradually in subsequent articles.

OK, let me summarize the key points of this method:

A. PHP methods are declared with functions, which is similar to the familiar Javascript.

B. When using variables, they must start with a dollar sign ($).

2. Reference passing and value passing of parameters

Parameter value passing and reference passing, I believe everyone has come into contact with it when learning C language. Here is an example using C#:

<span style="COLOR: blue">public void </span>Swap(<span style="COLOR: blue">int </span>a, <span style="COLOR: blue">int </span>b)
{
  <span style="COLOR: blue">int </span>temp = a;
  a = b;
  b = temp;
}
<span style="COLOR: blue">public void </span>Swap(<span style="COLOR: blue">ref int </span>a, <span style="COLOR: blue">ref int </span>b)
{
  <span style="COLOR: blue">int </span>temp = a;
  a = b;
  b = temp;
}
Copy after login

Then write a PHP version here.

  <span style="COLOR: blue"><?</SPAN><SPAN style="COLOR: maroon">php
    </SPAN><SPAN style="COLOR: red">function Swap1</SPAN>($<SPAN style="COLOR: red">a</SPAN>,$<SPAN style="COLOR: red">b</SPAN>)
    {
      $<SPAN style="COLOR: red">temp</SPAN><SPAN style="COLOR: blue">=$a;
      </SPAN>$<SPAN style="COLOR: red">a</SPAN><SPAN style="COLOR: blue">=$b;
      </SPAN>$<SPAN style="COLOR: red">b</SPAN><SPAN style="COLOR: blue">=$temp;
    </SPAN>}
    <SPAN style="COLOR: red">function Swap2</SPAN>(&$<SPAN style="COLOR: red">a</SPAN>,&$<SPAN style="COLOR: red">b</SPAN>)
    {
      $<SPAN style="COLOR: red">temp</SPAN><SPAN style="COLOR: blue">=$a;
      </SPAN>$<SPAN style="COLOR: red">a</SPAN><SPAN style="COLOR: blue">=$b;
      </SPAN>$<SPAN style="COLOR: red">b</SPAN><SPAN style="COLOR: blue">=$temp;
    </SPAN>}
    
    <SPAN style="COLOR: red">function CustomPrint</SPAN>($<SPAN style="COLOR: red">str</SPAN>)
    {
      <SPAN style="COLOR: red">echo</SPAN>($<SPAN style="COLOR: red">str</SPAN>);
      <SPAN style="COLOR: red">echo</SPAN>(<SPAN style="COLOR: blue">"<br/>"</span>);  
    }
    
    $<span style="COLOR: red">a</span><span style="COLOR: blue">=1;
    </span>$<span style="COLOR: red">b</span><span style="COLOR: blue">=2;
    </span><span style="COLOR: red">Swap1</span>($<span style="COLOR: red">a</span>,$<span style="COLOR: red">b</span>);
    <span style="COLOR: red">CustomPrint</span>(<span style="COLOR: blue">"值传递的结果:"</span>);
    <span style="COLOR: red">CustomPrint</span>(<span style="COLOR: blue">'$a='</span>.$<span style="COLOR: red">a</span>);
    <span style="COLOR: red">CustomPrint</span>(<span style="COLOR: blue">'$b='</span>.$<span style="COLOR: red">b</span>);
    $<span style="COLOR: red">a</span><span style="COLOR: blue">=1;
    </span>$<span style="COLOR: red">b</span><span style="COLOR: blue">=2;
    </span><span style="COLOR: red">Swap2</span>($<span style="COLOR: red">a</span>,$<span style="COLOR: red">b</span>);
    <span style="COLOR: red">CustomPrint</span>(<span style="COLOR: blue">"引用传递的结果:"</span>);
    <span style="COLOR: red">CustomPrint</span>(<span style="COLOR: blue">'$a='</span>.$<span style="COLOR: red">a</span>);
    <span style="COLOR: red">CustomPrint</span>(<span style="COLOR: blue">'$b='</span>.$<span style="COLOR: red">b</span>);
  <span style="COLOR: blue">?>
</span>
Copy after login

In this example: There are two points I need to make clear:

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

B. CustomPrint('$a='.$a); In this sentence, special explanation is needed about the single quotes and The only difference between double quotes is whether the variable name can be parsed. This example is enough to illustrate the problem:

  <span style="COLOR: blue"><?</SPAN><SPAN style="COLOR: maroon">php 
    </SPAN>$<SPAN style="COLOR: red">a</SPAN><SPAN style="COLOR: blue">=1;
    </SPAN><SPAN style="COLOR: red">echo</SPAN>(<SPAN style="COLOR: blue">"$a"</SPAN>);
    <SPAN style="COLOR: red">echo</SPAN>(<SPAN style="COLOR: blue">"<br/>"</span>);
    <span style="COLOR: red">echo</span>(<span style="COLOR: blue">'$a'</span>);
  <span style="COLOR: blue">?>
</span>
Copy after login

image

Finally, let’s talk about performance issues. When passing by value, PHP needs to copy and then pass it. In this way, if those large objects or strings are used, it will not only consume time, but also consume space. It's also a waste. At this time, if you pass by reference, you will avoid the performance-consuming copy operation. Very good for performance improvement.

3. Scope issue

In C#, since variables must be declared before being used, the concepts of a scope and subscope are involved, but there is no such concept in PHP.

Let’s take a look at a piece of C# code:

<span style="COLOR: blue">public class </span><span style="COLOR: #2b91af">Student
</span>{
  <span style="COLOR: blue">private string </span>name;
  <span style="COLOR: blue">public void </span>SayHello()
  {
    <span style="COLOR: #2b91af">HttpContext</span>.Current.Response.Write(<span style="COLOR: #a31515">"Hello,I am " </span>+ name);
  }
}
Copy after login

In other words, variables declared by the external class can be accessed within the method, but it is different in PHP:

  <span style="COLOR: blue"><?</SPAN><SPAN style="COLOR: maroon">php 
    </SPAN>$<SPAN style="COLOR: red">name</SPAN><SPAN style="COLOR: blue">="kym"</SPAN>;
    <SPAN style="COLOR: red">function SayHello</SPAN>()
    {
      <SPAN style="COLOR: red">if</SPAN>(<SPAN style="COLOR: red">isset</SPAN>($<SPAN style="COLOR: red">name</SPAN>))
      {
        <SPAN style="COLOR: red">echo</SPAN>(<SPAN style="COLOR: blue">"Hello $name"</SPAN>);  
      }
      <SPAN style="COLOR: red">else
      </SPAN>{
        <SPAN style="COLOR: red">echo</SPAN>(<SPAN style="COLOR: blue">'$name is undefined'</SPAN>);
      }
    }
    <SPAN style="COLOR: red">SayHello</SPAN>();
  <SPAN style="COLOR: blue">?>
</span>
Copy after login

image

Here is a function "isset", which can detect whether a variable is defined or whether it is an empty string.

Then this result shows that the external variable $name cannot be accessed within the function body.

I would like to mention one more thing here: a function corresponding to unset: unset. This function is used to remove the value of a variable.

Write a simple example:

<span style="COLOR: red"><?php 
  </SPAN><SPAN style="COLOR: #660000">$name</SPAN>=<SPAN style="COLOR: #008200">"kym"</SPAN>;
  <SPAN style="COLOR: blue">if</SPAN>(<SPAN style="COLOR: blue">isset</SPAN>(<SPAN style="COLOR: #660000">$name</SPAN>))
  {
    <SPAN style="COLOR: blue">echo</SPAN>(<SPAN style="COLOR: #008200">"Yes"</SPAN>);
  }
  <SPAN style="COLOR: blue">else
  </SPAN>{
    <SPAN style="COLOR: blue">echo</SPAN>(<SPAN style="COLOR: #008200">"No"</SPAN>);
  }
  <SPAN style="COLOR: blue">unset</SPAN>(<SPAN style="COLOR: #660000">$name</SPAN>);
  <SPAN style="COLOR: blue">if</SPAN>(<SPAN style="COLOR: blue">isset</SPAN>(<SPAN style="COLOR: #660000">$name</SPAN>))
  {
    <SPAN style="COLOR: blue">echo</SPAN>(<SPAN style="COLOR: #008200">"Yes"</SPAN>);
  }
  <SPAN style="COLOR: blue">else
  </SPAN>{
    <SPAN style="COLOR: blue">echo</SPAN>(<SPAN style="COLOR: #008200">"No"</SPAN>);
  }
<SPAN style="COLOR: red">?></span>
Copy after login

image

These will be mentioned in detail in the subsequent garbage collection.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321309.htmlTechArticle1. Overview of the method First, write the simplest function, which everyone can take a look at: html head title HelloPHP / title / head body ? php function CustomPrint ($ str ) { for ($ i =0...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template