Reputation: 1331
It's like assert isn't even being called. I am confused.
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:
<?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?
Upvotes: 18
Views: 6987
Reputation: 5341
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.
Upvotes: 0
Reputation: 1331
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
Upvotes: 26