search
Homephp教程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

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),