> php教程 > php手册 > 본문

浅谈php函数的参数的设定规则

WBOY
풀어 주다: 2016-06-06 19:50:41
원래의
914명이 탐색했습니다.

函数可以设定参数,也可以留空不进行设定。 1.当php函数设定形参,但是引用时实参不给,情况如下: 1 !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" 2 html xmlns="http://www

  函数可以设定参数,也可以留空不进行设定。

1.当php函数设定形参,但是引用时实参不给,情况如下:

<span> 1</span> span>PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<span> 2</span> 
<span> 3</span> 
<span> 4</span> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<span> 5</span> <title>php函数测试</title>
<span> 6</span> 
<span> 7</span> 
<span> 8</span> 
<span> 9</span> <span>php 
</span><span>10</span> <span>/*</span><span>********************
</span><span>11</span> <span>*Author:瞎猫碰上死老虎***
</span><span>12</span> <span>*Date:2/6/2015********
</span><span>13</span> <span>********************</span><span>*/</span>
<span>14</span> 
<span>15</span> <span>function</span> asd(<span>$a</span>,<span>$b</span>,<span>$c</span><span>){
</span><span>16</span>     <span>echo</span> <span>$a</span>+<span>$b</span>+<span>$c</span><span>;
</span><span>17</span> <span>}
</span><span>18</span> 
<span>19</span> asd(<span>);
</span><span>20</span> 
<span>21</span> 
<span>22</span> ?>
<span>23</span> 
<span>24</span> 
로그인 후 복사

结果为:

<span> 1</span> Warning: Missing argument 1 <span>for</span> asd(), called in F:\phpfind\WWW\ceshi.php on line 19 and <span>defined</span> in F:\phpfind\WWW\ceshi.php on line 15
<span> 2</span> 
<span> 3</span> Warning: Missing argument 2 <span>for</span> asd(), called in F:\phpfind\WWW\ceshi.php on line 19 and <span>defined</span> in F:\phpfind\WWW\ceshi.php on line 15
<span> 4</span> 
<span> 5</span> Warning: Missing argument 3 <span>for</span> asd(), called in F:\phpfind\WWW\ceshi.php on line 19 and <span>defined</span> in F:\phpfind\WWW\ceshi.php on line 15
<span> 6</span> 
<span> 7</span> Notice: Undefined variable: b in F:\phpfind\WWW\ceshi.php on line 16
<span> 8</span> 
<span> 9</span> Notice: Undefined variable: a in F:\phpfind\WWW\ceshi.php on line 16
<span>10</span> 
<span>11</span> Notice: Undefined variable: c in F:\phpfind\WWW\ceshi.php on line 16
<span>12</span> 0
로그인 후 복사

结论:会报错,但是也能显示出结果来。为0。

 

2.函数有形参,引用时,第一个实参什么都没有用逗号隔开,后面给实参。这个时候语法错误。这种写法在PHP是不支持的。如果你想这样,可以给NULL。示例如下:

<span> 1</span> span>PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<span> 2</span> 
<span> 3</span> 
<span> 4</span> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<span> 5</span> <title>php函数测试</title>
<span> 6</span> 
<span> 7</span> 
<span> 8</span> 
<span> 9</span> <span>php 
</span><span>10</span> <span>/*</span><span>********************
</span><span>11</span> <span>*Author:瞎猫碰上死老虎***
</span><span>12</span> <span>*Date:2/6/2015********
</span><span>13</span> <span>********************</span><span>*/</span>
<span>14</span> 
<span>15</span> <span>function</span> asd(<span>$a</span>,<span>$b</span>,<span>$c</span><span>){
</span><span>16</span>     <span>echo</span> <span>$a</span>+<span>$b</span>+<span>$c</span><span>;
</span><span>17</span> <span>}
</span><span>18</span> 
<span>19</span> asd(<span>NULL</span>,3,7<span>);
</span><span>20</span> 
<span>21</span> 
<span>22</span> ?>
<span>23</span> 
<span>24</span> 
로그인 후 복사

结果为:

<span>1</span> 10
로그인 후 복사

 

3.现在来进行测试形参设置了默认参数的情况,情况如下:

<span> 1</span> span>PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<span> 2</span> 
<span> 3</span> 
<span> 4</span> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<span> 5</span> <title>php函数测试</title>
<span> 6</span> 
<span> 7</span> 
<span> 8</span> 
<span> 9</span> <span>php 
</span><span>10</span> <span>/*</span><span>********************
</span><span>11</span> <span>*Author:瞎猫碰上死老虎***
</span><span>12</span> <span>*Date:2/6/2015********
</span><span>13</span> <span>********************</span><span>*/</span>
<span>14</span> 
<span>15</span> <span>function</span> asd(<span>$a</span>,<span>$b</span>=2,<span>$c</span>=5<span>){
</span><span>16</span>     <span>echo</span> <span>$a</span>+<span>$b</span>+<span>$c</span><span>;
</span><span>17</span> <span>}
</span><span>18</span> 
<span>19</span> asd(3,7<span>);
</span><span>20</span> 
<span>21</span> 
<span>22</span> ?>
<span>23</span> 
<span>24</span> 
로그인 후 복사

结果为:

<span>1</span> 15
로그인 후 복사

结论:如果有默认形参,实参可以不给。实参的顺序是按照形参的顺序来的。如果函数参数中,参数的后面有默认形参,那么实参可以不给出对应的参数。函数将按默认形参代入计算。

 

4.再来看一种情况。这个时候三个参数中,就中间设置了默认形参。如果给了两个实参,这个情况是怎么样的呢。

<span> 1</span> span>PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<span> 2</span> 
<span> 3</span> 
<span> 4</span> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<span> 5</span> <title>php函数测试</title>
<span> 6</span> 
<span> 7</span> 
<span> 8</span> 
<span> 9</span> <span>php 
</span><span>10</span> <span>/*</span><span>********************
</span><span>11</span> <span>*Author:瞎猫碰上死老虎***
</span><span>12</span> <span>*Date:2/6/2015********
</span><span>13</span> <span>********************</span><span>*/</span>
<span>14</span> 
<span>15</span> <span>function</span> asd(<span>$a</span>,<span>$b</span>=2,<span>$c</span><span>){
</span><span>16</span>     <span>echo</span> <span>$a</span>+<span>$b</span>+<span>$c</span><span>;
</span><span>17</span> <span>}
</span><span>18</span> 
<span>19</span> asd(8,9<span>);
</span><span>20</span> 
<span>21</span> 
<span>22</span> ?>
<span>23</span> 
<span>24</span> 
로그인 후 복사

结果为:

<span>1</span> Warning: Missing argument 3 <span>for</span> asd(), called in F:\phpfind\WWW\ceshi.php on line 19 and <span>defined</span> in F:\phpfind\WWW\ceshi.php on line 15
<span>2</span> 
<span>3</span> Notice: Undefined variable: c in F:\phpfind\WWW\ceshi.php on line 16
<span>4</span> 17
로그인 후 복사

结论:引用函数的时候,实参的顺序跟形参一一对应。如果无默认形参,不给相应的实参则会报错。当有默认形参在函数的参数组后面,这个时候省略是不会出现错误的,函数将按照默认形参参与运算。

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿