Doc #54549 [Opn->Csd]: Using $variable--; and $variable -= '1'; yield different results.
| From: | salathe@php.net | Date: | Tue, 26 Apr 2011 19:30:17 +0000 |
| Subject: | Doc #54549 [Opn->Csd]: Using $variable--; and $variable -= '1'; yield different results. | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-6293@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=54549&edit=1
ID: 54549
Updated by: salathe@php.net
Reported by: account22a23 at hotmail dot com
Summary: Using $variable--; and $variable -= '1'; yield
different results.
-Status: Open
+Status: Closed
Type: Documentation Problem
Package: *Programming Data Structures
Operating System: Fedora 14
PHP Version: 5.3.6
-Assigned To:
+Assigned To: salathe
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2011-04-26 21:29:42] salathe@php.net
Automatic comment from SVN on behalf of salathe
Revision: http://svn.php.net/viewvc/?view=revision&revision=310528
Log: decrementing, or incrementing non-A-Za-z, string variables has no
effect (doc #54549)
------------------------------------------------------------------------
[2011-04-17 15:05:59] 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-17 14:20:55] account22a23 at hotmail dot com
Code was run from command line.
------------------------------------------------------------------------
[2011-04-17 14:19:54] hotmail22a23 at hotmail dot com
Code was run from command line.
------------------------------------------------------------------------
[2011-04-17 14:03:37] 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) ...
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=54549&edit=1