2

Trying to use a simple "versioning" system for some hashes, I do the following:

$last_version = '009';
$increment = '001';

$result = $last_version + $increment;

var_dump($result);

I would expect: string(010) but I get int(10) and before I jump into if's and str-pad, I was wondering if there's any other way of conserving the desired format?

3
  • 1
    You need to cast it to a string after. Commented Feb 18, 2013 at 17:59
  • why not just store an int and then format it for output? $version = 1; $version++; echo sprintf('%03d', $version)? As for your str/int, you're forcing PHP to cast those strings to ints since you're using +`, which is addition. Commented Feb 18, 2013 at 18:02
  • There is no reason to expect string(010). If you wanted to combine strings, you would get "009001" otherwise, if you wanted to add thier numeric value, you would be using integers. It's easy to cast from one to another, but just try to understand the differences, otherwise you may be bug hunting for hours over something trivial in the future. Commented Feb 18, 2013 at 18:04

4 Answers 4

3

Using + automatically casts the variables into the appropriate number type (in this case an int, however different string formats can be casted to float).

If you want to keep the desired 0 left-padding, you can use sprintf() to format the result, as such:

$result = sprintf('%03d', $last_version + $increment);

The format specifier %03d specifies that you want an integer-string (d) with a length of 3 left-padded with the character 0.

More information about PHP's Type Juggling logic can be found in the PHP Documentation: Type Juggling

Sign up to request clarification or add additional context in comments.

3 Comments

Great... it did the job and I didn't have to end using str_pad
@w0rldart Just as an aside... why the aversion to str_pad? sprintf is doing the exact same thing here.
Yes, it does... it's just I wanted to see if there's any other alternative to it
1
$last_version = '009';
$increment = '001';

$result = $last_version + $increment;

$result = (string) $result ;

var_dump($result) ;

When you try to perform math operations with strings, they are cast to approprite type. In this case to int. But you can cast integer back to string in the above example.

3 Comments

Tried even before the question, I get the same output :\
At the end, $result is string(2) "10", not string(3) "010"
But yeah, sprintf("%03d", $result) in this case is the option.
1

You cannot add strings (it's as simple as that). That's why PHP implicitly converts both strings to integers (this is called dynamic typing).

To format your number, you could to the following:

$last_version = '009';
$increment    = '001';

$result = $last_version + $increment; // = 10
$result = sprintf("%03d", $result) // = "010"

Comments

1

When you use +, PHP will automatically cast the string to integers, thus the int(10 result you are seeing. You will not be able to add strings in this manner. So you best best would be to just keep the version as integer ans string pad like this:

$last_version = 9;
$increment = 1; 
$pad_length = 3;
$pad_string = '0';

$result = $last_version + increment; // or simply $last_version++; if increment will always be 1

$string_result = str_pad((string)$result, $pad_length, $pad_string, STR_PAD_LEFT);

2 Comments

I had ended up with a code similar to this one, but just as I asked... I was looking for a way to avoid str_pad and more lines of code.
@w0rldart You can certainlt use sprintf() as pointed out in other answers, however this is really no different. The key takeaway here is that in PHP strings can't be added like that. The math must be done on integers and the string format set afterwards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.