i'm realy done with this. My problem is the following:
code:
<?php
$highest = 0;
$string = "a.1";
$array = explode(".",$string);
$highest = $array[1];
$final = "secound value is: ".$highest++;
echo $final;
?>
All i want is adding something to the number in the $string. So as a result it should echo 2. However it echos 1.
Whats wrong?
Use pre-increment:
$final = "secound value is: ".++$highest;
More info: Incrementing/Decrementing Operators
Try the below code
<?php
$highest = 0;
$string = "a.1";
$array = explode(".",$string);
$highest = $array[1];
$final = "secound value is: ". ++$highest;
echo $final;
?>
The reason is, $highest++
is the post-increment. It will increment the value only after its usage. And ++$highest
is pre-increment will increment the value first and then use it
You are doing a post increment. If you do a pre-increment, you will get 2 as your result. Is this what you want?
$final = "secound value is: ".++$highest;
Just to put an answer out of the box. You can save yourself exploding and an array that you may not need/want.
$highest = substr($str, 2) + 1;
There is a difference between pre-increment and post increment.
pre-increment-(++$highest)
At first increase the value by one then executes the statement.
post-increment-($highest++)
At first executes the statement then increase the value by one.
So in this case You have to use pre-increment.Then line will be executed after increment of the value .
Try this code:
<?php
$highest = 0;
$string = "a.1";
$array = explode(".",$string);
$highest = $array[1];
$final = "secound value is: ".++$highest;
echo $final;
?>