Incrementing an empty string – PHP

  php

Q(Question):

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

A(Answer):

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

A(Answer):

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?

A(Answer):

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.

A(Answer):

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.

A(Answer):

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

A(Answer):

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:


$value = "a3";
for($i = 0; $i < 15; $i++, $value++);
echo $value;

Will result in "b8".

While this:


$value = "a3?";
for($i = 0; $i < 15; $i++, $value++);
echo $value;

Doesn’t change the value at all.

LEAVE A COMMENT