Home  >  Article  >  php教程  >  浅谈php函数的参数的设定规则

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

WBOY
WBOYOriginal
2016-06-06 19:50:41905browse

函数可以设定参数,也可以留空不进行设定。 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

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

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