单个参数
要将某些内容传递给函数,请使用以下命令:
function ContentAd($Arg){ //validation echo '<div class="Advertisement">'. $Arg .'</div>'; }
$Arg 变量将包含我们传递给函数的任何内容。
这些参数允许你做很多不同的事情,一个例子是,获取用户提交的链接,并将它们正确地包含在锚标签中。
<?php //First define the Function function ContentAd($Arg){ //validation echo '<div class="Advertisement">' . $Arg . '</div>'; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <title>My first Website</title> </head> <body> <?php //Then call the Function somewhere else in the Script. $content = '<a href="http://onitroad.com/">onitroad Tutorials</a>'; ContentAd($content); ?> </body> </html>
上面的输出仍然相同,但这次链接被传递给函数。
函数参数可用于将信息传递给函数,然后由函数内的代码处理或者操作。
如果函数包含在类中,则通常在使用类变量(属性)或者函数参数之间进行选择。
使用属性通常是更好的解决方案,因为要传递给函数的数据通常也会被其他函数使用。
调用具有参数的函数(方法),然后调用仅使用类属性的函数也更难。
字符串和整数
我们还可以使用字符串和整数作为参数,示例如下所示。
ContentAd('String', 144);
多个参数
我们可以通过用逗号 (,) 分隔来使用多个参数。
function ContentAd($Arg1, $Arg2){ //validation echo '<div class="Advertisement">'. $Arg1 .' - '. $Arg2 .'</div>'; }
调用函数时也使用逗号。
ContentAd($etc1, $etc2);
日期:2020-06-02 22:15:42 来源:oir作者:oir