Step by Step Learn PHP (3) PHP function _php Foundation

Source: Internet
Author: User
Tags garbage collection learn php

1. Methods Overview

First of all, write a simplest function, you can take a look at it:

<HTML> <head> <title>hellophp</title> </  Head> <body>  function Customprint ($ for($i=0; $i <5;$Echo ($echo (customprint?> </ Body > </html              

Through this example, I believe we all understand the PHP function of the general writing, as for the grammar, and other classes C language difference is not small, but also are while,for,if, as for other differences, will be in the post, gradually.

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

A. The PHP method is declared with function, which is similar to our familiar JavaScript.

B. The use of variables must begin with a dollar character ($).

2. Reference passing and value passing of parameters

Parameter value passing and reference passing, I believe that everyone in the learning of C language are exposed to, here in C # to write examples:

public void Swap (int temp = A; a = b; b = temp;} 

Then write a PHP version here.

  ?Phpfunction Swap1($A,$B) { $Temp= $a;$ a  = $b; $  B  = $temp;}  function Swap2 (&$  a,&$  b) {$  temp  = $a; $  a  = $b; $  B  = $temp;}  function Customprint ($  str) { echo ($  str);  Echo ( <br/>); $  a  =1; $  B  = 2;  Swap1 ($  a,$  b);  Customprint ( The result of the value passing: ");  customprint ( ' $a = '. $  a);  customprint ( ' $b = '. $  b); $  a  =1; $  B  = 2;  Swap2 ($  a,$  b);  Customprint ( "The result of a reference pass:");  customprint ( ' $a = '. $  a);  customprint ( ' $b = '. $  B); ?>                  

In this example: There are two points I need to explain:

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

B. customprint (' $a = '. $a); In this sentence, you need to specify the difference between single and double quotes, the only difference between them is whether the variable name can be resolved, and this example is sufficient to illustrate the problem:

  ? PHP $aecho       (The Echo (

Finally, the next question about performance, when passed by value, PHP needs to replicate, and then pass, so if those large objects or strings, it will not only time-consuming, but also a waste of space. At this point, if a reference is passed, it eliminates the performance-intensive copy operation. is good for performance improvement.

3. Scope issues

In C #, because a variable must be declared before it is used, it involves the concept of a scope and a child scope, and in PHP there is no such concept.

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

HttpContext.Current.Response.Write ("hello,i am" + name);}

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

  ? php $nameif (isset ($echo        (

This shows a function "Isset", which detects whether a variable is defined, or whether it is an empty string.

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

Here's a little bit more: a function corresponding to unset: unset. This function is used to remove the value of a variable.

Write a simple example:

<?php 
  $name =if (isset (unset (theif (issetEcho) (Echo (?>          

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

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.