<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Check for numeric character(s)</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.ctype-cntrl.html">ctype_cntrl</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.ctype-graph.html">ctype_graph</a></div> <div class="up"><a href="ref.ctype.html">Ctype Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.ctype-digit" class="refentry"> <div class="refnamediv"> <h1 class="refname">ctype_digit</h1> <p class="verinfo">(PHP 4 >= 4.0.4, PHP 5)</p><p class="refpurpose"><span class="refname">ctype_digit</span> — <span class="dc-title">Check for numeric character(s)</span></p> </div> <div class="refsect1 description" id="refsect1-function.ctype-digit-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>ctype_digit</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$text</code></span> )</div> <p class="para rdfs-comment"> Checks if all of the characters in the provided <span class="type"><a href="language.types.string.html" class="type string">string</a></span>, <em><code class="parameter">text</code></em>, are numerical. </p> </div> <div class="refsect1 parameters" id="refsect1-function.ctype-digit-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">text</code></em></span> <dd> <p class="para"> The tested string. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.ctype-digit-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> if every character in the string <em><code class="parameter">text</code></em> is a decimal digit, <strong><code>FALSE</code></strong> otherwise. </p> </div> <div class="refsect1 changelog" id="refsect1-function.ctype-digit-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.1.0</td> <td> Before PHP 5.1.0, this function returned <strong><code>TRUE</code></strong> when <em><code class="parameter">text</code></em> was an empty string. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.ctype-digit-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-5074"> <p><strong>Example #1 A <span class="function"><strong>ctype_digit()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$strings </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'1820.20'</span><span style="color: #007700">, </span><span style="color: #DD0000">'10002'</span><span style="color: #007700">, </span><span style="color: #DD0000">'wsl!12'</span><span style="color: #007700">);<br />foreach (</span><span style="color: #0000BB">$strings </span><span style="color: #007700">as </span><span style="color: #0000BB">$testcase</span><span style="color: #007700">) {<br /> if (</span><span style="color: #0000BB">ctype_digit</span><span style="color: #007700">(</span><span style="color: #0000BB">$testcase</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"The string </span><span style="color: #0000BB">$testcase</span><span style="color: #DD0000"> consists of all digits.\n"</span><span style="color: #007700">;<br /> } else {<br /> echo </span><span style="color: #DD0000">"The string </span><span style="color: #0000BB">$testcase</span><span style="color: #DD0000"> does not consist of all digits.\n"</span><span style="color: #007700">;<br /> }<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> The string 1820.20 does not consist of all digits. The string 10002 consists of all digits. The string wsl!12 does not consist of all digits. </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5075"> <p><strong>Example #2 A <span class="function"><strong>ctype_digit()</strong></span> example comparing strings with integers</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$numeric_string </span><span style="color: #007700">= </span><span style="color: #DD0000">'42'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$integer </span><span style="color: #007700">= </span><span style="color: #0000BB">42</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">ctype_digit</span><span style="color: #007700">(</span><span style="color: #0000BB">$numeric_string</span><span style="color: #007700">); </span><span style="color: #FF8000">// true<br /></span><span style="color: #0000BB">ctype_digit</span><span style="color: #007700">(</span><span style="color: #0000BB">$integer</span><span style="color: #007700">); </span><span style="color: #FF8000">// false (ASCII 42 is the * character)<br /><br /></span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$numeric_string</span><span style="color: #007700">); </span><span style="color: #FF8000">// true<br /></span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$integer</span><span style="color: #007700">); </span><span style="color: #FF8000">// true<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.ctype-digit-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> This function expects a <span class="type"><a href="language.types.string.html" class="type string">string</a></span> to be useful, so for example passing in an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> may not return the expected result. However, also note that HTML forms will result in numeric strings and not integers. See also the <a href="language.types.html" class="link">types</a> section of the manual. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> If an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> between -128 and 255 inclusive is provided, it is interpreted as the ASCII value of a single character (negative values have 256 added in order to allow characters in the Extended ASCII range). Any other integer is interpreted as a string containing the decimal digits of the integer.</p></p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.ctype-digit-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.ctype-alnum.html" class="function" rel="rdfs-seeAlso">ctype_alnum()</a> - Check for alphanumeric character(s)</span></li> <li class="member"> <span class="function"><a href="function.ctype-xdigit.html" class="function" rel="rdfs-seeAlso">ctype_xdigit()</a> - Check for character(s) representing a hexadecimal digit</span></li> <li class="member"> <span class="function"><a href="function.is-numeric.html" class="function" rel="rdfs-seeAlso">is_numeric()</a> - Finds whether a variable is a number or a numeric string</span></li> <li class="member"> <span class="function"><a href="function.is-int.html" class="function" rel="rdfs-seeAlso">is_int()</a> - Find whether the type of a variable is integer</span></li> <li class="member"> <span class="function"><a href="function.is-string.html" class="function" rel="rdfs-seeAlso">is_string()</a> - Find whether the type of a variable is string</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.ctype-cntrl.html">ctype_cntrl</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.ctype-graph.html">ctype_graph</a></div> <div class="up"><a href="ref.ctype.html">Ctype Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>