Jump to content

Errors are preventing all output


Recommended Posts

I've recently upgraded to PHP 7 from PHP 5, but I've noticed now that warnings and notices are preventing other output from being displayed.  In PHP 5, if I tried to echo an undefined variable, I would get the notice in the midst of all the other output.  But now, in PHP 7 it seems to just output the notice and nothing else, although I do get several notices if I have the undefined variable in a loop.  This is an issue for me, because I often place echo statements around the code when I'm debugging to trace the flow of the code, but that doesn't work in PHP 7 anymore because none of my debugging statements are being printed to the screen.

I can make it work and adjust my debugging procedure by placing exit calls just before the line that's generating the error, but I'm still curious if there's a INI setting or something that can change this behavior?

Link to post
Share on other sites

Maybe it's an error reporting level. My code doesn't halt if I echo something that doesn't exist. If I rember correctly I think I had some similar issue until I change the error reporting level.
This is also a neat trick in php 7 like isset but without all the extra work

echo $var ?? 'default value';

 

 

Edited by ckrudelux
Added explanation
Link to post
Share on other sites
Posted (edited)

My code doesn't halt either.  As mentioned, I can get multiple notices, but I get no other output besides the error notices.  As an example:

echo "Hello World";
echo $undefinedVar1;
echo $undefinedVar2;

The above code, in PHP 5, would print the "Hello World" line and then the undefined variable errors (both of them).  In 7, I still get both error messages, but not the "Hello World" line.

EDIT: Oh, and my error reporting level is the same in both:
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT

Edited by ShadowMage
Link to post
Share on other sites

Hm, looked at my own files and noticed that I have this in the top. Does it solve the issue? I think I gave up trying to find what was wrong and just tried this and left it like that.

<?php error_reporting(E_ALL); ini_set("display_errors", "on");

Link to post
Share on other sites

How about

// Report all PHP errors
error_reporting(-1);

 

All

<?php

// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR E_WARNING E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)

error_reporting(E_ERROR E_WARNING E_PARSE E_NOTICE);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting'E_ALL);

?>
Edited by dsonesuk
Link to post
Share on other sites
  • 1 month later...
Posted (edited)

So, I'm bumping this back up because it's become an issue again.  I had been working around it using die() to print out variables and such just before any offending lines, but I've encountered a situation now where that doesn't work.  Consider the following:

$x = 10;
$y = 3;
for ($z=0; $z<10; $z++) {
	echo "Attempt $x / $y: ";
	echo ($x/$y)."<br />";
	$y--;
}

In PHP 5, I would get output as:

Attempt 10 / 3: 3.33333333
Attempt 10 / 2: 5
Attempt 10 / 1: 10
Attempt 10 / 0:
PHP Warning:  Division by zero in C:\inetpub\major\test.php on line 37

Now, in PHP 7, I just get the error.  And I can't use die() in the loop to print the "attempt..." line because the first iteration through the loop is correct and functional.  I really need to figure out how to make PHP output everything again like it used to.

While writing this, I thought of a workaround for the above situation.  I could put a conditional to check the value of $y like this:

$x = 10;
$y = 3;
for ($z=0; $z<10; $z++) {
	if ($y==0)
		echo "dividing by 0: $x / $y<br />";
	echo "Attempt $x / $y: ";
	echo ($x/$y)."<br />";
	$y--;
}

Or use a try/catch block for more complicated equations.  Maybe this is best practice anyway?  EDIT: Turns out a try/catch doesn't catch this error apparently... I don't know.  Still seems silly to me that PHP would prevent all other output in the event of a non-fatal error.

Edited by ShadowMage
Link to post
Share on other sites
Posted (edited)

This is really beginning to be a problem...

I've found all the division by 0 errors I had before, but I still have this guy:
PHP Warning:  A non-numeric value encountered in ...filepath... on line 166

Line 166 looks like this:

$numPieces = ($Dim['bays']*$Dim['purlins'])*$Dim['qty'];

The error doesn't even tell me which of the three variables is throwing the error.  Normally, this wouldn't be an issue, because I'd just print out all the variables in the equation to see what they're being set to.  However, because of the issue that started this thread, that's not possible.

I've tried to catch this error in the following ways:

try {
	$numPieces = ($Dim['bays']*$Dim['purlins'])*$Dim['qty'];
} catch (Throwable $e) {
	echo "<pre>".print_r($Dim, true)."</pre>";
}

and:

$blnDump = false;
$which = '';
if (!is_numeric($Dim['bays'])) {
	$blnDump = true;
	$which = 'bays';
} elseif (!is_numeric($Dim['purlins'])) {
	$blnDump = true;
	$which = 'purlins';
} elseif (!is_numeric($Dim['qty'])) {
	$blnDump = true;
	$which = 'qty';
}
if ($blnDump)
	echo "$which was non-numeric:<br /><pre>".print_r($Dim, true)."</pre>";

but neither one catches the error and I just get the error printed on the page.  The error prints twice, because the code above is in a function that gets called multiple times within a loop, and the issue apparently is occurring on two different iterations of the loop.  And, because the code is in a loop, I can't use exit; or die(); to kill the script and print my debugging messages, because it kills the script on the first iteration of the loop.  As luck would have it, the first iteration is error free and is therefore unhelpful to say the least.

Edited by ShadowMage
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...