Assert not working in php. So simple. What am I doing wrong?

那年仲夏 提交于 2020-05-11 04:53:18

问题


It's like assert isn't even being called. I am confused.

the version

:~/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

the script

:~/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");

when I run it

:~/code/x/test$ php x.php
Hello
Bye

I would have expected the program to terminate with an exception. Am I going crazy?


回答1:


Thank you RiggsFolly.

Looks like Assertions are OFF out of the box on 7.0. In my php.ini file zend.assertions was set to -1, which means they are ignored. I have changed the setting to 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

The script now works as expected.

:~/code/x/test$ php x.php
Hello
PHP Warning:  assert(): assert(false) failed in /home/ubuntu/code/x/test/x.php on line 8


来源:https://stackoverflow.com/questions/40129963/assert-not-working-in-php-so-simple-what-am-i-doing-wrong

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!