• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php的代码重用与函数

php 搞代码 4年前 (2022-01-23) 13次浏览 已收录 0个评论
<?php//使用require()、include()函数echo 'This is the main file.
';require('reusable.php');echo 'The script will end now.
';//函数声明function my_function(){ echo 'My function was called';}my_function();//使用参数function create_table($data){ echo ""; reset($data); $value=current($data); while($value){ echo "\n"; $value=next($data); } echo "
".$value."
";}$my_array=array('Line one','Line two','Line three');create_table($my_array);//作用域 关键字globalfunction fn(){ global $var; $var="contens 2"; echo "inside the function,\$var =".$var."
";}$var="contents 1";fn();echo "outside the function,\$var =".$var."
";//参数的引用传递和值传递:地址符&function increment(&$value,$amount=1){ $value=$value+$amount;}$a=10;echo $a.'
';increment($a);echo $a.'
';//return关键字:终止函数的执行function larger($x,$y){ if((!isset($x))||(!isset($y))){ echo "This function requires two numbers.
"; return; } if($x>=$y){ echo $x."
"; }else{ echo $y."
"; }}$a=1;$b=2.5;$c=1.9;larger($a,$b);larger($d,$a);//return关键字:从函数返回一个值function smaller($x,$y){ if((!isset($x))||(!isset($y))){ return false; }else if($x<=$y){ return $x; }else{ return $y; }}echo smaller($a,$b).'
';echo smaller($d,$a).'
';//本文来源gaodai$ma#com搞$$代**码)网8搞代gaodaima码实现递归function reverse_r($str){ if(strlen($str)>0){ reverse_r(substr($str,1)); } echo substr($str,0,1); return;}function reverse_i($str){ for($i=1;$i<=strlen($str);$i++){ echo substr($str,-$i,1); } return;}reverse_r('hello');echo '
';reverse_i('hello');//phpinfo();


更多细节参考php手册—函数

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了php的代码重用与函数,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php的代码重用与函数
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址