469,642 Members | 1,133 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,642 developers. It's quick & easy.

Incrementing an empty string

code green
1,726 Expert 1GB
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
May 15 '08 #1
6 2139
hsriat
1,654 Expert 1GB
And do you consider it as a good thing or a bad thing?
May 15 '08 #2
code green
1,726 Expert 1GB
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?
May 15 '08 #3
dlite922
1,584 Expert 1GB
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.
May 16 '08 #4
Atli
5,058 Expert 4TB
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.

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.
May 16 '08 #5
code green
1,726 Expert 1GB
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
May 16 '08 #6
Atli
5,058 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. $value = "a3";
  2. for($i = 0; $i < 15; $i++, $value++);
  3. echo $value;
  4.  
Will result in "b8".

While this:
Expand|Select|Wrap|Line Numbers
  1. $value = "a3?";
  2. for($i = 0; $i < 15; $i++, $value++);
  3. echo $value;
  4.  
Doesn't change the value at all.
May 16 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Dan Bass | last post: by
14 posts views Thread by cj | last post: by
26 posts views Thread by Neville Lang | last post: by
4 posts views Thread by masterpaladin38 | last post: by
35 posts views Thread by Smithers | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.