断言在php中不起作用。这么简单。我做错了什么?
好像assert甚至都没有被调用。我很困惑 版本断言在php中不起作用。这么简单。我做错了什么?,php,assert,Php,Assert,好像assert甚至都没有被调用。我很困惑 版本 :~/code/x/test$ php -v PHP 7.0.11-1+deb.sury.org~xenial+1 (cli) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.11-1+deb.sury.org~xenial+1
:~/code/x/test$ php -v
PHP 7.0.11-1+deb.sury.org~xenial+1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.11-1+deb.sury.org~xenial+1, Copyright (c) 1999-2016, by Zend Technologies
剧本
:~/code/x/test$ cat x.php
<?php
print ("Hello\n");
assert_options(ASSERT_ACTIVE,true);
assert_options(ASSERT_BAIL,true);
assert(false);
assert(true);
print ("Bye\n");
我本以为程序会以异常终止。我疯了吗?谢谢你,真愚蠢
看起来断言在7.0上是现成的。在我的php.ini文件中,zend.assertions被设置为-1,这意味着它们被忽略。我已将设置更改为1
[Assertion]
; Switch whether to compile assertions at all (to have no overhead at run-time)
; -1: Do not compile at all
; 0: Jump over assertion at run-time
; 1: Execute assertions
; Changing from or to a negative value is only possible in php.ini! (For turning assertions on and off at run-time, see assert.active, when zend.assertions = 1)
; Default Value: 1
; Development Value: 1
; Production Value: -1
; http://php.net/zend.assertions
zend.assertions = 1
脚本现在按预期工作
:~/code/x/test$ php x.php
Hello
PHP Warning: assert(): assert(false) failed in /home/ubuntu/code/x/test/x.php on line 8
在PHP7.0.12中,我得到一个
警告:assert():assert(false)失败
assert在升级到7.0.12的PHP7检查中更改了一点,但输出是相同的code
php-v php 7.0.12-1+deb.sury.org~xenial+1(cli)(NTS)版权所有(c)1997-2016 php组Zend Engine v3.0.0,版权所有(c)1998-2016 Zend Technologies with Zend OPcache v7.0.12-1+deb.sury.org~xenial+1版权所有(c)1999-2016,由Zend Technologies提供:~/code/x/test$php x.php Hello ByeThen检查参数(手册中提到的所有内容)如果需要查看异常(在PHP7中介绍),您已经在php.ini
Includeassert.exception
中设置了1
。
:~/code/x/test$ php x.php
Hello
PHP Warning: assert(): assert(false) failed in /home/ubuntu/code/x/test/x.php on line 8