php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #54549 Using $variable--; and $variable -= '1'; yield different results.
Submitted: 2011-04-17 14:03 UTC Modified: 2011-04-26 21:30 UTC
Votes:3
Avg. Score:1.0 ± 0.0
Reproduced:0 of 3 (0.0%)
From: account22a23 at hotmail dot com Assigned: salathe (profile)
Status: Closed Package: *Programming Data Structures
PHP Version: 5.3.6 OS: Fedora 14
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: account22a23 at hotmail dot com
New email:
PHP Version: OS:

 

 [2011-04-17 14:03 UTC] account22a23 at hotmail dot com
Description:
------------
While the code below may have other problems, the simple fact of the matter is that when I uncomment "$int--;" and comment "$int -= '1';" the code will loop for at least a minute, but when it is left as is below it does not and will terminate as expected.  Since I've been told the two pieces of code have the exact same logic, I have no explaination for this.  Please contact me if you know why.  The php package was installed using 'yum install php php-mysql' on fedora 14.




Test script:
---------------
<?php
function GeneratePassword($int, $string){
	$new_string = '';
	while ($int >= 0){
		$new_string += $string[rand(0, (strlen($string) - 1))];
		//$int--;
		$int -= '1';
		echo 'round';
		echo 'dump of int: ' . var_dump($int);
		echo 'dump of string: ' . var_dump($string);
		echo 'dump of new_string: ' . var_dump($new_string);
		
	}
	return ($new_string);
}

fwrite(STDOUT, "Choose an int");
$int = fgets(STDIN);
fwrite(STDOUT, "Choose a string");
$string = fgets(STDIN);
$pass = GeneratePassword($int, $string);
echo 'anything';
echo $pass;
?>

Actual result:
--------------
CASE 1 USING:
//$int--;
$int -= '1';
------------------------------------------------------

php -f delete_me2.php
Choose an int2
Choose a stringbb
roundint(1)
dump of int: string(3) "bb
"
dump of string: int(0)
dump of new_string: roundint(0)
dump of int: string(3) "bb
"
dump of string: int(0)
dump of new_string: roundint(-1)
dump of int: string(3) "bb
"
dump of string: int(0)
dump of new_string: anything0


CASE 2 USING:
$int--;
//$int -= '1';
------------------------------------------------------

php -f delete_me2.php
Choose an int2
Choose a stringbb
roundstring(2) "2
"
dump of int: string(3) "bb
"
dump of string: string(0) ""
dump of new_string: roundstring(2) "2
"
dump of int: string(3) "bb
"
dump of string: string(0) ""
dump of new_string: roundstring(2) "2
"
dump of int: string(3) "bb
"
dump of string: string(0) ""
dump of new_string: roundstring(2) "2
"
dump of int: string(3) "bb
"
dump of string: string(0) ""
dump of new_string: roundstring(2) "2
"
dump of int: string(3) "bb
"
dump of string: string(0) ""
dump of new_string: roundstring(2) "2
"
dump of int: string(3) "bb
"
dump of string: string(0) ""
dump of new_string: roundstring(2) "2
"
dump of int: string(3) "bb
"
dump of string: string(0) ""
dump of new_string: roundstring(2) "2
"
dump of int: string(3) "bb
"
dump of string: string(0) ""
dump of new_string: roundstring(2) "2
"
dump of int: string(3) "bb
"
dump of string: string(0) ""
dump of new_string: roundstring(2) "2
"
dump of int: string(3) "bb
"
dump of string: string(0) ""
dump of new_string: roundstring(2) "2
"
dump of int: string(3) "bb
"
dump of string: string(0) ""
dump of new_string: roundstring(2) "2
"

... (loops for at least 60 seconds) ...

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-17 14:19 UTC] hotmail22a23 at hotmail dot com
Code was run from command line.
 [2011-04-17 14:20 UTC] account22a23 at hotmail dot com
Code was run from command line.
 [2011-04-17 15:05 UTC] cataphract@php.net
-Type: Bug +Type: Documentation Problem
 [2011-04-17 15:05 UTC] cataphract@php.net
The post decrement operator has no effect on non-numeric strings. Your $int variable is not == "2"; its value is actually "2\n" (with a line break on the end), so you get no effect, as in:

<?php
$a = $b = "2\n";
$a--;
var_dump($a === $b); //bool(true)

Using $a -= '1'; works because this operation is more permissive on the first operand and takes "2\n" as if it were 2: decrement_function calls is_numeric_string with allow_errors=0 and sub_function with allow_errors=1.

This should be documented.

Also, you might want to use:
echo 'dump of int: ', var_dump($int)
instead of
echo 'dump of int: ' . var_dump($int)
because var_dump has side effects and by using ".", you're forcing it to be evaluated before anything is echoed.
 [2011-04-26 21:29 UTC] salathe@php.net
Automatic comment from SVN on behalf of salathe
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=310528
Log: decrementing, or incrementing non-A-Za-z, string variables has no effect (doc #54549)
 [2011-04-26 21:30 UTC] salathe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: salathe
 [2011-04-26 21:30 UTC] salathe@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC