logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Other Software View previous topic :: View next topic
Reply to topic   Topic: PHP8 Too many warnings, how to distinguish
Author
spser



Joined: 29 Aug 2016
Posts: 97

PostPosted: Mon 07 Dec '20 6:34    Post subject: PHP8 Too many warnings, how to distinguish Reply with quote

Countless _GET['**'] _POST['**'] calls, warnings pop up. Now you need to close the warning report to run, but some warning reports are indispensable.

How should I be compatible?


"A number of notices have been converted into warnings:

Attempting to read an undefined variable.
Attempting to read an undefined property.
Attempting to read an undefined array key.
Attempting to read a property of a non-object.
Attempting to access an array index of a non-array.
Attempting to convert an array to string.
Attempting to use a resource as an array key.
Attempting to use null, a boolean, or a float as a string offset.
Attempting to read an out-of-bounds string offset.
Attempting to assign an empty string to a string offset.
Attempting to assign multiple bytes to a string offset will now emit a warning."
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Mon 07 Dec '20 15:47    Post subject: Reply with quote

The true way would be to check of that variable index is available.

e.g.
Code:

if (isset($_POST['x']) {
//...
}


Thad bad and ugly way is to change the error level in php.ini, since it is now E_ALL by default. PHP 7.x it was E_ALL & ~E_NOTICE
Back to top
spser



Joined: 29 Aug 2016
Posts: 97

PostPosted: Fri 11 Dec '20 10:38    Post subject: Reply with quote

Too many places are written as if(POST[xx]), it is very difficult to change.
Also don't want to turn off the warning prompt...
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Fri 11 Dec '20 15:06    Post subject: Reply with quote

Quote:
Also don't want to turn off the warning prompt...


What are you talking about?
Back to top
spser



Joined: 29 Aug 2016
Posts: 97

PostPosted: Thu 24 Dec '20 8:44    Post subject: Reply with quote

James Blond wrote:
Quote:
Also don't want to turn off the warning prompt...


What are you talking about?


Code:

php 5 / php 7 
echo $arr['user'][$userid][$this->age];  // return null
 
php 8
echo $arr['user'][$userid][$this->age];  // Warning: Undefined variable ***
// How to write so as not to make mistakes??
if( isset($arr) && isset($arr['user']) && isset($userid) && isset($this->age) && isset($arr['user'][$userid])..... )

Unrestricted isset?
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Mon 28 Dec '20 23:43    Post subject: Reply with quote

spser wrote:


Code:

php 5 / php 7 
echo $arr['user'][$userid][$this->age];  // return null
 
php 8
echo $arr['user'][$userid][$this->age];  // Warning: Undefined variable ***
// How to write so as not to make mistakes??
if( isset($arr) && isset($arr['user']) && isset($userid) && isset($this->age) && isset($arr['user'][$userid])..... )

Unrestricted isset?


For the Pointer you can the PHP 8 new feature https://wiki.php.net/rfc/nullsafe_operator
I don't know if that works for array, too. PHP 8 is fairly new.
Back to top
spser



Joined: 29 Aug 2016
Posts: 97

PostPosted: Tue 05 Jan '21 7:49    Post subject: Reply with quote

James Blond wrote:
spser wrote:


Code:

php 5 / php 7 
echo $arr['user'][$userid][$this->age];  // return null
 
php 8
echo $arr['user'][$userid][$this->age];  // Warning: Undefined variable ***
// How to write so as not to make mistakes??
if( isset($arr) && isset($arr['user']) && isset($userid) && isset($this->age) && isset($arr['user'][$userid])..... )

Unrestricted isset?


For the Pointer you can the PHP 8 new feature https://wiki.php.net/rfc/nullsafe_operator
I don't know if that works for array, too. PHP 8 is fairly new.


Code:
$arr = array();

echo $arr['bb']?['dd'];  // Parse error: syntax error, unexpected token ";"
echo $arr['bb'?]['dd'];    // Parse error: syntax error, unexpected token "]"

don't works.... help.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Tue 05 Jan '21 16:28    Post subject: Reply with quote

Well then you have to use the isset function
Back to top
spser



Joined: 29 Aug 2016
Posts: 97

PostPosted: Wed 06 Jan '21 8:39    Post subject: Reply with quote

James Blond wrote:
Well then you have to use the isset function


Isset has to be used many, many times, and even the judgment is incomplete.

Not the best way.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Wed 06 Jan '21 9:42    Post subject: Reply with quote

it seems your code is not PHP 8 ready.

You could change the error level in php.ini to

Code:

E_ALL & ~E_NOTICE


That doesn't improve the code, but it will hide the notice errors.
Back to top
spser



Joined: 29 Aug 2016
Posts: 97

PostPosted: Thu 07 Jan '21 8:44    Post subject: Reply with quote

James Blond wrote:
it seems your code is not PHP 8 ready.

You could change the error level in php.ini to

Code:

E_ALL & ~E_NOTICE


That doesn't improve the code, but it will hide the notice errors.


php8 returns a warning, not a prompt E_NOTICE
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Thu 07 Jan '21 9:35    Post subject: Reply with quote

spser wrote:


php8 returns a warning, not a prompt E_NOTICE


RTFM? [1] Sure you can turn of Warnings, too. However, I don't recommend running your code with PHP 8. Is there a specific reason you need PHP 8?



[1] https://www.php.net/manual/en/function.error-reporting
Back to top
spser



Joined: 29 Aug 2016
Posts: 97

PostPosted: Fri 08 Jan '21 10:09    Post subject: Reply with quote

James Blond wrote:
spser wrote:


php8 returns a warning, not a prompt E_NOTICE


RTFM? [1] Sure you can turn of Warnings, too. However, I don't recommend running your code with PHP 8. Is there a specific reason you need PHP 8?



[1] https://www.php.net/manual/en/function.error-reporting


Early technical test, operation and maintenance may upgrade php8
Back to top


Reply to topic   Topic: PHP8 Too many warnings, how to distinguish View previous topic :: View next topic
Post new topic   Forum Index -> Other Software