<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://commons.oreilly.com/wiki/skins/common/feed.css?97"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>PHP Cookbook/Numbers - Revision history</title>
		<link>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Numbers&amp;action=history</link>
		<description>Revision history for this page on the wiki</description>
		<language>en</language>
		<generator>MediaWiki 1.11.0</generator>
		<lastBuildDate>Sat, 25 May 2013 17:28:18 GMT</lastBuildDate>
		<item>
			<title>Newacct: /* Solution */</title>
			<link>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Numbers&amp;diff=26129&amp;oldid=prev</link>
			<description>&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Solution&lt;/span&gt;&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 02:21, 30 January 2010&lt;/td&gt;
			&lt;/tr&gt;
		&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 282:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 282:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; $log10 = log10(10);      // 1&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; $log10 = log10(10);      // 1&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;For logs using other bases, use &amp;lt;tt&amp;gt;pc_logn( )&amp;lt;/tt&amp;gt;:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;For logs using other bases, use &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;tt&amp;gt;log( )&amp;lt;/tt&amp;gt; with the base as second argument (starting in PHP 4.3):&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt; &lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt; $log2  = log(10, 2); // 3.3219280948874&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;In PHP prior to 4.3, you can use the following &lt;/ins&gt;&amp;lt;tt&amp;gt;pc_logn( )&amp;lt;/tt&amp;gt; &lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;function&lt;/ins&gt;:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; function pc_logn($number, $base) {&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; function pc_logn($number, $base) {&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 289:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 293:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; $log2  = pc_logn(10, 2); // 3.3219280948874&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; $log2  = pc_logn(10, 2); // 3.3219280948874&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Since PHP 4.3, you should instead use the &amp;lt;tt&amp;gt;log( )&amp;lt;/tt&amp;gt; function with the base as second argument:&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; $log2  = log(10, 2); // 3.3219280948874&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;=== Discussion ===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;=== Discussion ===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</description>
			<pubDate>Sat, 30 Jan 2010 02:21:25 GMT</pubDate>			<dc:creator>Newacct</dc:creator>			<comments>http://commons.oreilly.com/wiki/index.php/Talk:PHP_Cookbook/Numbers</comments>		</item>
		<item>
			<title>Newacct: /* Solution */</title>
			<link>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Numbers&amp;diff=26126&amp;oldid=prev</link>
			<description>&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Solution&lt;/span&gt;&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 22:20, 29 January 2010&lt;/td&gt;
			&lt;/tr&gt;
		&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 289:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 289:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; $log2  = pc_logn(10, 2); // 3.3219280948874&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; $log2  = pc_logn(10, 2); // 3.3219280948874&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Since PHP 4.3, you should instead use the &amp;lt;tt&amp;gt;log( )&amp;lt;/tt&amp;gt; function with the base as second argument:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; $log2  = log(10, 2); // 3.3219280948874&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;=== Discussion ===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;=== Discussion ===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key wikicontent:diff:version:1.11a:oldid:7314:newid:26126 --&gt;
&lt;/table&gt;</description>
			<pubDate>Fri, 29 Jan 2010 22:20:35 GMT</pubDate>			<dc:creator>Newacct</dc:creator>			<comments>http://commons.oreilly.com/wiki/index.php/Talk:PHP_Cookbook/Numbers</comments>		</item>
		<item>
			<title>Docbook2Wiki: Initial conversion from Docbook</title>
			<link>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Numbers&amp;diff=7314&amp;oldid=prev</link>
			<description>&lt;p&gt;Initial conversion from Docbook&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 13:36, 7 March 2008&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/table&gt;</description>
			<pubDate>Fri, 07 Mar 2008 13:36:04 GMT</pubDate>			<dc:creator>Docbook2Wiki</dc:creator>			<comments>http://commons.oreilly.com/wiki/index.php/Talk:PHP_Cookbook/Numbers</comments>		</item>
		<item>
			<title>Evanlenz: 1 revision(s)</title>
			<link>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Numbers&amp;diff=3107&amp;oldid=prev</link>
			<description>&lt;p&gt;1 revision(s)&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 22:29, 6 March 2008&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/table&gt;</description>
			<pubDate>Thu, 06 Mar 2008 22:29:57 GMT</pubDate>			<dc:creator>Evanlenz</dc:creator>			<comments>http://commons.oreilly.com/wiki/index.php/Talk:PHP_Cookbook/Numbers</comments>		</item>
		<item>
			<title>Docbook2Wiki: Initial conversion from Docbook</title>
			<link>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Numbers&amp;diff=3106&amp;oldid=prev</link>
			<description>&lt;p&gt;Initial conversion from Docbook&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{PHP Cookbook/TOC}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In everyday life, numbers are easy to identify. They're 3:00 P.M., as in the current time, or $1.29, as in the cost of a pint of milk. Maybe they're like π, the ratio of the circumference to the diameter of a circle. They can be pretty large, like Avogadro's number, which is about 6 x 10&amp;lt;sup&amp;gt;23&amp;lt;/sup&amp;gt;. In PHP, numbers can be all these things.&lt;br /&gt;
&lt;br /&gt;
However, PHP doesn't treat all these numbers as &amp;quot;numbers.&amp;quot; Instead, it breaks them down into two groups: integers and floating-point numbers. Integers are whole numbers, such as -4, 0, 5, and 1,975. Floating-point numbers are decimal numbers, such as -1.23, 0.0, 3.14159, and 9.9999999999.&lt;br /&gt;
&lt;br /&gt;
Conveniently, most of the time PHP doesn't make you worry about the differences between the two because it automatically converts integers to floating-point numbers and floating-point numbers to integers. This conveniently allows you to ignore the underlying details. It also means &amp;lt;tt&amp;gt;3/2&amp;lt;/tt&amp;gt; is &amp;lt;tt&amp;gt;1.5&amp;lt;/tt&amp;gt;, not &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt;, as it would be in some programming languages. PHP also automatically converts from strings to numbers and back. For instance, &amp;lt;tt&amp;gt;1+&amp;quot;1&amp;quot;&amp;lt;/tt&amp;gt; is &amp;lt;tt&amp;gt;2&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
However, sometimes this blissful ignorance can cause trouble. First, numbers can't be infinitely large or small; there's a minimum size of 2.2e-308 and a maximum size of about 1.8e308.&amp;lt;ref&amp;gt;These numbers are actually platform-specific, but the values are common because they are from the 64-bit IEEE standard 754.&amp;lt;/ref&amp;gt; If you need larger (or smaller) numbers, you must use the BCMath or GMP libraries, which are discussed in [[PHP Cookbook/Numbers#Handling Very Large or Very Small Numbers|Recipe 2.14]].&lt;br /&gt;
&lt;br /&gt;
Next, floating-point numbers aren't guaranteed to be exactly correct but only correct plus or a minus a small amount. Now, this amount is small enough for most occasions, but you can end up with problems in certain instances. For instance, humans automatically convert 6 followed by an endless string of 9s after the decimal point to 7, but PHP thinks it's 6 with a bunch of 9s. Therefore, if you ask PHP for the integer value of that number, it returns 6, not 7. For similar reasons, if the digit located in the 200th decimal place is significant, floating-point numbers aren't useful. Again, the BCMath and GMP libraries ride to the rescue. But, for most occasions, PHP behaves very nicely when playing with numbers and lets you treat them just as you do in real life.&lt;br /&gt;
&lt;br /&gt;
== Checking Whether a String Contains a Valid Number ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to ensure that a string contains a number. For example, you want to validate an age that the user has typed into a form input field.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;is_numeric( )&amp;lt;/tt&amp;gt; :&lt;br /&gt;
&lt;br /&gt;
 if (is_numeric('five')) { /* false */ }&lt;br /&gt;
 &lt;br /&gt;
 if (is_numeric(5))      { /* true  */ }&lt;br /&gt;
 if (is_numeric('5'))    { /* true  */ }&lt;br /&gt;
 &lt;br /&gt;
 if (is_numeric(-5))     { /* true  */ }&lt;br /&gt;
 if (is_numeric('-5'))   { /* true  */ }&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Besides working on numbers, &amp;lt;tt&amp;gt;is_numeric( )&amp;lt;/tt&amp;gt; can also be applied to numeric strings. The distinction here is that the integer &amp;lt;tt&amp;gt;5&amp;lt;/tt&amp;gt; and the string &amp;lt;tt&amp;gt;5&amp;lt;/tt&amp;gt; technically aren't the same in PHP.&amp;lt;ref&amp;gt;The most glaring example of this difference came during the transition from PHP 3 to PHP 4. In PHP 3, &amp;lt;tt&amp;gt;empty('0')&amp;lt;/tt&amp;gt; returned &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;, but as of PHP 4, it returns &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt;. On the other hand, &amp;lt;tt&amp;gt;empty(0)&amp;lt;/tt&amp;gt; has always returned &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt; and still does. (Actually, you need to call &amp;lt;tt&amp;gt;empty( )&amp;lt;/tt&amp;gt; on ''variables'' containing '&amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt;' and &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt;.) See the Introduction to [[PHP Cookbook/Variables|Chapter 5]] for details.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Helpfully, &amp;lt;tt&amp;gt;is_numeric( )&amp;lt;/tt&amp;gt; properly parses decimal numbers, such as &amp;lt;tt&amp;gt;5.1&amp;lt;/tt&amp;gt;; however, numbers with thousands separators, such as &amp;lt;tt&amp;gt;5,100&amp;lt;/tt&amp;gt;, cause &amp;lt;tt&amp;gt;is_numeric( )&amp;lt;/tt&amp;gt; to return &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To strip the thousands separators from your number before calling &amp;lt;tt&amp;gt;is_numeric( )&amp;lt;/tt&amp;gt; use &amp;lt;tt&amp;gt;str_replace( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;is_numeric(str_replace($number, ',', ''));&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To check if your number is a specific type, there are a variety of self-explanatorily named related functions: &amp;lt;tt&amp;gt;is_bool( )&amp;lt;/tt&amp;gt; , &amp;lt;tt&amp;gt;is_float( )&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;is_double( )&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;is_real( )&amp;lt;/tt&amp;gt;; they're all the same), and &amp;lt;tt&amp;gt;is_int( )&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;is_integer( )&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;is_long( )&amp;lt;/tt&amp;gt; ).&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;is_numeric( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/is-numeric'' and &amp;lt;tt&amp;gt;str_replace( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/str-replace''.&lt;br /&gt;
&lt;br /&gt;
== Comparing Floating-Point Numbers ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to check whether two floating-point numbers are equal.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use a small delta value, and check if the numbers are equal within that delta:&lt;br /&gt;
&lt;br /&gt;
 $delta = 0.00001;&lt;br /&gt;
 &lt;br /&gt;
 $a = 1.00000001;&lt;br /&gt;
 $b = 1.00000000;&lt;br /&gt;
 &lt;br /&gt;
 if (abs($a - $b) &amp;lt; $delta) { /* $a and $b are equal */ }&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Floating-point numbers are represented in binary form with only a finite number of bits for the mantissa and the exponent. You get overflows when you exceed those bits. As a result, sometimes PHP (and other languages, too) don't believe two equal numbers are actually equal because they may differ toward the very end.&lt;br /&gt;
&lt;br /&gt;
To avoid this problem, instead of checking if &amp;lt;tt&amp;gt;$a == $b&amp;lt;/tt&amp;gt;, make sure the first number is within a very small amount (&amp;lt;tt&amp;gt;$delta&amp;lt;/tt&amp;gt;) of the second one. The size of your delta should be the smallest amount of difference you care about between two numbers. Then use &amp;lt;tt&amp;gt;abs( )&amp;lt;/tt&amp;gt; to get the absolute value of the difference.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Numbers#Rounding Floating-Point Numbers|Recipe 2.4]] for information on rounding floating-point numbers; documentation on floating-point numbers in PHP at ''http://www.php.net/language.types.float''.&lt;br /&gt;
&lt;br /&gt;
== Rounding Floating-Point Numbers ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to round a floating-point number, either to an integer value or to a set number of decimal places.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
To round a number to the closest integer, use &amp;lt;tt&amp;gt;round( )&amp;lt;/tt&amp;gt; :&lt;br /&gt;
&lt;br /&gt;
 $number = round(2.4);   // $number = 2&lt;br /&gt;
&lt;br /&gt;
To round up, use &amp;lt;tt&amp;gt;ceil( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 $number = ceil(2.4);    // $number = 3&lt;br /&gt;
&lt;br /&gt;
To round down, use &amp;lt;tt&amp;gt;floor( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 $number = floor(2.4);   // $number = 2&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
If a number falls exactly between two integers, its behavior is undefined:&lt;br /&gt;
&lt;br /&gt;
 $number = round(2.5);   // $number is 2 or 3!&lt;br /&gt;
&lt;br /&gt;
Be careful! As we mention in [[PHP Cookbook/Numbers#Comparing Floating-Point Numbers|Recipe 2.3]], floating-point numbers don't always work out to exact values because of how they're stored internally by the computer. This can create situations in which the obvious answer isn't. A value you expect to have a decimal part of &amp;quot;0.5&amp;quot; might instead be &amp;quot;.499999...9&amp;quot; (with a whole bunch of 9s) or &amp;quot;.500000...1&amp;quot; (with many 0s and a trailing 1). If you want to ensure that a number is rounded up as you might expect, add a small delta value to it before rounding:&lt;br /&gt;
&lt;br /&gt;
 $delta = 0.0000001;&lt;br /&gt;
 $number = round(2.5 + $delta);   // $number = 3&lt;br /&gt;
&lt;br /&gt;
To keep a set number of digits after the decimal point, &amp;lt;tt&amp;gt;round( )&amp;lt;/tt&amp;gt; accepts an optional precision argument. For example, if you are calculating the total price for the items in a user's shopping cart:&lt;br /&gt;
&lt;br /&gt;
 $cart = 54.23;&lt;br /&gt;
 $tax = $cart * .05;&lt;br /&gt;
 $total = $cart + $tax;       // $total = 56.9415&lt;br /&gt;
 &lt;br /&gt;
 $final = round($total, 2);   // $final = 56.94&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Numbers#Comparing Floating-Point Numbers|Recipe 2.3]] for information on comparing floating-point numbers; documentation on &amp;lt;tt&amp;gt;round( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/round''.&lt;br /&gt;
&lt;br /&gt;
== Operating on a Series of Integers ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to apply a piece of code over a range of integers.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use the &amp;lt;tt&amp;gt;range( )&amp;lt;/tt&amp;gt; function, which returns an array populated with integers:&lt;br /&gt;
&lt;br /&gt;
 foreach(range($start,$end) as $i) {&lt;br /&gt;
     plot_point($i);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Instead of using &amp;lt;tt&amp;gt;range( )&amp;lt;/tt&amp;gt;, it can be more efficient to use a &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop. Also, you can increment using values other than 1. For example:&lt;br /&gt;
&lt;br /&gt;
 for ($i = $start; $i &amp;lt;= $end; $i += $increment) {&lt;br /&gt;
     plot_point($i);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Loops like this are common. For instance, you could be plotting a function and need to calculate the results for multiple points on the graph. Or, you could be NASA counting down until the launch of the Space Shuttle Columbia.&lt;br /&gt;
&lt;br /&gt;
In the first example, &amp;lt;tt&amp;gt;range( )&amp;lt;/tt&amp;gt; returns an array with values from &amp;lt;tt&amp;gt;$start&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;$end&amp;lt;/tt&amp;gt;. Then &amp;lt;tt&amp;gt;foreach&amp;lt;/tt&amp;gt; pulls out each element and assigns it to &amp;lt;tt&amp;gt;$i&amp;lt;/tt&amp;gt; inside of the loop. The advantage of using &amp;lt;tt&amp;gt;range( )&amp;lt;/tt&amp;gt; is its brevity, but this technique has a few disadvantages. For one, a large array can take up unnecessary memory. Also, you're forced to increment the series one number at a time, so you can't loop through a series of even integers, for example.&lt;br /&gt;
&lt;br /&gt;
As of PHP 4.1, it is valid for &amp;lt;tt&amp;gt;$start&amp;lt;/tt&amp;gt; to be larger than &amp;lt;tt&amp;gt;$end&amp;lt;/tt&amp;gt;. In this case, the numbers returned by &amp;lt;tt&amp;gt;range( )&amp;lt;/tt&amp;gt; are in descending order. Also, you can use iterate over character sequences:&lt;br /&gt;
&lt;br /&gt;
 print_r(range('l', 'p'));&lt;br /&gt;
 '''Array'''&lt;br /&gt;
                '''('''&lt;br /&gt;
                '''    [0] =&amp;gt; l'''&lt;br /&gt;
                '''    [1] =&amp;gt; m'''&lt;br /&gt;
                '''    [2] =&amp;gt; n'''&lt;br /&gt;
                '''    [3] =&amp;gt; o'''&lt;br /&gt;
                '''    [4] =&amp;gt; p'''&lt;br /&gt;
                ''')'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop method just uses a single integer and avoids the array entirely. While it's longer, you have greater control over the loop, because you can increment and decrement &amp;lt;tt&amp;gt;$i&amp;lt;/tt&amp;gt; more freely. Also, you can modify &amp;lt;tt&amp;gt;$i&amp;lt;/tt&amp;gt; from inside the loop, something you can't do with &amp;lt;tt&amp;gt;range( )&amp;lt;/tt&amp;gt;, because PHP reads in the entire array when it enters the loop, and changes to the array don't effect the sequence of elements.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Arrays#Initializing an Array to a Range of Integers|Recipe 4.4]] for details on initializing an array to a range of integers; documentation on &amp;lt;tt&amp;gt;range( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/range''.&lt;br /&gt;
&lt;br /&gt;
== Generating Random Numbers Within a Range ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to generate a random number within a range of numbers.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;mt_rand( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 // random number between $upper and $lower, inclusive&lt;br /&gt;
 $random_number = mt_rand($lower, $upper);&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Generating random numbers is useful when you want to display a random image on a page, randomize the starting position of a game, select a random record from a database, or generate a unique session identifier.&lt;br /&gt;
&lt;br /&gt;
To generate a random number between two end points, pass &amp;lt;tt&amp;gt;mt_rand( )&amp;lt;/tt&amp;gt; two arguments:&lt;br /&gt;
&lt;br /&gt;
 $random_number = mt_rand(1, 100);&lt;br /&gt;
&lt;br /&gt;
Calling &amp;lt;tt&amp;gt;mt_rand( )&amp;lt;/tt&amp;gt; without any arguments returns a number between &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt; and the maximum random number, which is returned by &amp;lt;tt&amp;gt;mt_getrandmax( )&amp;lt;/tt&amp;gt; .&lt;br /&gt;
&lt;br /&gt;
Generating truly random numbers is hard for computers to do. Computers excel at following instructions methodically; they're not so good at spontaneity. If you want to instruct a computer to return random numbers, you need to give it a specific set of repeatable commands; the very fact that they're repeatable undermines the desired randomness.&lt;br /&gt;
&lt;br /&gt;
PHP has two different random number generators, a classic function called &amp;lt;tt&amp;gt;rand( )&amp;lt;/tt&amp;gt; and a better function called &amp;lt;tt&amp;gt;mt_rand( )&amp;lt;/tt&amp;gt;. MT stands for Mersenne Twister, which is named for the French monk and mathematician Marin Mersenne and the type of prime numbers he's associated with. The algorithm is based on these prime numbers. Since &amp;lt;tt&amp;gt;mt_rand( )&amp;lt;/tt&amp;gt; is more random and faster than &amp;lt;tt&amp;gt;rand( )&amp;lt;/tt&amp;gt;, we prefer it to &amp;lt;tt&amp;gt;rand( )&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you're running a version of PHP earlier than 4.2, before using &amp;lt;tt&amp;gt;mt_rand( )&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;rand( )&amp;lt;/tt&amp;gt;) for the first time in a script, you need to seed the generator, by calling &amp;lt;tt&amp;gt;mt_srand( )&amp;lt;/tt&amp;gt; (or &amp;lt;tt&amp;gt;srand( )&amp;lt;/tt&amp;gt;). The ''seed'' is a number the random function uses as the basis for generating the random numbers it returns; it's how to solve the repeatable versus random dilemma mentioned earlier. Use the value returned by &amp;lt;tt&amp;gt;microtime( )&amp;lt;/tt&amp;gt; , a high-precision time function, to get a seed that changes very quickly and is unlikely to repeat — qualities desirable in a good seed. After the initial seed, you don't need to reseed the randomizer. PHP 4.2 and later automatically handles seeding for you, but if you manually provide a seed before calling &amp;lt;tt&amp;gt;mt_rand( )&amp;lt;/tt&amp;gt; for the first time, PHP doesn't alter it by substituting a new seed of its own.&lt;br /&gt;
&lt;br /&gt;
If you want to select a random record from a database — an easy way is to find the total number of fields inside the table — select a random number in that range, and then request that row from the database:&lt;br /&gt;
&lt;br /&gt;
 $sth = $dbh-&amp;gt;query('SELECT COUNT(*) AS count FROM quotes');&lt;br /&gt;
 if ($row = $sth-&amp;gt;fetchRow()) {&lt;br /&gt;
     $count = $row[0];&lt;br /&gt;
 } else {&lt;br /&gt;
     die ($row-&amp;gt;getMessage());&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 $random = mt_rand(0, $count - 1);&lt;br /&gt;
 &lt;br /&gt;
 $sth = $dbh-&amp;gt;query(&amp;quot;SELECT quote FROM quotes LIMIT $random,1&amp;quot;);&lt;br /&gt;
 while ($row = $sth-&amp;gt;fetchRow()) {&lt;br /&gt;
     print $row[0] . &amp;quot;\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
This snippet finds the total number of rows in the table, computes a random number inside that range, and then uses &amp;lt;tt&amp;gt;LIMIT $random,1&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;SELECT&amp;lt;/tt&amp;gt; one line from the table starting at position &amp;lt;tt&amp;gt;$random&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if you're using MySQL 3.23 or above, you can do this:&lt;br /&gt;
&lt;br /&gt;
 $sth = $dbh-&amp;gt;query('SELECT quote FROM quotes ORDER BY RAND() LIMIT 1');&lt;br /&gt;
 while ($row = $sth-&amp;gt;fetchRow()) {&lt;br /&gt;
     print $row[0] . &amp;quot;\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In this case, MySQL randomizes the lines, and then the first row is returned.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Numbers#Generating Biased Random Numbers|Recipe 2.7]] for how to generate biased random numbers; documentation on &amp;lt;tt&amp;gt;mt_rand( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/mt-rand'' and &amp;lt;tt&amp;gt;rand( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/rand''; the MySQL Manual on &amp;lt;tt&amp;gt;RAND( )&amp;lt;/tt&amp;gt; at ''http://www.mysql.com/doc/M/a/Mathematical_functions.html''.&lt;br /&gt;
&lt;br /&gt;
== Generating Biased Random Numbers ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to generate random numbers, but you want these numbers to be somewhat biased, so that numbers in certain ranges appear more frequently than others. For example, you want to spread out a series of banner ad impressions in proportion to the number of impressions remaining for each ad campaign.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use the &amp;lt;tt&amp;gt;pc_rand_weighted( )&amp;lt;/tt&amp;gt; function shown in [[PHP Cookbook/Numbers#phpckbk-CHP-2-EX-1|Example 2-1]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-2-EX-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 2-1. pc_rand_weighted( )'''&lt;br /&gt;
&lt;br /&gt;
 // returns the weighted randomly selected key&lt;br /&gt;
 function pc_rand_weighted($numbers) {&lt;br /&gt;
     $total = 0;&lt;br /&gt;
     foreach ($numbers as $number =&amp;gt; $weight) {&lt;br /&gt;
         $total += $weight;&lt;br /&gt;
         $distribution[$number] = $total;&lt;br /&gt;
     }&lt;br /&gt;
     $rand = mt_rand(0, $total - 1);&lt;br /&gt;
     foreach ($distribution as $number =&amp;gt; $weights) {&lt;br /&gt;
         if ($rand &amp;lt; $weights) { return $number; }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Imagine if instead of an array in which the values are the number of remaining impressions, you have an array of ads in which each ad occurs exactly as many times as its remaining number of impressions. You can simply pick an unweighted random place within the array, and that'd be the ad that shows.&lt;br /&gt;
&lt;br /&gt;
This technique can consume a lot of memory if you have millions of impressions remaining. Instead, you can calculate how large that array would be (by totalling the remaining impressions), pick a random number within the size of the make-believe array, and then go through the array figuring out which ad corresponds to the number you picked. For instance:&lt;br /&gt;
&lt;br /&gt;
 $ads = array('ford' =&amp;gt; 12234, // advertiser, remaining impressions&lt;br /&gt;
              'att'  =&amp;gt; 33424,&lt;br /&gt;
              'ibm'  =&amp;gt; 16823);&lt;br /&gt;
 &lt;br /&gt;
 $ad = pc_rand_weighted($ads);&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Numbers#Generating Random Numbers Within a Range|Recipe 2.6]] for how to generate random numbers within a range.&lt;br /&gt;
&lt;br /&gt;
== Taking Logarithms ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to take the logarithm of a number.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
For logs using base ''e'' (natural log), use &amp;lt;tt&amp;gt;log( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 $log = log(10);          // 2.30258092994&lt;br /&gt;
&lt;br /&gt;
For logs using base 10, use &amp;lt;tt&amp;gt;log10( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 $log10 = log10(10);      // 1&lt;br /&gt;
&lt;br /&gt;
For logs using other bases, use &amp;lt;tt&amp;gt;pc_logn( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 function pc_logn($number, $base) {&lt;br /&gt;
     return log($number) / log($base);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 $log2  = pc_logn(10, 2); // 3.3219280948874&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Both &amp;lt;tt&amp;gt;log( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;log10( )&amp;lt;/tt&amp;gt; are defined only for numbers that are greater than zero. The &amp;lt;tt&amp;gt;pc_logn( )&amp;lt;/tt&amp;gt; function uses the change of base formula, which says that the log of a number in base &amp;lt;tt&amp;gt;n&amp;lt;/tt&amp;gt; is equal to the log of that number, divided by the log of &amp;lt;tt&amp;gt;n&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;log( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/log'' and &amp;lt;tt&amp;gt;log10( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/log10''.&lt;br /&gt;
&lt;br /&gt;
== Calculating Exponents ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to raise a number to a power.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
To raise ''e'' to a power, use &amp;lt;tt&amp;gt;exp( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 $exp = exp(2);        // 7.3890560989307&lt;br /&gt;
&lt;br /&gt;
To raise it to any power, use &amp;lt;tt&amp;gt;pow( )&amp;lt;/tt&amp;gt; :&lt;br /&gt;
&lt;br /&gt;
 $exp = pow( 2, M_E);  // 6.5808859910179&lt;br /&gt;
 &lt;br /&gt;
 $pow = pow( 2, 10);   // 1024&lt;br /&gt;
 $pow = pow( 2, -2);   // 0.25&lt;br /&gt;
 $pow = pow( 2, 2.5);  // 5.6568542494924&lt;br /&gt;
 &lt;br /&gt;
 $pow = pow(-2, 10);   // 1024&lt;br /&gt;
 $pow = pow( 2, -2);   // 0.25&lt;br /&gt;
 $pow = pow(-2, -2.5); // NAN (Error: Not a Number)&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
The built-in constant &amp;lt;tt&amp;gt;M_E&amp;lt;/tt&amp;gt; is an approximation of the value of ''e''. It equals 2.7182818284590452354. So &amp;lt;tt&amp;gt;exp($n)&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;pow(M_E,&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;$n)&amp;lt;/tt&amp;gt; are identical.&lt;br /&gt;
&lt;br /&gt;
It's easy to create very large numbers using &amp;lt;tt&amp;gt;exp( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;pow( )&amp;lt;/tt&amp;gt;; if you outgrow PHP's maximum size (almost 1.8e308), see [[PHP Cookbook/Numbers#Handling Very Large or Very Small Numbers|Recipe 2.14]] for how to use the arbitrary precision functions. With these functions, PHP returns &amp;lt;tt&amp;gt;INF&amp;lt;/tt&amp;gt;, infinity, if the result is too large and &amp;lt;tt&amp;gt;NAN&amp;lt;/tt&amp;gt;, not-a-number, on an error.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;pow( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/pow'', &amp;lt;tt&amp;gt;exp( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/exp'', and information on predefined mathematical constants at ''http://www.php.net/math''.&lt;br /&gt;
&lt;br /&gt;
== Formatting Numbers ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You have a number and you want to print it with thousands and decimals separators. For instance, you want to display prices for items in a shopping cart.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use the &amp;lt;tt&amp;gt;number_format( )&amp;lt;/tt&amp;gt; function to format as an integer:&lt;br /&gt;
&lt;br /&gt;
 $number = 1234.56;&lt;br /&gt;
 print number_format($number);    // 1,235 because number is rounded up&lt;br /&gt;
&lt;br /&gt;
Specify a number of decimal places to format as a decimal:&lt;br /&gt;
&lt;br /&gt;
 print number_format($number, 2); // 1,234.56&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;number_format( )&amp;lt;/tt&amp;gt; function formats a number by inserting the correct decimal and thousands separators for your locale. If you want to manually specify these values, pass them as the third and fourth parameters:&lt;br /&gt;
&lt;br /&gt;
 $number = 1234.56;&lt;br /&gt;
 print number_format($number, 2, '@', '#'); // 1#234@56&lt;br /&gt;
&lt;br /&gt;
The third argument is used as the decimal point and the last separates thousands. If you use these options, you must specify both arguments.&lt;br /&gt;
&lt;br /&gt;
By default, &amp;lt;tt&amp;gt;number_format( )&amp;lt;/tt&amp;gt; rounds the number to the nearest integer. If you want to preserve the entire number, but you don't know ahead of time how many digits follow the decimal point in your number, use this:&lt;br /&gt;
&lt;br /&gt;
 $number = 1234.56; // your number&lt;br /&gt;
 list($int, $dec) = explode('.', $number);&lt;br /&gt;
 print number_format($number, strlen($dec));&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;number_format( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/number-format''.&lt;br /&gt;
&lt;br /&gt;
== Printing Correct Plurals ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to correctly pluralize words based on the value of a variable. For instance, you are returning text that depends on the number of matches found by a search.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use a conditional expression:&lt;br /&gt;
&lt;br /&gt;
 $number = 4;&lt;br /&gt;
 print &amp;quot;Your search returned $number &amp;quot; . ($number == 1 ? 'hit' : 'hits') . '.';&lt;br /&gt;
 '''Your search returned 4 hits'''.&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
It's slightly shorter to write the line as:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;print &amp;quot;Your search returned $number hit&amp;quot; . ($number == 1 ? '' : 's') . '.';&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, for odd pluralizations, such as &amp;quot;person&amp;quot; versus &amp;quot;people,&amp;quot; we find it clearer to break out the entire word rather than just the letter.&lt;br /&gt;
&lt;br /&gt;
Another option is to use one function for all pluralization, as shown in the &amp;lt;tt&amp;gt;pc_may_pluralize( )&amp;lt;/tt&amp;gt; function in [[PHP Cookbook/Numbers#phpckbk-CHP-2-EX-2|Example 2-2]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-2-EX-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 2-2. pc_may_pluralize( )'''&lt;br /&gt;
&lt;br /&gt;
 function pc_may_pluralize($singular_word, $amount_of) {&lt;br /&gt;
 &lt;br /&gt;
     // array of special plurals&lt;br /&gt;
     $plurals = array(&lt;br /&gt;
         'fish' =&amp;gt; 'fish',&lt;br /&gt;
         'person' =&amp;gt; 'people',&lt;br /&gt;
     );&lt;br /&gt;
 &lt;br /&gt;
     // only one&lt;br /&gt;
     if (1 == $amount_of) {&lt;br /&gt;
         return $singular_word;&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     // more than one, special plural&lt;br /&gt;
     if (isset($plurals[$singular_word])) {&lt;br /&gt;
         return $plurals[$singular_word];&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     // more than one, standard plural: add 's' to end of word&lt;br /&gt;
     return $singular_word . 's';&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here are some examples:&lt;br /&gt;
&lt;br /&gt;
 $number_of_fish = 1;&lt;br /&gt;
 print &amp;quot;I ate $number_of_fish &amp;quot; . pc_may_pluralize('fish', $number_of_fish) . '.';&lt;br /&gt;
 &lt;br /&gt;
 $number_of_people = 4; &lt;br /&gt;
 print 'Soylent Green is ' . pc_may_pluralize('person', $number_of_people) . '!';&lt;br /&gt;
 '''I ate 1 fish.'''&lt;br /&gt;
                '''Soylent Green is people!'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
If you plan to have multiple plurals inside your code, using a function such as &amp;lt;tt&amp;gt;pc_may_pluralize( )&amp;lt;/tt&amp;gt; increases readability. To use the function, pass &amp;lt;tt&amp;gt;pc_may_pluralize( )&amp;lt;/tt&amp;gt; the singular form of the word as the first argument and the amount as the second. Inside the function, there's a large array, &amp;lt;tt&amp;gt;$plurals&amp;lt;/tt&amp;gt;, that holds all the special cases. If the &amp;lt;tt&amp;gt;$amount&amp;lt;/tt&amp;gt; is &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt;, you return the original word. If it's greater, you return the special pluralized word, if it exists. As a default, just add an &amp;quot;s&amp;quot; to the end of the word.&lt;br /&gt;
&lt;br /&gt;
== Calculating Trigonometric Functions ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to use trigonometric functions, such as sine, cosine, and tangent.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
PHP supports many trigonometric functions natively: &amp;lt;tt&amp;gt;sin( )&amp;lt;/tt&amp;gt; , &amp;lt;tt&amp;gt;cos( )&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;tan( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 $cos = cos(2.1232);&lt;br /&gt;
&lt;br /&gt;
You can also use their inverses: &amp;lt;tt&amp;gt;asin( )&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;acos( )&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;atan( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 $atan = atan(1.2);&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
These functions assume their arguments are in radians, not degrees. (See [[PHP Cookbook/Numbers#Doing Trigonometry in Degrees, not Radians|Recipe 2.13]] if this is a problem.)&lt;br /&gt;
&lt;br /&gt;
The function &amp;lt;tt&amp;gt;atan2( )&amp;lt;/tt&amp;gt; takes two variables &amp;lt;tt&amp;gt;$x&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;$y&amp;lt;/tt&amp;gt;, and computes &amp;lt;tt&amp;gt;atan($x/$y)&amp;lt;/tt&amp;gt;. However, it always returns the correct sign because it uses both parameters when finding the quadrant of the result.&lt;br /&gt;
&lt;br /&gt;
For secant, cosecant, and cotangent, you should manually calculate the reciprocal values of &amp;lt;tt&amp;gt;sin( )&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;cos( )&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;tan( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 $n = .707;&lt;br /&gt;
 &lt;br /&gt;
 $secant    = 1 / sin($n);&lt;br /&gt;
 $cosecant  = 1 / cos($n);&lt;br /&gt;
 $cotangent = 1 / tan($n);&lt;br /&gt;
&lt;br /&gt;
Starting in PHP 4.1, you can also use hyperbolic functions: &amp;lt;tt&amp;gt;sinh( )&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;cosh( )&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;tanh( )&amp;lt;/tt&amp;gt;, plus, of course, &amp;lt;tt&amp;gt;asin( )&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;cosh( )&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;atanh( )&amp;lt;/tt&amp;gt;. The inverse functions, however, aren't supported on Windows.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Numbers#Doing Trigonometry in Degrees, not Radians|Recipe 2.13]] for how to perform trig operations in degrees, not radians; documentation on &amp;lt;tt&amp;gt;sin( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/sin'', &amp;lt;tt&amp;gt;cos( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/cos'', &amp;lt;tt&amp;gt;tan( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/tan'', &amp;lt;tt&amp;gt;asin( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/asin'', &amp;lt;tt&amp;gt;acos( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/acos'', &amp;lt;tt&amp;gt;atan( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/atan'', and &amp;lt;tt&amp;gt;atan2( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/atan2''.&lt;br /&gt;
&lt;br /&gt;
== Doing Trigonometry in Degrees, not Radians ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You have numbers in degrees but want to use the trigonometric functions.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;deg2rad( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;rad2deg( )&amp;lt;/tt&amp;gt; on your input and output:&lt;br /&gt;
&lt;br /&gt;
 $cosine = rad2deg(cos(deg2rad($degree)));&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
By definition, 360 degrees is equal to 2π radians, so it's easy to manually convert between the two formats. However, these functions use PHP's internal value of π, so you're assured a high-precision answer. To access this number for other calculations, use the constant &amp;lt;tt&amp;gt;M_PI&amp;lt;/tt&amp;gt;, which is 3.14159265358979323846.&lt;br /&gt;
&lt;br /&gt;
There is no built-in support for gradians. This is considered a feature, not a bug.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Numbers#Doing Trigonometry in Degrees, not Radians|Recipe 2.13]] for trig basics; documentation on &amp;lt;tt&amp;gt;deg2rad( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/deg2rad'' and &amp;lt;tt&amp;gt;rad2deg( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/rad2deg''.&lt;br /&gt;
&lt;br /&gt;
== Handling Very Large or Very Small Numbers ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You need to use numbers that are too large (or small) for PHP's built-in floating-point numbers.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use either the BCMath or GMP libraries.&lt;br /&gt;
&lt;br /&gt;
Using BCMath:&lt;br /&gt;
&lt;br /&gt;
 $sum = bcadd('1234567812345678', '8765432187654321');&lt;br /&gt;
 &lt;br /&gt;
 // $sum is now the string '9999999999999999'&lt;br /&gt;
 print $sum;&lt;br /&gt;
&lt;br /&gt;
Using GMP:&lt;br /&gt;
&lt;br /&gt;
 $sum = gmp_add('1234567812345678', '8765432187654321');&lt;br /&gt;
 &lt;br /&gt;
 // $sum is now a GMP resource, not a string; use gmp_strval( ) to convert&lt;br /&gt;
 print gmp_strval($sum);&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
The BCMath library is easy to use. You pass in your numbers as strings, and the function return the sum (or difference, product, etc.) as a string. However, the range of actions you can apply to numbers using BCMath is limited to basic arithmetic.&lt;br /&gt;
&lt;br /&gt;
The GMP library is available as of PHP 4.0.4. While most members of the GMP family of functions accept integers and strings as arguments, they prefer to pass numbers around as resources, which are essentially pointers to the numbers. So, unlike BCMath functions, which return strings, GMP functions return only resources. You then pass the resource to any GMP function, and it acts as your number.&lt;br /&gt;
&lt;br /&gt;
The only downside is when you want to view or use the resource with a non-GMP function, you need to explicitly convert it using &amp;lt;tt&amp;gt;gmp_strval( )&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;gmp_intval( )&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
GMP functions are liberal in what they accept. For instance:&lt;br /&gt;
&lt;br /&gt;
 $four = gmp_add(2, 2);            // You can pass integers&lt;br /&gt;
 $eight = gmp_add('4', '4');       // Or strings&lt;br /&gt;
 $twelve = gmp_add($four, $eight); // Or GMP resources&lt;br /&gt;
 print gmp_strval($twelve);        // Prints 12&lt;br /&gt;
&lt;br /&gt;
However, you can do many more things with GMP numbers than addition, such as raising a number to a power, computing large factorials very quickly, finding a greatest common divisor (GCD), and other fancy mathematical stuff:&lt;br /&gt;
&lt;br /&gt;
 // Raising a number to a power&lt;br /&gt;
 $pow = gmp_pow(2, 10);             // 1024&lt;br /&gt;
 &lt;br /&gt;
 // Computing large factorials very quickly&lt;br /&gt;
 $factorial = gmp_fact(20);         // 2432902008176640000&lt;br /&gt;
 &lt;br /&gt;
 // Finding a GCD&lt;br /&gt;
 $gcd = gmp_gcd (123, 456);         // 3&lt;br /&gt;
 &lt;br /&gt;
 // Other fancy mathematical stuff&lt;br /&gt;
 $legdendre = gmp_legendre(1, 7);   // 1&lt;br /&gt;
&lt;br /&gt;
The BCMath and GMP libraries aren't necessarily enabled with all PHP configurations. As of PHP 4.0.4, BCMath is bundled with PHP, so it's likely to be available. However, GMP isn't bundled with PHP, so you'll need to download, install it, and instruct PHP to use it during the configuration process. Check the values of &amp;lt;tt&amp;gt;function_defined('bcadd')&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;function_defined('gmp_init')&amp;lt;/tt&amp;gt; to see if you can use BCMath and GMP.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on BCMath at ''http://www.php.net/bc'' and GMP at ''http://www.php.net/gmp''.&lt;br /&gt;
&lt;br /&gt;
== Converting Between Bases ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You need to convert a number from one base to another.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use the &amp;lt;tt&amp;gt;base_convert( )&amp;lt;/tt&amp;gt; function:&lt;br /&gt;
&lt;br /&gt;
 $hex = 'a1';                           // hexadecimal number (base 16)&lt;br /&gt;
 &lt;br /&gt;
 // convert from base 16 to base 10&lt;br /&gt;
 $decimal = base_convert($hex, 16, 10); // $decimal is now 161&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;base_convert( )&amp;lt;/tt&amp;gt; function changes a string in one base to the correct string in another. It works for all bases from 2 to 36 inclusive, using the letters &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; through &amp;lt;tt&amp;gt;z&amp;lt;/tt&amp;gt; as additional symbols for bases above 10. The first argument is the number to be converted, followed by the base it is in and the base you want it to become.&lt;br /&gt;
&lt;br /&gt;
There are also a few specialized functions for conversions to and from base 10 and the most commonly used other bases of 2, 8, and 16. They're &amp;lt;tt&amp;gt;bindec( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;decbin( )&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;octdec( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;decoct( )&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;hexdec( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;dechex( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 // convert to base 10&lt;br /&gt;
 print bindec(11011); // 27&lt;br /&gt;
 print octdec(33);    // 27&lt;br /&gt;
 print hexdec('1b');  // 27&lt;br /&gt;
 &lt;br /&gt;
 // convert from base 10&lt;br /&gt;
 print decbin(27);    // 11011&lt;br /&gt;
 print decoct(27);    // 33&lt;br /&gt;
 print dechex(27);    // 1b&lt;br /&gt;
&lt;br /&gt;
Another alternative is to use &amp;lt;tt&amp;gt;sprintf( )&amp;lt;/tt&amp;gt; , which allows you to convert decimal numbers to binary, octal, and hexadecimal numbers with a wide range of formatting, such as leading 0s and a choice between upper- and lowercase letters for hexadecimal numbers.&lt;br /&gt;
&lt;br /&gt;
For instance, say you want to print out HTML color values:&lt;br /&gt;
&lt;br /&gt;
 printf('#%02X%02X%02X', 0, 102, 204); // #0066CC&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;base_convert( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/base-convert'' and &amp;lt;tt&amp;gt;sprintf( )&amp;lt;/tt&amp;gt; formatting options at ''http://www.php.net/sprintf''.&lt;br /&gt;
&lt;br /&gt;
== Calculating Using Numbers in Bases Other Than Decimal ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to perform mathematical operations with numbers formatted not in decimal, but in octal or hexadecimal. For example, you want to calculate web-safe colors in hexadecimal.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Prefix the number with a leading symbol, so PHP knows it isn't in base 10. The following values are all equal:&lt;br /&gt;
&lt;br /&gt;
 0144  // base 8&lt;br /&gt;
  100  // base 10 &lt;br /&gt;
 0x64  // base 16&lt;br /&gt;
&lt;br /&gt;
Here's how to count from decimal 1 to 15 using hexadecimal notation:&lt;br /&gt;
&lt;br /&gt;
 for ($i = 0x1; $i &amp;lt; 0x10; $i++) { print &amp;quot;$i\n&amp;quot;; }&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Even if you use hexadecimally formatted numbers in a &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop, by default, all numbers are printed in decimal. In other words, the code in the Solution doesn't print out &amp;quot;..., 8, 9, a, b, ...&amp;quot;. To print in hexadecimal, use one of the methods listed in [[PHP Cookbook/Numbers#Converting Between Bases|Recipe 2.15]]. Here's an example:&lt;br /&gt;
&lt;br /&gt;
 for ($i = 0x1; $i &amp;lt; 0x10; $i++) { print dechex($i) . &amp;quot;\n&amp;quot;; }&lt;br /&gt;
&lt;br /&gt;
For most calculations, it's easier to use decimal. Sometimes, however, it's more logical to switch to another base, for example, when using the 216 web-safe colors. Every web color code is of the form &amp;lt;tt&amp;gt;''RRGGBB''&amp;lt;/tt&amp;gt;, where &amp;lt;tt&amp;gt;''RR''&amp;lt;/tt&amp;gt; is the red color, &amp;lt;tt&amp;gt;''GG''&amp;lt;/tt&amp;gt; is the green color, and &amp;lt;tt&amp;gt;''BB''&amp;lt;/tt&amp;gt; is the blue color. Each color is actually a two-digit hexadecimal number between 0 and FF.&lt;br /&gt;
&lt;br /&gt;
What makes web-safe colors special is that &amp;lt;tt&amp;gt;''RR''&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;''GG''&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;''BB''&amp;lt;/tt&amp;gt; each must be one of the following six numbers: 00, 33, 66, 99, CC, and FF (in decimal: 0, 51, 102, 153, 204, 255). So, 003366 is web-safe, but 112233 is not. Web-safe colors render without dithering on a 256-color display.&lt;br /&gt;
&lt;br /&gt;
When creating a list of these numbers, use hexadecimal notation in this triple-loop to reinforce the list's hexadecimal basis:&lt;br /&gt;
&lt;br /&gt;
 for ($rr = 0; $rr &amp;lt;= 0xFF; $rr += 0x33)&lt;br /&gt;
     for ($gg = 0; $gg &amp;lt;= 0xFF; $gg += 0x33)&lt;br /&gt;
         for ($bb = 0; $bb &amp;lt;= 0xFF; $bb += 0x33)&lt;br /&gt;
             printf(&amp;quot;%02X%02X%02X\n&amp;quot;, $rr, $gg, $bb);&lt;br /&gt;
&lt;br /&gt;
Here the loops compute all possible web-safe colors. However, instead of stepping through them in decimal, you use hexadecimal notation, because it reinforces the hexadecimal link between the numbers. Print them out using &amp;lt;tt&amp;gt;printf( )&amp;lt;/tt&amp;gt; to format them as uppercase hexadecimal numbers at least two digits long. One-digit numbers are passed with a leading zero.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Numbers#Converting Between Bases|Recipe 2.15]] for details on converting between bases; Chapter 3, &amp;quot;Web Design Principles for Print Designers,&amp;quot; in ''Web Design in a Nutshell'' (O'Reilly).&lt;br /&gt;
== Notes ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</description>
			<pubDate>Thu, 06 Mar 2008 22:28:45 GMT</pubDate>			<dc:creator>Docbook2Wiki</dc:creator>			<comments>http://commons.oreilly.com/wiki/index.php/Talk:PHP_Cookbook/Numbers</comments>		</item>
	</channel>
</rss>