search
Homephp教程php手册php函数与传递参数实例分析,php函数实例分析

php函数与传递参数实例分析,php函数实例分析

本文实例讲述了函数的调用与函数定义语法,并讲解了关于函数中的变量以及向函数传递数值方法.分享给大家供大家参考。具体如下:

一、函数的基础

php提供了大量的函数,并且允许用户自定函数,php函数定义实例代码如下:

复制代码 代码如下:

function myCount($inValue1,$inValue2)
{
  $AddValue = $inValue1+$inValue2;
  return $AddValue;     //返回计算结果
}
$Count = myCount(59,100);
echo $Count;     //输出159
?>


函数一但被定义就可以在任何地方使用.

二、函数传参数

php 函数参数在函数定义时进行申明定义,函数可以有任意多个参数,应用最普遍的传递方法,是按值传递的,或通过引用和默认参数值应用得相对比较少.实例代码如下:

复制代码 代码如下:

function myColor ($inColor = "蓝色")
{
    return "我喜欢的颜色: $inColor. ";
}
echo myColor();
echo myColor("粉色");
?>


一般传递的值不会因函数内部改变而改变的,除非是全局变量或是引用我们来看php函数引用实例,代码如下:

复制代码 代码如下:

function str_unite (&$string)
{
    $string .= '也喜欢蓝色.';
}
$str = '喜欢红色,';
str_unite ($str);
echo $str;    // 输出结果: '喜欢红色,也喜欢蓝色.'
?>


全局变量,代码如下:

复制代码 代码如下:

$a = 1;
$b = 2;
function Sum()
{
    global $a, $b;
    $b = $a + $b;
}
Sum();
echo $b;
?>

希望本文所述对大家的PHP程序设计有所帮助。

Statement
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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment