Constants

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@312207 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Jakub Vrana 2011-06-16 12:55:11 +00:00
parent faa96c62bf
commit 0da36c5a16
4 changed files with 49 additions and 15 deletions

View file

@ -32,6 +32,17 @@
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<constant>JSON_ERROR_STATE_MISMATCH</constant>
(<type>integer</type>)
</term>
<listitem>
<simpara>
Occurs with underflow or with the modes mismatch.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<constant>JSON_ERROR_CTRL_CHAR</constant>
@ -131,6 +142,18 @@
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<constant>JSON_NUMERIC_CHECK</constant>
(<type>integer</type>)
</term>
<listitem>
<simpara>
Encodes numeric strings as numbers.
Available since PHP 5.3.3.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</appendix>

View file

@ -185,17 +185,18 @@ $json = json_encode(
);
// Define the errors.
$json_errors = array(
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
);
$constants = get_defined_constants(true);
$json_errors = array();
foreach ($constants["json"] as $name => $value) {
if (!strncmp($name, "JSON_ERROR_", 11)) {
$json_errors[$value] = $name;
}
}
// Show the errors for different depths.
foreach(range(4, 3, -1) as $depth) {
var_dump(json_decode($json, True, $depth));
echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
echo 'Last error: ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
}
?>
]]>
@ -222,10 +223,10 @@ array(1) {
}
}
}
Last error : No error has occurred
Last error: JSON_ERROR_NONE
NULL
Last error : The maximum stack depth has been exceeded
Last error: JSON_ERROR_DEPTH
]]>
</screen>
</example>

View file

@ -43,6 +43,7 @@
<constant>JSON_HEX_TAG</constant>,
<constant>JSON_HEX_AMP</constant>,
<constant>JSON_HEX_APOS</constant>,
<constant>JSON_NUMERIC_CHECK</constant>,
<constant>JSON_FORCE_OBJECT</constant>.
</para>
</listitem>

View file

@ -50,13 +50,13 @@
<entry></entry>
</row>
<row>
<entry><constant>JSON_ERROR_CTRL_CHAR</constant></entry>
<entry>Control character error, possibly incorrectly encoded</entry>
<entry><constant>JSON_ERROR_STATE_MISMATCH</constant></entry>
<entry>Invalid or malformed JSON</entry>
<entry></entry>
</row>
<row>
<entry><constant>JSON_ERROR_STATE_MISMATCH</constant></entry>
<entry>Invalid or malformed JSON</entry>
<entry><constant>JSON_ERROR_CTRL_CHAR</constant></entry>
<entry>Control character error, possibly incorrectly encoded</entry>
<entry></entry>
</row>
<row>
@ -97,17 +97,26 @@ foreach($json as $string)
switch(json_last_error())
{
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_NONE:
echo ' - No errors';
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}