php function 有什么好处?解决方案

WBOY
Release: 2016-06-13 12:58:56
Original
903 people have browsed it

php function 有什么好处?
PHP function 到底有什么好处? 可以自动清除数组,清除变量,清除MEMORY吗?


<br />
$array = array('http://www.google.com.hk','http://www.baidu.com','http://www.1.com/','http://www.yahoo.com.cn');<br />
foreach($array as $url){<br />
    $ch = curl_init();<br />
	curl_setopt($ch, CURLOPT_URL, $url);<br />
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br />
	curl_setopt($ch, CURLOPT_HEADER, true);<br />
	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)');<br />
	$html = curl_exec($ch);<br />
	curl_close($ch);<br />
	echo $html.'<hr />';<br />
	$html = NULL;<br />
        unset($html);<br />
}<br />
Copy after login


function get_html($url){<br />
	$ch = curl_init();<br />
	curl_setopt($ch, CURLOPT_URL, $url);<br />
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br />
	curl_setopt($ch, CURLOPT_HEADER, true);<br />
	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)');<br />
	$html = curl_exec($ch);<br />
	curl_close($ch);<br />
	return $html;<br />
}<br />
$array = array('http://www.google.com.hk','http://www.baidu.com','http://www.1.com/','http://www.yahoo.com.cn');<br />
foreach($array as $url){<br />
    get_html($url);<br />
}
Copy after login

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


------解决方案--------------------
对于你后面的两个问题我不知道是什么意思。
其实简简单单的想,你用函数封装了功能实现,这样代码不更好看吗?
------解决方案--------------------
unset 不unset 两种方法都没关系...写成函数的方式 方便你随时调用 代码结构更清晰...

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template