Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Incorrect variable declaration

Debugging legacy code and I have a strange issue. The legacy code is being moved to PHP 7.2. I don't know which version of PHP it was originally written for but it does work in PHP 5.6.

Below is my example of the problem...

$variable = '';
$variable['key'] = 'Hello World!';

echo $variable['key'] // H

When I echo $variable['key'] it only gets the first character from the value. I know now that it is because $variable is initially declared as a string.

But why does this work in PHP 5.6? What can I do to make this work in 7.2 without trawling through thousands of lines of code?

Is there a directive like strict_types I can use?

like image 614
itsliamoco Avatar asked Nov 16 '18 16:11

itsliamoco


People also ask

Which are not correct variable in PHP?

A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

What are the rules to declare variable in PHP?

Rules for PHP variables: A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Variable names are case-sensitive ($age and $AGE are two different variables)

Which variable syntax is valid for PHP?

All variables in PHP start with a $ sign, followed by the name of the variable. A variable name must start with a letter or the underscore character _ . A variable name cannot start with a number. A variable name in PHP can only contain alpha-numeric characters and underscores ( A-z , 0-9 , and _ ).


1 Answers

From php.net

Warning Writing to an out of range offset pads the string with spaces. Non-integer types are converted to integer. Illegal offset type emits E_NOTICE. Only the first character of an assigned string is used. As of PHP 7.1.0, assigning an empty string throws a fatal error. Formerly, it assigned a NULL byte.

http://php.net/manual/en/language.types.string.php#language.types.string.substr

So "key" is converted to 0, and the first character is set. Because this is a char type, only "H" is set from the given string.

$variable = '';
$variable['key'] = 'Hello World!';

echo $variable;       
echo $variable['key'];

If you change your code to the above you can see better what happens.

So the text 'ello World!' is lost and gone in PHP >= 7.1 because you set the first character, the type stays string.

In php 5.6 you will get Notice: Array to string conversion in /in/N2poP on line 6

So in prior versions you overwrite the complete variable, and the initial empty string would be gone, PHP simply creates a new array. This behavior only happens with an empty string!

This is also noted in the documentation: http://php.net/manual/en/language.types.string.php#language.types.string.substr

Note: As of PHP 7.1.0, applying the empty index operator on an empty string throws a fatal error. Formerly, the empty string was silently converted to an array.

The easiest solution would be removing the $variable = ''; part, it's invalid anyway and never used in your legacy code. or by replacing it with $variable = [];

Because this behavior only happens with an empty string in php < 7.1 you could use a regular expression to find all places where you should refactor to fix the issue.

like image 51
Sander Visser Avatar answered Oct 17 '22 01:10

Sander Visser