Step by step learning PHP (3) php function _ PHP Tutorial

Source: Internet
Author: User
Learn PHP (3) php functions step by step. 1. Method overview first, write a simple function. you can just take a look at it: htmlheadtitleHelloPHPtitleheadbody? Phpfunctionmpmprint ($ str) {for ($ i0 1. Method overview

First, you can write a simple function at a glance:

<html><head>  <title>HelloPHP
  title>
   head><body>  
    php    function CustomPrint($str)    {      for($i=0;$i<5;$i++)      {        echo($str);        echo('
'); } } CustomPrint("Hello"); ?> body> html>

Through this example, I believe everyone understands the general syntax of functions in PHP. As for the syntax, it is not similar to other C languages, and it is also a while, for, if, etc, for other differences, we will gradually talk about them in subsequent articles.

OK. Let me summarize the key points of this method:

A. PHP methods are declared using functions, which is similar to Javascript.

B. variables must begin with a dollar sign ($.

2. parameter reference transfer and value transfer

The value transfer and reference transfer of the parameter, I believe that everyone has come into contact with the C language. here we 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;}

Write a PHP version here.

  
  PhpFunction Swap1($A, $B) {$Temp= $;$A= $ B;$B= $ Temp;}Function Swap2(& $A, & $B) {$Temp= $;$A= $ B;$B= $ Temp;}Function CustomPrint($Str){Echo($Str);Echo("
");} $A= 1;$B= 2;Swap1($A, $B);CustomPrint("Result of value transfer :");CustomPrint('$ A ='. $A);CustomPrint('$ B ='. $B); $A= 1;$B= 2;Swap2($A, $B);CustomPrint("Reference passed results :");CustomPrint('$ A ='. $A);CustomPrint('$ B ='. $B);?>

In this example, I need to explain the following two points:

A. The difference between value transfer and reference transfer lies in the "&" before the parameter.

B.CustomPrint('$ A ='. $A); In this sentence, we need to explain the difference between single quotes and double quotes. there is only one difference between them, that is, whether the variable name can be parsed. This example is enough to explain the problem:

  
   php     $a=1;    echo("$a");    echo("
"); echo('$a'); ?>

Finally, let's talk about the performance. when passing by value, PHP needs to copy and then pass it. in this way, if the large objects or strings are used, it will not only take time, it is also a waste of space. In this case, the performance-consuming copy operation is not required if the reference is passed. It is good for performance improvement.

3. Scope problems

In C #, variables must be declared before they are used. Therefore, a scope and subscope are involved, but PHP does not.

Let's look at a piece of C # code:

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

That is to say, variables declared by external classes can be accessed in the method, but they are different in PHP:

  
  php     $name="kym";    function SayHello()    {      if(isset($name))      {        echo("Hello $name");        }      else      {        echo('$name is undefined');      }    }    SayHello();  ?>

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

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

Here we will mention one more point: a function corresponding to unset: unset. This function is used to remove the value of a variable.

Write a simple example:

   $name="kym";  if(isset($name))  {    echo("Yes");  }  else  {    echo("No");  }  unset($name);  if(isset($name))  {    echo("Yes");  }  else  {    echo("No");  }?>

These will be described in detail in subsequent garbage collection.

Http://www.bkjia.com/PHPjc/321309.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/321309.htmlTechArticle1. Method overview first, write a simple function, you can take a look at it: html head title HelloPHP/title/head body? Php function CustomPrint ($ str) {for ($ I = 0...

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.