How to suppress warning of a particular line of code?

2

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

Excerpt of 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;
    }
...

Log file generated by Nginx:

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...
    
asked by anonymous 03.11.2017 / 20:10

2 answers

2

When accessing $Key[$SrcPos] in some iteration of while it generates a warning because the value of $SrcPos does not match any key of the array, in these cases it is simpler and correct to use a isset() to handle the situation. p>

The error happens in ord($Key[$SrcPos] and not in the @$SrcAs assignment if it were to apply at (not recommended) it should be this way: $SrcAsc = $SrcAsc ^ ord(@$Key[$SrcPos]);

You 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
}

Relationships:

What's the function of '@' at the start of expressions in PHP

Why do you say that using @ atm to suppress errors is bad practice?

    
03.11.2017 / 20:22
2

The correct thing is to always handle the errors, for a better performance of your script , however as the question is about suppress :

Suppressing System-wide

<?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 PHP documentation

    
03.11.2017 / 20:32