【问题标题】:Assert is not working in PHP. So simple. What am I doing wrong?断言在 PHP 中不起作用。很简单。我究竟做错了什么?
【发布时间】:2016-10-19 11:18:06
【问题描述】:

就像 assert 甚至没有被调用一样。我很困惑。

版本

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

脚本:

x.php

<?php
    print ("Hello\n");

    assert_options(ASSERT_ACTIVE,true);
    assert_options(ASSERT_BAIL,true);

    assert(false);
    assert(true);

    print ("Bye\n");

当我运行它时

php x.php

Hello
Bye

我原以为程序会因异常而终止。我要疯了吗?

【问题讨论】:

  • 在 PHP7.0.12 我得到一个Warning: assert(): assert(false) failed
  • 断言在 PHP7 检查 the manual987654321@ 中有所改变
  • 我升级到 7.0.12 但输出是一样的。 codephp -v PHP 7.0.12-1+deb.sury.org~xenial+1 (cli) ( NTS ) 版权所有 (c) 1997-2016 The PHP Group 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, by Zend Technologies :~/code/x/test$ php x.php Hello Bye跨度>
  • 然后检查您在php.ini中设置的参数(手册中都提到过)

标签: php assert


【解决方案1】:

看起来断言在 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

脚本现在按预期工作。

php x.php

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

【讨论】:

  • 如果需要查看异常,请包含 assert.exception1(在 php 7 中引入)。
猜你喜欢
  • 2012-10-25
  • 2016-05-27
  • 2019-05-26
  • 2017-04-10
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多