<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kej.tw Revisited &#187; php</title>
	<atom:link href="http://blog.kej.tw/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.kej.tw</link>
	<description>一個懶鬼的部落格</description>
	<lastBuildDate>Wed, 27 Apr 2022 20:02:14 +0000</lastBuildDate>
	<language>zh-TW</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8.3</generator>
	<item>
		<title>PHP設定錯誤回報等級</title>
		<link>http://blog.kej.tw/2005-10/09/php_error_reporting/</link>
		<comments>http://blog.kej.tw/2005-10/09/php_error_reporting/#comments</comments>
		<pubDate>Sun, 09 Oct 2005 11:46:51 +0000</pubDate>
		<dc:creator><![CDATA[Kej]]></dc:creator>
				<category><![CDATA[程式設計]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.kejjang.com/?p=4</guid>
		<description><![CDATA[用error_reporting()函式來自訂錯誤回報的等級，以取得所有的錯誤/警告/注意信息]]></description>
				<content:encoded><![CDATA[<p>開始寫PHP以來，就對一個小地方很感冒，就是PHP並沒有禁止使用未宣告或初始化過的變數，也沒有提出警告。大概是因為自己也算是個容易粗心大意的人吧，所以有時候會不小心將變數名稱key錯了或怎麼的，整個程式便跑出了奇怪、非預期的結果。這時要debug是非常吃力的一件事情，不禁讓我懷念起perl的use strict。</p>
<p>不過後來發現，這其實是PHP預設將注意信息(NOTICE)隱藏起來而已，因為會引起這些注意信息的錯誤(例如某變數未初始化)，並不會導致致命的結果，所以我們可以用error_reporting()函式來自訂錯誤回報的等級。</p>
<p>一般PHP預設值是7或2039，也就是<strong>E_ALL &#038; ~E_NOTICE</strong>(php.ini的寫法) 或 <strong>E_ALL ^ E_NOTICE</strong>(error_reporting函式內參數的寫法，我沒試過能不能混用，我懶嘛)。因此若是在程式內設定<strong>error_reporting(E_ALL)</strong>，便可以取得所有的錯誤 /警告 /注意信息，對於神經比較大條的程式設計師來說，debug就方便多了。當然程式完成之後，你也可以用<strong>error_reporting(0)</strong>來抑制所有錯誤警告，畢竟這些訊息是不需要給使用者看到的吧(呃，當然，如果你確定程式百分之百不會出錯的話)。</p>
<p>p.s.</p>
<ol style='margin-top:-10px'>
<li>在參數的使用上，應該儘可能使用E_ALL、E_WARNING等常數，盡量不要使用數字，以避免新版本的數字跟常數的定義規則發生變化。</li>
<li>當然，使用ini_set(&#8216;error_reporting&#8217;,E_ALL) 也能達到與error_reporting(E_ALL) 一樣的效果，不過為什麼要自找麻煩呢？ <img src="http://blog.kej.tw/wp-includes/images/smilies/icon_razz.gif" alt=":P" class="wp-smiley" />  </li>
</ol>
<p>參考：<a href="http://tw2.php.net/manual/tw/function.error-reporting.php">error_reporting()</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kej.tw/2005-10/09/php_error_reporting/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
