在我知道的程序语言中关于function这个命令都是用来定义函数的,下面我们来介绍一下关于在php中的function的用法。
用户自定义函数也称自定义函数,它们不是PHP提供的,是由程序员创建的。由于自己创建了这样的函数,所以就可以完全控制这些函数。因此可以让一个函数完全按照自己希望的方式运行。
1,申明函数
在PHP中,定义函数的方法同其他编程语言几乎一样。下面是PHP申明函数的语法结构:
实例代码如下:
- Function function_name($argument1,$argument2,$argument3,。。。。。。$argumentn)
- {
- //函数代码code
- Return 返回值;
- }
以上语法结构中,关键字的含义如下。
(1)function:用于申明用户自定义函数的关键字。
(2)function_name:要创建的函数名称。该名称将在以后被调用时使用。函数名应该唯一,因为PHP不支持重载。在命名函数的时候,需要遵循和变量命名
相同的原则。但是函数名不能以$开头,而变量可以。
(3)argument:要传递给函数的值。函数可以有多个参数,它们之间用逗号。但是参数项是可选的,可以在调用函数的时候不传递任何参数。
(4)code:是在函数被调用的时候执行的一段代码。如果有两条或者多条语句,则代码必须用大括号"{}"括起来。但是,如果只有一条代码,则不需要大括号。
(5)Return:将调用的代码需要的值返回。任何类型都可以返回,包括列表和对象。这导致函数立即结束它的运行,并且将控制权传递回它被调用的行。
2,无参数函数
代码func_1。php
实例代码如下:
- <?php
- require 'a。php';
- echo "我不会被执行!";
- ?>
3,有参数函数
PHP支持按值传递参数(默认),通过引用传递和默认参数值。可变长度参数列表仅在PHP4和后继版本中被支持。
(1)值传递参数。按值进行参数传递是PHP的默认传递方式。使用这个方法,必须在主程序调用时传递一个值(参数)。
代码sum。php
实例代码如下:
- <?php
- function sum ($a,$b)
- {
- Echo $a+$b;
- }
- sum(100,20); //开始调用该函数
- ?>
(2)引用参数。在按照值传递的时候,只有参数的副本传递给被调用的函数。但是在被调用函数内部对这些值的任何修改,都不会影响调用函数
中的原始值。引用传递其实也就是地址传递,将一个变量的地址作为参数传递。
代码valuechange。php
实例代码如下:
- <?php
- $myNum=100;
- function Valuechange ($number)
- {
- $number=$number+1;
- Echo $number 。 "<br>";
- }
- Valuechange($myNum);
- Echo $myNum;
- ?>
(3)默认值参数。使用默认的参数值传递方法,函数必须在调用的时候有一个参数。如果没有使用的值,就把默认值传递给函数参数。默认值必须是常量表达式,不是变量,
类成员或者函数调用。
提示:当使用默认参数时,任何默认参数必须放在非默认参数的右侧;否则,可能函数不会按照预期的情况工作。
代码func_default。php
实例代码如下:
- <?php
- $myNum=100;
- function Valuechange ($number)
- {
- $number=$number+1;
- Echo $number 。 "<br>";
- }
- Valuechange($myNum);
- Echo $myNum;
- ?>
波比源码 » php中function 的用法
buy levaquin 250mg generic levaquin online order
purchase avodart for sale cost flomax 0.2mg order ondansetron 8mg online
cost aldactone buy valtrex 1000mg online cheap diflucan uk
ampicillin 250mg generic order cephalexin 125mg pills erythromycin brand
fildena 50mg over the counter order generic fildena order robaxin generic
suhagra pill order estrace 1mg pills buy estrace 2mg
lamotrigine 200mg over the counter buy lamotrigine 50mg sale buy tretinoin cream
tadalis 20mg pills order voltaren 50mg pill cost diclofenac
order generic accutane order generic zithromax 250mg buy zithromax 500mg online
indomethacin pills purchase lamisil online trimox 250mg cheap
order arimidex 1mg pill order generic arimidex 1 mg cost of viagra 100mg
order deltasone 40mg without prescription cheap cialis buy viagra 100mg online cheap
buy generic accutane 10mg stromectol 12mg tablet stromectol ebay
buy doxycycline 100mg order lasix 40mg online cheap order lasix 100mg generic
prograf 5mg tablet requip 1mg brand order urso 300mg for sale
molnupiravir 200mg over the counter order omnicef 300mg without prescription lansoprazole over the counter
buy sertraline Viagra 100 mg sildenafil 50mg us
cheap cialis without prescription buy viagra next day delivery buy sildenafil 50mg sale
avlosulfon 100 mg generic buy dapsone 100mg online cheap aceon 8mg cost
order provera order microzide without prescription periactin 4mg over the counter
fluvoxamine canada buy glipizide 5mg sale buy glipizide sale
purchase nootropil pill piracetam 800mg drug cheap sildenafil online
zithromax 250mg for sale buy azithromycin 250mg sale neurontin 100mg usa
cialis 5mg canada tadalafil 20mg uk order anafranil 25mg without prescription
chloroquine for sale cenforce tablet buy olumiant 2mg pills
purchase zyprexa without prescription brand diovan valsartan pills
order amlodipine pill buy generic amlodipine 10mg tadalafil 5mg oral
brand omeprazole 20mg blackjack poker online free roulette
lopressor order vardenafil 10mg without prescription buy levitra online cheap
purchase clomiphene generic buy albuterol generic play poker online free
dapoxetine 60mg ca dapoxetine 60mg without prescription levothyroxine over the counter
order orlistat 120mg generic generic zovirax 400mg buy acyclovir 800mg online cheap
cialis 40mg propranolol uk order clopidogrel 75mg for sale
cialis pills 20mg buy inderal 10mg generic buy plavix 75mg for sale
buy zyloprim for sale purchase rosuvastatin for sale zetia brand
order generic methotrexate 10mg methotrexate online order buy reglan 20mg generic
cozaar 25mg drug buy losartan 50mg online cheap topamax ca
buy ozobax online cheap ozobax order buy generic toradol
zantac 300mg usa generic meloxicam 15mg order celecoxib 100mg pills
brand tamsulosin 0.4mg buy generic zofran 4mg aldactone ca
cialis daily cost ampicillin 500mg sale order cipro 1000mg pills
cheap cephalexin 500mg clindamycin over the counter buy erythromycin 500mg online
sildenafil drug cialis 40mg over the counter buy cialis 40mg
online blackjack free casino slot tadalafil 40mg for sale
purchase sildalis sale brand estrace 2mg order lamictal generic
prednisone pill cost amoxicillin 500mg buy amoxil 250mg pills
order viagra 100mg online cheap viagra 50mg brand tadalafil 40mg usa
online casino with free signup bonus real money usa best ed pills at gnc tadalafil medication
fildena 100mg us fildena 50mg pills buy generic budesonide