For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > November 2005 > removing PHP warnings for specific scripts









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author removing PHP warnings for specific scripts
ice

2005-11-18, 7:55 am

Hi there

I am using some 3rd party code which is generating a PHP Warning in
the log file. I don't want to switch off warnings entirely for my Dev
box, but would like to disable this warning for the specific file.

Is there a way of doing this?
for example

in the php.ini,
error_reporting = E_ALL

seems to be for the entire php instance.
but can i do something like:

error_reporting = E_ALL

path to file - error_reporting = E_ERROR

If anyone knows log4j, looking for that kind of error logging, where
individual classes or packages could ahve different loggin levels.

thank you

Ice70
Michael Meckelein

2005-11-18, 7:55 am

"ice" wrote
> Hi there
>
> I am using some 3rd party code which is generating a PHP Warning in
> the log file. I don't want to switch off warnings entirely for my Dev
> box, but would like to disable this warning for the specific file.
>
> Is there a way of doing this?
> for example
>
> in the php.ini,
> error_reporting = E_ALL
>
> seems to be for the entire php instance.


"The error_reporting() function sets the error_reporting directive at
runtime. PHP has many levels of errors, using this function sets that level
for the duration (runtime) of your script."
http://www.php.net/error_reporting

Michael


ice

2005-11-18, 6:57 pm

On Fri, 18 Nov 2005 12:49:52 +0100, "Michael Meckelein"
<michael@go-on-line.de> wrote:

>"ice" wrote
>
>"The error_reporting() function sets the error_reporting directive at
>runtime. PHP has many levels of errors, using this function sets that level
>for the duration (runtime) of your script."
>http://www.php.net/error_reporting
>
>Michael
>

Hi Michael

thank you for the pointer.

The files I am trying to supress warnings are included in many other
scripts, and setting the error_reporting(E_ERROR) also removed the
noice / warnings on the 'parent' script - the code I am writing and
would like these to remain so I can make sure I have clean code. Is
there a way of limiting the error_reporting() to only the limits of a
script on a page, not the 'parent' its included in?

Thank you for you help

Ice70
ZeldorBlat

2005-11-18, 6:57 pm

>I am using some 3rd party code which is generating a PHP Warning in
>the log file. I don't want to switch off warnings entirely for my Dev
>box, but would like to disable this warning for the specific file.
>
>Is there a way of doing this?


If you want to supress errors and warnings for a particular function
call, you can prepend an @ to the function name when you call it:

$x = @myFunc($arg1, $arg2);

ice

2005-11-18, 6:57 pm

On 18 Nov 2005 06:36:05 -0800, "ZeldorBlat" <zeldorblat@gmail.com>
wrote:

>
>If you want to supress errors and warnings for a particular function
>call, you can prepend an @ to the function name when you call it:
>
>$x = @myFunc($arg1, $arg2);


Sorted, tracked down how the warning was generated and added the '@'
before its call.
Not exactly what I was after, but it has removed the warnings, so i
can see the problems in my code - and that is close enough for me :)

thank you :)

Ice70
mgor

2005-11-18, 6:57 pm


<?php
ini_set( 'error_reporting', E_ERROR );
?>

http://www.php.net/manual/en/function.ini-set.php


--
mgor
------------------------------------------------------------------------
mgor's Profile: http://www.macosx.com/forums/member.php?userid=48939
View this thread: http://www.macosx.com/forums/showthread.php?t=260967
macosx.com - The Answer to Mac Support - http://www.macosx.com

Connector5

2005-11-18, 9:55 pm

nah... He doesnt want to do it to all his scripts at once...


This example function will turn on error reporting for just one include,
with some benefits. Reusable code rocks!


function debug_include($filename, $suppress_notices = false)
{
if ($suppress_notices)
{
// Save yourself some garbage :-)
error_reporting(E_ALL ^ E_NOTICE);
}
else
{
error_reporting(E_ALL);
}

foreach($GLOBALS as $key => $val)
{
if (!is_numeric(substr($key, 0, 1)))
{
if (!$key == 'filename') /* Just to be safe */
{
// Bring the globals into the local scope, reference style.
$$key = &$val;
}
}
}


// Use include_once if you need to...
include($filename);

error_reporting(0);
}



"mgor" <mgor.1ypbja@nomx.macosx.com> wrote in message
news:mgor.1ypbja@nomx.macosx.com...
>
> <?php
> ini_set( 'error_reporting', E_ERROR );
> ?>
>
> http://www.php.net/manual/en/function.ini-set.php
>
>
> --
> mgor
> ------------------------------------------------------------------------
> mgor's Profile: http://www.macosx.com/forums/member.php?userid=48939
> View this thread: http://www.macosx.com/forums/showthread.php?t=260967
> macosx.com - The Answer to Mac Support - http://www.macosx.com
>



ice

2005-11-21, 6:56 pm

Hi Connector5

that rocks!

thank you very much
Ice

On Fri, 18 Nov 2005 17:48:12 -0800, "Connector5"
<junkmilenko@charter.net> wrote:

>nah... He doesnt want to do it to all his scripts at once...
>
>
>This example function will turn on error reporting for just one include,
>with some benefits. Reusable code rocks!
>
>
>function debug_include($filename, $suppress_notices = false)
>{
> if ($suppress_notices)
> {
> // Save yourself some garbage :-)
> error_reporting(E_ALL ^ E_NOTICE);
> }
> else
> {
> error_reporting(E_ALL);
> }
>
> foreach($GLOBALS as $key => $val)
> {
> if (!is_numeric(substr($key, 0, 1)))
> {
> if (!$key == 'filename') /* Just to be safe */
> {
> // Bring the globals into the local scope, reference style.
> $$key = &$val;
> }
> }
> }
>
>
> // Use include_once if you need to...
> include($filename);
>
> error_reporting(0);
>}
>
>
>
>"mgor" <mgor.1ypbja@nomx.macosx.com> wrote in message
>news:mgor.1ypbja@nomx.macosx.com...
>


Hilarion

2005-11-21, 6:56 pm

> function debug_include($filename, $suppress_notices = false)
> {
> if ($suppress_notices)
> {
> // Save yourself some garbage :-)
> error_reporting(E_ALL ^ E_NOTICE);
> }
> else
> {
> error_reporting(E_ALL);
> }
>
> foreach($GLOBALS as $key => $val)
> {
> if (!is_numeric(substr($key, 0, 1)))
> {
> if (!$key == 'filename') /* Just to be safe */
> {
> // Bring the globals into the local scope, reference style.
> $$key = &$val;
> }
> }
> }
>
>
> // Use include_once if you need to...
> include($filename);
>
> error_reporting(0);
> }



I'd change it to something like this (not tested):

function debug_include( $filename, $error_level = E_ALL )
{
foreach( $GLOBALS as $key => $val )
{
if (!is_numeric( substr( $key, 0, 1 ) ) && ($key != 'filename'))
{
$$key = &$val;
}
}

$previous_level = error_reporting( $error_level );

include $filename;

error_reporting( $previous_level );
}

which allows custom error_reporting level and sets the error reporting
to the level it was before after including the file and this way
it'll not affect the script that does the include.

The flaw of such include method is (AFAIK) that any "unset" made to the
"global" variables done in the included script will not affect variables
in the script which called this "debug_include" (only thing that is
unset is the local reference).


Hilarion
ice

2005-11-21, 6:56 pm

Thank you Hilarion

As it turned out, the 3rd party include didn;t actually do what I
wanted - an XML parser that didn't actually parse correctly :(

The error reporting stuff here is really handy, big thank you.

Will try & rememeber the Global problem if I start to get weirdness :)

Ice

On Mon, 21 Nov 2005 17:43:37 +0100, "Hilarion"
<hilarion@SPAM.op.SMIECI.pl> wrote:

>
>
>I'd change it to something like this (not tested):
>
>function debug_include( $filename, $error_level = E_ALL )
>{
> foreach( $GLOBALS as $key => $val )
> {
> if (!is_numeric( substr( $key, 0, 1 ) ) && ($key != 'filename'))
> {
> $$key = &$val;
> }
> }
>
> $previous_level = error_reporting( $error_level );
>
> include $filename;
>
> error_reporting( $previous_level );
>}
>
>which allows custom error_reporting level and sets the error reporting
>to the level it was before after including the file and this way
>it'll not affect the script that does the include.
>
>The flaw of such include method is (AFAIK) that any "unset" made to the
>"global" variables done in the included script will not affect variables
>in the script which called this "debug_include" (only thing that is
>unset is the local reference).
>
>
>Hilarion


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com