assert

(PHP 4, PHP 5, PHP 7)

assert锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷欠锟轿� FALSE

说锟斤拷

PHP 5

assert ( mixed $assertion [, string $description ] ) : bool

PHP 7

assert ( mixed $assertion [, Throwable $exception ] ) : bool

assert() 锟斤拷锟斤拷指锟斤拷锟斤拷 assertion 锟斤拷锟节斤拷锟轿� FALSE 时锟斤拷取锟绞碉拷锟斤拷锟叫讹拷锟斤拷

Traditional assertions (PHP 5 and 7)

锟斤拷锟� assertion 锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟结被 assert() 锟斤拷锟斤拷 PHP 锟斤拷锟斤拷锟斤拷执锟叫★拷 assertion 锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟角碉拷锟斤拷锟矫讹拷锟斤拷时锟斤拷锟侥匡拷锟斤拷锟斤拷锟叫★拷锟斤拷锟斤拷锟斤拷诙锟斤拷锟绞э拷锟绞憋拷锟较拷锟斤拷锟斤拷 assertion 锟斤拷锟绞斤拷锟� 锟斤拷锟斤拷味锟斤拷锟斤拷锟斤拷愦拷锟斤拷锟� boolean 锟斤拷锟斤拷锟斤拷锟斤拷为 assertion锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞疚拷锟斤拷院锟斤拷锟斤拷牟锟斤拷锟斤拷锟斤拷诘锟斤拷锟斤拷愣拷锟斤拷 assert_options() 锟斤拷锟斤拷锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷转锟斤拷为锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值 FALSE 锟结被转锟斤拷锟缴匡拷锟街凤拷锟斤拷锟斤拷

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟接︼拷锟街伙拷锟斤拷锟斤拷锟斤拷锟斤拷浴锟� 锟斤拷应锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟皆硷拷锟绞憋拷锟斤拷锟斤拷锟斤拷锟斤拷欠锟绞硷拷锟接︼拷锟轿� TRUE锟斤拷锟斤拷指示某些锟斤拷锟斤拷锟斤拷螅锟斤拷呒锟斤拷锟斤拷骞︼拷艿拇锟斤拷冢锟斤拷锟斤拷锟斤拷锟秸癸拷锟斤拷锟斤拷锟斤拷囟锟斤拷锟较低筹拷锟斤拷坪凸锟斤拷埽锟斤拷锟�

锟斤拷锟皆诧拷应锟斤拷锟斤拷锟斤拷锟斤拷通锟斤拷锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷募锟介。 锟斤拷为一锟斤拷锟斤拷锟介法锟斤拷锟节讹拷锟皆斤拷锟斤拷时锟斤拷拇锟斤拷锟揭灿︼拷锟斤拷芄锟斤拷锟饺凤拷锟斤拷锟斤拷小锟�

assert() 锟斤拷锟斤拷为锟斤拷锟斤拷通锟斤拷 assert_options() 锟斤拷锟斤拷锟矫o拷锟斤拷锟斤拷锟街诧拷页锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 .ini 锟斤拷锟矫★拷

assert_options() ASSERT_CALLBACK 锟斤拷锟斤拷指锟斤拷锟斤拷锟斤拷锟斤拷锟矫回碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷失锟杰的讹拷锟皆★拷

assert() 锟截碉拷锟斤拷锟斤拷锟节癸拷锟斤拷锟皆讹拷锟斤拷锟斤拷锟阶硷拷锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟矫o拷锟斤拷为锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷椎夭锟斤拷锟斤拷锟斤拷锟皆的达拷锟诫,锟斤拷锟斤拷锟斤拷锟斤拷锟皆碉拷位锟斤拷锟斤拷息锟斤拷 锟斤拷锟斤拷息锟杰癸拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷使锟矫讹拷锟皆匡拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷悖�

锟截碉拷锟斤拷锟斤拷应锟矫斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟剿讹拷锟斤拷失锟斤拷锟斤拷锟节碉拷锟侥硷拷锟斤拷 锟节讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟剿讹拷锟斤拷失锟斤拷锟斤拷锟节碉拷锟叫号o拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷失锟杰的憋拷锟绞斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� — 锟斤拷锟斤拷值锟斤拷锟斤拷 1 锟斤拷锟斤拷 "two" 锟斤拷锟斤拷锟结传锟捷碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� PHP 5.4.8 锟斤拷锟斤拷锟竭版本锟斤拷锟矫伙拷也锟斤拷锟斤拷锟结供锟斤拷锟侥革拷锟斤拷选锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷耍锟斤拷锟斤拷诮锟� description 指锟斤拷锟斤拷 assert()锟斤拷

Expectations (PHP 7 only)

assert() is a language construct in PHP 7, allowing for the definition of expectations: assertions that take effect in development and testing environments, but are optimised away to have zero cost in production.

While assert_options() can still be used to control behaviour as described above for backward compatibility reasons, PHP 7 only code should use the two new configuration directives to control the behaviour of assert() and not call assert_options().

PHP 7 configuration directives for assert()
Directive Default value Possible values
zend.assertions 1
  • 1: generate and execute code (development mode)
  • 0: generate code but jump around it at runtime
  • -1: do not generate code (production mode)
assert.exception 0
  • 1: throw when the assertion fails, either by throwing the object provided as the exception or by throwing a new AssertionError object if exception wasn't provided
  • 0: use or generate a Throwable as described above, but only generate a warning based on that object rather than throwing it (compatible with PHP 5 behaviour)

锟斤拷锟斤拷

assertion

锟斤拷锟皆★拷In PHP 5, this must be either a string to be evaluated or a boolean to be tested. In PHP 7, this may also be any expression that returns a value, which will be executed and the result used to indicate whether the assertion succeeded or failed.

description

锟斤拷锟� assertion 失锟斤拷锟剿o拷选锟斤拷 description 锟斤拷锟斤拷锟斤拷锟斤拷锟绞э拷锟斤拷锟较拷铩�

exception

In PHP 7, the second parameter can be a Throwable object instead of a descriptive string, in which case this is the object that will be thrown if the assertion fails and the assert.exception configuration directive is enabled.

锟斤拷锟斤拷值

assertion 锟斤拷 false 锟津返伙拷 FALSE锟斤拷锟斤拷锟斤拷锟斤拷 TRUE锟斤拷

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
7.0.0 assert() is now a language construct and not a function. assertion() can now be an expression. The second parameter is now interpreted either as an exception (if a Throwable object is given), or as the description supported from PHP 5.4.8 onwards.
5.4.8 锟斤拷锟斤拷锟剿诧拷锟斤拷 description锟斤拷 description 锟斤拷锟斤拷也锟斤拷为锟斤拷锟侥革拷锟斤拷锟斤拷锟结供锟斤拷 ASSERT_CALLBACK 模式锟斤拷幕氐锟斤拷锟斤拷锟斤拷锟�

锟斤拷锟斤拷

Traditional assertions (PHP 5 and 7)

Example #1 使锟斤拷锟皆讹拷锟藉处锟斤拷锟斤拷锟斤拷锟绞э拷艿亩锟斤拷锟�

<?php
// 锟斤拷锟斤拷锟斤拷裕锟斤拷锟斤拷锟斤拷锟斤拷锟轿� quiet
assert_options(ASSERT_ACTIVE1);
assert_options(ASSERT_WARNING0);
assert_options(ASSERT_QUIET_EVAL1);

//锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
function my_assert_handler($file$line$code)
{
    echo 
"<hr>Assertion Failed:
        File '
$file'<br />
        Line '
$line'<br />
        Code '
$code'<br /><hr />";
}

// 锟斤拷锟矫回碉拷锟斤拷锟斤拷
assert_options(ASSERT_CALLBACK'my_assert_handler');

// 锟斤拷一锟斤拷锟斤拷锟绞э拷锟�
assert('mysql_query("")');
?>

Example #2 使锟斤拷锟皆讹拷锟藉处锟斤拷锟斤拷锟斤拷印锟斤拷锟斤拷锟斤拷息

<?php
// 锟斤拷锟斤拷锟斤拷裕锟斤拷锟斤拷锟斤拷锟斤拷锟轿� quiet
assert_options(ASSERT_ACTIVE1);
assert_options(ASSERT_WARNING0);
assert_options(ASSERT_QUIET_EVAL1);

//锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
function my_assert_handler($file$line$code$desc null)
{
    echo 
"Assertion failed at $file:$line$code";
    if (
$desc) {
        echo 
": $desc";
    }
    echo 
"\n";
}

// 锟斤拷锟矫回碉拷锟斤拷锟斤拷
assert_options(ASSERT_CALLBACK'my_assert_handler');

// Make an assertion that should fail
assert('2 < 1');
assert('2 < 1''Two is less than one');
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

Assertion failed at test.php:21: 2 < 1
Assertion failed at test.php:22: 2 < 1: Two is less than one

Expectations (PHP 7 only)

Example #3 Expectations without a custom exception

<?php
assert
(true == false);
echo 
'Hi!';
?>

With zend.assertions set to 0, the above example will output:

Hi!

With zend.assertions set to 1 and assert.exception set to 0, the above example will output:

Warning: assert(): assert(true == false) failed in - on line 2
Hi!

With zend.assertions set to 1 and assert.exception set to 1, the above example will output:

Fatal error: Uncaught AssertionError: assert(true == false) in -:2
Stack trace:
#0 -(2): assert(false, 'assert(true == ...')
#1 {main}
  thrown in - on line 2

Example #4 Expectations with a custom exception

<?php
class CustomError extends AssertionError {}

assert(true == false, new CustomError('True is not false!'));
echo 
'Hi!';
?>

With zend.assertions set to 0, the above example will output:

Hi!

With zend.assertions set to 1 and assert.exception set to 0, the above example will output:

Warning: assert(): CustomError: True is not false! in -:4
Stack trace:
#0 {main} failed in - on line 4
Hi!

With zend.assertions set to 1 and assert.exception set to 1, the above example will output:

Fatal error: Uncaught CustomError: True is not false! in -:4
Stack trace:
#0 {main}
  thrown in - on line 4

锟轿硷拷

  • assert_options() - 锟斤拷锟斤拷/锟斤拷取锟斤拷锟皆的革拷锟街憋拷志