断言不在php中工作.很简单.我究竟做错了什么? [英] Assert not working in php. So simple. What am I doing wrong?

查看:84
本文介绍了断言不在php中工作.很简单.我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像断言甚至没有被调用一样.我很困惑.

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

版本

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

当我运行它

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

我希望程序会异常终止.我要疯了吗?

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

推荐答案

谢谢RiggsFolly.

Thank you RiggsFolly.

类似断言的外观在7.0上是开箱即用的.在我的php.ini文件中,zend.assertions设置为-1,这意味着它们将被忽略.我已将设置更改为1.

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

这篇关于断言不在php中工作.很简单.我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆