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

Jul 21, 2016 pm 03:40 PM
h html php step Write function study method Overview of look first

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...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles