PHP Incorrect variable declaration












9















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?










share|improve this question




















  • 1





    For anyone curious about the output in different PHP versions: 3v4l.org/Q8iiY

    – Namoshek
    Nov 16 '18 at 16:07






  • 4





    It's because the string 'key' when treated as an integer becomes 0 - since $variable is a string what you're doing is 'Hello World'[0] ... which is H

    – CD001
    Nov 16 '18 at 16:07






  • 1





    ^^^ So remove $variable = ''; or do $variable = ;

    – AbraCadaver
    Nov 16 '18 at 16:08













  • Can't find any reference to this behavior in my limited searches but chances are you'll just want to fix all the buggy code -- which, hopefully your project isn't too large. This is incorrect syntax either way, not really sure why PHP 5 would behave like that except: PHP. Can't find any related php.ini settings either.

    – Sheng Slogar
    Nov 16 '18 at 16:11











  • Unfortunately, phpstan doesn't consider this bad code. So I also don't see a good way of automatically fixing these issues.

    – Namoshek
    Nov 16 '18 at 16:14
















9















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?










share|improve this question




















  • 1





    For anyone curious about the output in different PHP versions: 3v4l.org/Q8iiY

    – Namoshek
    Nov 16 '18 at 16:07






  • 4





    It's because the string 'key' when treated as an integer becomes 0 - since $variable is a string what you're doing is 'Hello World'[0] ... which is H

    – CD001
    Nov 16 '18 at 16:07






  • 1





    ^^^ So remove $variable = ''; or do $variable = ;

    – AbraCadaver
    Nov 16 '18 at 16:08













  • Can't find any reference to this behavior in my limited searches but chances are you'll just want to fix all the buggy code -- which, hopefully your project isn't too large. This is incorrect syntax either way, not really sure why PHP 5 would behave like that except: PHP. Can't find any related php.ini settings either.

    – Sheng Slogar
    Nov 16 '18 at 16:11











  • Unfortunately, phpstan doesn't consider this bad code. So I also don't see a good way of automatically fixing these issues.

    – Namoshek
    Nov 16 '18 at 16:14














9












9








9


0






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?










share|improve this question
















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?







php php-5.6 legacy-code php-7.2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 18:08







itsliamoco

















asked Nov 16 '18 at 16:02









itsliamocoitsliamoco

295217




295217








  • 1





    For anyone curious about the output in different PHP versions: 3v4l.org/Q8iiY

    – Namoshek
    Nov 16 '18 at 16:07






  • 4





    It's because the string 'key' when treated as an integer becomes 0 - since $variable is a string what you're doing is 'Hello World'[0] ... which is H

    – CD001
    Nov 16 '18 at 16:07






  • 1





    ^^^ So remove $variable = ''; or do $variable = ;

    – AbraCadaver
    Nov 16 '18 at 16:08













  • Can't find any reference to this behavior in my limited searches but chances are you'll just want to fix all the buggy code -- which, hopefully your project isn't too large. This is incorrect syntax either way, not really sure why PHP 5 would behave like that except: PHP. Can't find any related php.ini settings either.

    – Sheng Slogar
    Nov 16 '18 at 16:11











  • Unfortunately, phpstan doesn't consider this bad code. So I also don't see a good way of automatically fixing these issues.

    – Namoshek
    Nov 16 '18 at 16:14














  • 1





    For anyone curious about the output in different PHP versions: 3v4l.org/Q8iiY

    – Namoshek
    Nov 16 '18 at 16:07






  • 4





    It's because the string 'key' when treated as an integer becomes 0 - since $variable is a string what you're doing is 'Hello World'[0] ... which is H

    – CD001
    Nov 16 '18 at 16:07






  • 1





    ^^^ So remove $variable = ''; or do $variable = ;

    – AbraCadaver
    Nov 16 '18 at 16:08













  • Can't find any reference to this behavior in my limited searches but chances are you'll just want to fix all the buggy code -- which, hopefully your project isn't too large. This is incorrect syntax either way, not really sure why PHP 5 would behave like that except: PHP. Can't find any related php.ini settings either.

    – Sheng Slogar
    Nov 16 '18 at 16:11











  • Unfortunately, phpstan doesn't consider this bad code. So I also don't see a good way of automatically fixing these issues.

    – Namoshek
    Nov 16 '18 at 16:14








1




1





For anyone curious about the output in different PHP versions: 3v4l.org/Q8iiY

– Namoshek
Nov 16 '18 at 16:07





For anyone curious about the output in different PHP versions: 3v4l.org/Q8iiY

– Namoshek
Nov 16 '18 at 16:07




4




4





It's because the string 'key' when treated as an integer becomes 0 - since $variable is a string what you're doing is 'Hello World'[0] ... which is H

– CD001
Nov 16 '18 at 16:07





It's because the string 'key' when treated as an integer becomes 0 - since $variable is a string what you're doing is 'Hello World'[0] ... which is H

– CD001
Nov 16 '18 at 16:07




1




1





^^^ So remove $variable = ''; or do $variable = ;

– AbraCadaver
Nov 16 '18 at 16:08







^^^ So remove $variable = ''; or do $variable = ;

– AbraCadaver
Nov 16 '18 at 16:08















Can't find any reference to this behavior in my limited searches but chances are you'll just want to fix all the buggy code -- which, hopefully your project isn't too large. This is incorrect syntax either way, not really sure why PHP 5 would behave like that except: PHP. Can't find any related php.ini settings either.

– Sheng Slogar
Nov 16 '18 at 16:11





Can't find any reference to this behavior in my limited searches but chances are you'll just want to fix all the buggy code -- which, hopefully your project isn't too large. This is incorrect syntax either way, not really sure why PHP 5 would behave like that except: PHP. Can't find any related php.ini settings either.

– Sheng Slogar
Nov 16 '18 at 16:11













Unfortunately, phpstan doesn't consider this bad code. So I also don't see a good way of automatically fixing these issues.

– Namoshek
Nov 16 '18 at 16:14





Unfortunately, phpstan doesn't consider this bad code. So I also don't see a good way of automatically fixing these issues.

– Namoshek
Nov 16 '18 at 16:14












1 Answer
1






active

oldest

votes


















6














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.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341454%2fphp-incorrect-variable-declaration%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    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.






    share|improve this answer






























      6














      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.






      share|improve this answer




























        6












        6








        6







        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 17:05

























        answered Nov 16 '18 at 16:12









        Sander VisserSander Visser

        2,75011934




        2,75011934






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341454%2fphp-incorrect-variable-declaration%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini