Incrementing an empty string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    Incrementing an empty string

    If somewhere inside a script I am doing the following [PHP]$myvar ++;[/PHP]
    Does it make any difference whether I initialise the variable
    [PHP]$myvar = '';
    or
    $myvar = 0;[/PHP] I come from a C/C++ background which would fall over if you triied to add one to an empty string.
    If php debugging is set to STRICT,
    a warning is generated if an attempt to increment a non declared variable is made.
    I try to write robust code and find it hard to accept there is no potential problem here
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    And do you consider it as a good thing or a bad thing?

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      The power to assign any type to a variable is useful.
      When returning a value from a function for example.
      it can be a boolean, a float or even an error message depending on what happened inside the function.
      I found strict type casting frustrating in this example using C++.

      But once a variable has been assigned a value, albeit an empty string,
      it makes me jittery to ADD an integer to this.

      What for example happens here [PHP]$a = 'A';
      $a++;[/PHP] I do not see this being any different to [PHP]$a = '';
      $a++;[/PHP] And moderators, what has happened to the forum PHP tags?

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        Originally posted by code green
        If somewhere inside a script I am doing the following [PHP]$myvar ++;[/PHP]
        Does it make any difference whether I initialise the variable
        [PHP]$myvar = '';
        or
        $myvar = 0;[/PHP] I come from a C/C++ background which would fall over if you triied to add one to an empty string.
        If php debugging is set to STRICT,
        a warning is generated if an attempt to increment a non declared variable is made.
        I try to write robust code and find it hard to accept there is no potential problem here
        stick to the methods you used in C++ while coding PHP and you'll be safe. You'll produce much better software.

        To me having loose types, is like slacking off. I dont' do it for my big applications.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          PHP is a "loosely typed language" with very powerful conversion capabilities.
          This can be both a good and a bad thing. It can make it very easy to work with variables, but it can also cause errors, when people are not familiar with how specific variables will be converted and expect different results.

          For people used to "strongly typed languages" like C, where every variable has to be defined as a specific type, this can be somewhat strange.

          If you increment a pre-defined string containing an x number of characters, the last character in the string will be incremented and effectively changed into the next character in the alphabet.

          A variable that has been defined as "" or '' would be considered NULL and/or 0, which will be incremented into 1.

          I recommend keeping close eye on what types your variables are tho, not relying on PHP's default conversions to much. They can change and mess everything up.

          Originally posted by code green
          And moderators, what has happened to the forum PHP tags?
          There were some issues with the highlighting software being used so it was removed. I do believe they are currently looking for replacements tho.

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            Good discussion with some good points.
            Loosly typed code can lead to bad habits and problems.
            I have seen 'programmers' struggling with sha1() and md5() for example
            when the value is a float, and not understanding the weird results because a string is expected.
            If you increment a pre-defined string containing an x number of characters, the last character in the string will be incremented and effectively changed into the next character in the alphabet
            So it increments the ASCII code value?
            I never knew that
            Last edited by code green; May 16 '08, 10:40 AM. Reason: mistake

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Originally posted by code green
              So it increments the ASCII code value?
              I never knew that
              I don't think it's quite that simple.
              If it were indeed incrementing the ASCII code, it should increment all characters, not only alphanumeric characters. But it doesn't

              For example, this:
              [code=php]
              $value = "a3";
              for($i = 0; $i < 15; $i++, $value++);
              echo $value;
              [/code]
              Will result in "b8".

              While this:
              [code=php]
              $value = "a3?";
              for($i = 0; $i < 15; $i++, $value++);
              echo $value;
              [/code]
              Doesn't change the value at all.

              Comment

              Working...