Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Incrementing an empty string

Question posted by: code green (Expert) on May 15th, 2008 11:24 AM
If somewhere inside a script I am doing the following
Code: ( text )
  1. $myvar ++;

Does it make any difference whether I initialise the variable
Code: ( text )
  1. $myvar = '';
  2. or
  3. $myvar = 0;
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
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
hsriat's Avatar
hsriat
Expert
1,447 Posts
May 15th, 2008
01:26 PM
#2

Re: Incrementing an empty string
And do you consider it as a good thing or a bad thing?

Reply
code green's Avatar
code green
Expert
738 Posts
May 15th, 2008
02:44 PM
#3

Re: Incrementing an empty string
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
Code: ( text )
  1. $a = 'A';
  2. $a++;
I do not see this being any different to
Code: ( text )
  1. $a = '';
  2. $a++;
And moderators, what has happened to the forum PHP tags?

Reply
dlite922's Avatar
dlite922
Needs Regular Fix
491 Posts
May 16th, 2008
07:56 AM
#4

Re: Incrementing an empty string
Quote:
Originally Posted by code green
If somewhere inside a script I am doing the following
Code: ( text )
  1. $myvar ++;

Does it make any difference whether I initialise the variable
Code: ( text )
  1. $myvar = '';
  2. or
  3. $myvar = 0;
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.

Reply
Atli's Avatar
Atli
Moderator
2,028 Posts
May 16th, 2008
09:28 AM
#5

Re: Incrementing an empty string
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.

Quote:
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.

Reply
code green's Avatar
code green
Expert
738 Posts
May 16th, 2008
10:00 AM
#6

Re: Incrementing an empty string
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.
Quote:
Originally Posted by
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 16th, 2008 at 10:40 AM. Reason: mistake
Reply
Atli's Avatar
Atli
Moderator
2,028 Posts
May 16th, 2008
12:23 PM
#7

Re: Incrementing an empty string
Quote:
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: ( text )
  1. $value = "a3";
  2. for($i = 0; $i < 15; $i++, $value++);
  3. echo $value;

Will result in "b8".

While this:
Code: ( text )
  1. $value = "a3?";
  2. for($i = 0; $i < 15; $i++, $value++);
  3. echo $value;

Doesn't change the value at all.

Reply
Reply
Not the answer you were looking for? Post your question . . .
178,100 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top PHP Forum Contributors