Assert is not working in PHP. So simple. What am I doing wrong?
Asked Answered
A

2

18

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

The version

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:

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

php x.php

Hello
Bye

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

Arbalest answered 19/10, 2016 at 11:18 Comment(4)
In PHP7.0.12 I get a Warning: assert(): assert(false) failedFranciscka
Assert changed a bit in PHP7 check the manualFranciscka
I upgraded to 7.0.12 but the output is the same. codephp -v PHP 7.0.12-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.12-1+deb.sury.org~xenial+1, Copyright (c) 1999-2016, by Zend Technologies :~/code/x/test$ php x.php Hello ByeArbalest
Then check the parameter (all mentioned in the manual) you have set in your php.iniFranciscka
A
26

It 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.

php x.php

Hello
PHP Warning:  assert(): assert(false) failed in /home/ubuntu/code/x/test/x.php on line 8
Arbalest answered 19/10, 2016 at 11:42 Comment(1)
Include assert.exception to 1 if need to see an exception (Introduced in php 7).Wonderment
W
0

EVEN after turning a bunch of knobs in php.ini, I couldn't get PHP assertions to work. To get nice big loud noisy pretty error messages in PHP, use throw new Exception('message'); when a condition fails:

ini_set('error_reporting', E_ALL & ~E_NOTICE);
ini_set('display_errors', "1");
ini_set('html_errors', true);
@error_reporting(E_ALL & ~E_NOTICE);

if ( ! is_dir("./appfolder/")) throw new Exception('./appfolder/ does not exist!');

Of course, it should go without saying the reason PHP probably nuked assert was due to widespread misuse of it on production servers leaking all kinds of sensitive data. The widely perceived misconceptions about PHP being insecure stem from inappropriate use of code such as above on production deployments, so please ensure its removed or negated or disabled or whatever in production.

Wilsey answered 13/6 at 20:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.