php function 有什么好处?

mumubangditu 2012-12-08 09:26:36
PHP function 到底有什么好处? 可以自动清除数组,清除变量,清除MEMORY吗?



$array = array('http://www.google.com.hk','http://www.baidu.com','http://www.1.com/','http://www.yahoo.com.cn');
foreach($array as $url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)');
$html = curl_exec($ch);
curl_close($ch);
echo $html.'<hr />';
$html = NULL;
unset($html);
}


function get_html($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)');
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
$array = array('http://www.google.com.hk','http://www.baidu.com','http://www.1.com/','http://www.yahoo.com.cn');
foreach($array as $url){
get_html($url);
}

如果是function的话,是否也要unset数组?
还是只要return,下次执行function时会自动清空第一次执行function时的数组?

...全文
140 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
黄袍披身 2012-12-09
  • 打赏
  • 举报
回复
unset 不unset 两种方法都没关系...写成函数的方式 方便你随时调用 代码结构更清晰...
mumubangditu 2012-12-09
  • 打赏
  • 举报
回复
原来没有关系。
改一下昵称 2012-12-08
  • 打赏
  • 举报
回复
对于你后面的两个问题我不知道是什么意思。 其实简简单单的想,你用函数封装了功能实现,这样代码不更好看吗?

21,881

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧