How to delete a certain line of code warning?

Asked

Viewed 142 times

2

How to correctly suppress warnings generated by PHP in a given line of code? I tried using "@", but still the warning is generated in the log.

Excerpt from the code that is generating the Warning:

...
    while ($SrcPos <= strlen($Src)-1) {
      $SrcAsc = (ord($Src[$SrcPos]) + $OffSet) % 255;
      $SrcAsc = @$SrcAsc ^ ord($Key[$SrcPos]); //***** Nesta Linha! (inseri o @ mas ainda assim está gerando o Aviso)
      $Dest   = $Dest.strtoupper(dechex($SrcAsc));
      $OffSet = $SrcAsc;
      $SrcPos = $SrcPos + 1;
    }
...

Nginx generated Log File:

2017/11/03 16:01:01 [error] 2295#0: *382 FastCGI sent in stderr: "PHP message: PHP Notice:  Uninitialized string...
PHP message: PHP Notice:  Uninitialized string offset: 11 in /usr/share/nginx/www/util/functions.php on line 475
PHP message: PHP Notice:  Uninitialized string offset: 12 in /usr/share/nginx/www/util/functions.php on line 475
PHP message: PHP Notice:  Uninitialized string offset: 13 in /usr/share/nginx/www/util/functions.php on line 475
PHP message: PHP Notice:  Uninitialized string offset: 14 in /usr/share/nginx/www/util/functions.php on line 475
PHP message: PHP Notice:  Uninitialized string offset: 15 in /usr/share/nginx/www/util/functions.php on line 475...
  • 1

    pq not a isset() to handle the error? $Key[$SrcPos] must have an invalid Index.

  • @rray I will test and post the result.

2 answers

2


When accessing $Key[$SrcPos] in some while iteration generates a Warning due to the value of $SrcPos do not match any array key, in such cases it is simpler and correct to use a isset() to address the situation.

The mistake happens in ord($Key[$SrcPos] and not in the assignment @$SrcAs if you were to apply arroba (not recommended) it should be so: $SrcAsc = $SrcAsc ^ ord(@$Key[$SrcPos]);

Can solve the problem with a ternary:

$SrcAsc = isset($Key[$SrcPos]) ? ord($Key[$SrcPos]) : 0;

Or if/Else block:

if(isset($Key[$SrcPos])){
   $SrcAsc = ord($Key[$SrcPos]);
}else{
   //faz outra coisa
}

Related:

What is the function of@at the beginning of PHP expressions

Why do they say using @arroba to suppress errors is a bad practice?

  • That’s right @rray, it worked correctly. It was better this way, as it is avoiding the warning rather than hiding. I just don’t understand why "@" didn’t work either. Thanks!

  • @Carlosandrade edited the answer with more details.

  • Exactly. Now the use of "@".

  • I was running some tests on an online publisher and @ also was not working, do not know why, at home I will test again on my machine.

  • @Kaduamaral looks at that example. Has an extension that disables the @ more details here

2

The correct thing is to always treat the mistakes, for a better performance of your script, but how the question is about suppress:

Suppressing throughout the System

<?php

// Desliga o report de erros
error_reporting(0);

// Reporta apenas erros de execução
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reportar E_NOTICE pode ser bom também (reportar variáveis não 
// inicializadas e etc...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Reportar todos os erros exceto E_NOTICE
// Este é o padrão setado no php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Reportar todos os erros
error_reporting(E_ALL);

// O mesmo que error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

Source: This code has been copied and translated comments from the PHP documentation

  • Very good. Thanks for the tips!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.