(PHP 4, PHP 5, PHP 7)
assert — ���һ�������Ƿ�Ϊ FALSE
PHP 5
PHP 7
assert() ����ָ����
assertion
���ڽ��Ϊ FALSE
ʱ��ȡ�ʵ����ж���
��� assertion
���ַ����������ᱻ assert() ���� PHP ������ִ�С�
assertion
���ַ����������ǵ����ö���ʱ���Ŀ������С�������ڶ���ʧ��ʱ��Ϣ����� assertion
���ʽ��
����ζ������㴫���� boolean ��������Ϊ assertion
�����������������ʾΪ���Ժ����IJ������ڵ����㶨��� assert_options() ������ʱ��������ת��Ϊ�ַ�����������ֵ FALSE
�ᱻת���ɿ��ַ�����
�����������Ӧ��ֻ���������ԡ�
��Ӧ�����������Լ��ʱ���������Ƿ�ʼ��Ӧ��Ϊ TRUE
����ָʾijЩ������������幦�ܵĴ��ڣ�������չ�������ض���ϵͳ���ƺ��ܣ���
���Բ�Ӧ��������ͨ����ʱ������������������ļ�顣 ��Ϊһ�����鷨���ڶ��Խ���ʱ��Ĵ���ҲӦ���ܹ���ȷ�����С�
assert() ����Ϊ����ͨ�� assert_options() �����ã������ֲ�ҳ���������� .ini ���á�
assert_options() ASSERT_CALLBACK
����ָ���������ûص�����������ʧ�ܵĶ��ԡ�
assert() �ص������ڹ����Զ���������ʱ���������ã���Ϊ������������ز�������ԵĴ��룬���������Ե�λ����Ϣ�� ����Ϣ�ܹ���������������ʹ�ö��Կ���������������㣡
�ص�����Ӧ�ý�������������
��һ�����������˶���ʧ�����ڵ��ļ���
�ڶ������������˶���ʧ�����ڵ��кţ�����������������ʧ�ܵı��ʽ���������� — ����ֵ���� 1 ���� "two" �����ᴫ�ݵ������������
PHP 5.4.8 �����߰汾���û�Ҳ�����ṩ���ĸ���ѡ��������������ˣ����ڽ� description
ָ���� assert()��
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().
Directive | Default value | Possible values |
---|---|---|
zend.assertions | 1 |
|
assert.exception | 0 |
|
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
ʧ���ˣ�ѡ�� 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 ģʽ��Ļص�������
|
Example #1 ʹ���Զ��崦�������ʧ�ܵĶ���
<?php
// ������ԣ���������Ϊ quiet
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
//����������
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_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
//����������
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
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