Home php教程 php手册 php函数详解之学习笔记

php函数详解之学习笔记

Jun 13, 2016 am 10:16 AM
php function point Can Reasonable exist study method yes use of notes Detailed explanation repeat

在php中函数是常用的一种合理的可以重复使用的方法,函数分别系统函数与用户自定义函数,下面我来给大家介绍这两种php函数的使用方法,有需了解的朋友可参考。

系统函数

如phpinfo() ;这就是一个系统函数了,关于系统函数我们不多说,直接参考php教程或手册都会有。

用户自定义函数

php 引入函数的概念使程序变得简洁,运算效率更快。函数是实现某种功能的一段程序,这段程序相当于一个模块,需要时用一些简单的命令就可以调用出来使用,不用每次都重新编写。就好象电脑,我们不需要了解里面的复杂结构,只要了解它提供的功能直接使用即可。

php自定义函数-php虽然内置了很多实用的函数,可是不可能满足实际设计的需要,这时我们就需要自己创建函数。

基本结构形式:

 代码如下 复制代码

function 函数名(参数){
 语句体
}

function为声明函数;

函数名必须唯一,和变量名不同的是函数名不区分大小写;
function和函数名之间必须要有空格;
参数可以省略,也可以添加多个参数,用英文逗号隔开。
php调用函数-当我们定义了函数后,就可以通过以下结构形式对函数进行调用:

函数名(参数)

参数可以省略,但必须保留括号。
实例:

 代码如下 复制代码

 function e(){
  $i=5+6;
  echo $i;
 }
 e();
?>

函数的传递参数

如果函数定义了参数,那么在调用函数时就必须注意参数值的传递问题。php函数参数传递的方式有两种:按值传递和引用传递,默认为按值传递。

1、按值传递:

实例1:

 代码如下 复制代码

 function sum($num,$price){
  $total=$num*$price;
  echo "总价格为:$total";
 }
 sum(10,78);
?>

默认参数

php函数默认参数-如果用一个函数时并没有提供该参数的值,那么该参数将使用函数制定的默认值,默认值必须已经在函数定义时赋值。

实例:

 代码如下 复制代码

 function add($a,$b=10){  /* 定义变量$b默认参数 */
  $sum=$a+$b;
  echo $sum ."
";
 }
 add(1,5);   /* 按值传递参数代替默认参数 */
 add(1);    /* 没有给$b传递参数将采用默认值 */
?>

函数返回值

我们在设计程序时,很多时候不希望将函数的运行结果直接在浏览器显示出来,这时可以通过return语句先将函数值调用出来再进行其它操作。

php函数返回值实例:

 代码如下 复制代码

 function add($a,$b=10){  
  $sum=$a+$b;
  return $sum; /* 将$sum的值赋给函数 */
 }
 $sum=add(1,5);  /* 这里变量$sum得到函数的值 */   
 echo $sum ."
";
?>

函数嵌套调用

函数的内部不但可以是一般的执行语句,也可以是函数,这就是php函数嵌套的用法。

函数嵌套只一个函数里面还调用了其它函数,这里要理解的是各函数均处于平行关系,函数的定义是独立的。就像一个容器里面可以放置很多个小容器,而这些容器彼此独立,各自容纳自己的东西。

实例:

 代码如下 复制代码

 function volume($l,$w){
  function height($a){
   $a=$a/2;
   return $a;
  }
  $a=$l*$w;
  $v=$a*height($a);
  echo $v;
 }
 volume(8,3);
?>

函数递归调用

什么是php递归函数?在解释这个概念之前,我们先来看一个数学例子。如果要算出n的阶乘,即n!,则首先应该知道(n-1)!,而要知道(n-1)!,又必须知道(n-2)!。以此类推,直至1!=1,再返回计算2!,3!…(n-1)!,n!,才能最终得出n阶乘的结果。

php函数递归调用的含义就是函数能够调用自身,这种直接或者简介调用函数本身的操作在php语言中是允许的,成之为递归函数。

实例:

 代码如下 复制代码

 function calculate($n){
  if($n==0)
  return 1;
  else
  return $n*calculate($n-1);
 }
 $s=calculate(3);
 echo $s;
?>

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles