php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63632 $var =. 'val';
Submitted: 2012-11-28 09:57 UTC Modified: 2012-11-28 14:27 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: bensor987 at neuf dot fr Assigned:
Status: Wont fix Package: Variables related
PHP Version: Irrelevant OS: All
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: bensor987 at neuf dot fr
New email:
PHP Version: OS:

 

 [2012-11-28 09:57 UTC] bensor987 at neuf dot fr
Description:
------------
Why not add  "$var =. 'val';" syntax ? 

It would concatenate 'val' before $var.

Actually, you have to do this
<?php
$var = 'val' . $var;
?>

My suggestion make the code shorter :

<?php
$var =. 'val';
?>

Test script:
---------------
<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"
$c = "World! " . $a; // now $c contains "World! Hello "

$a = $b = "Hello ";
$a .= "World!";     // now $a contains "Hello World!"
$b =. "World! ";     // now $b contains "World! Hello "
?>



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-11-28 10:57 UTC] inefedor at gmail dot com
Collides with:
$asd =.4;
echo $asd;

Output should be: 0.4
 [2012-11-28 11:07 UTC] bensor987 at neuf dot fr
Erm. Maybe we could use another syntax for this suggestion ?
 [2012-11-28 11:16 UTC] inefedor at gmail dot com
I think the main concern with "preconcatenation" is 100% chance of reallocation of memory. If it will be part of syntax - people would use it often, but it's something you should avoid using of.

Try to execute this code:
$c = 10000;
$b = 200;

$t1 = microtime(1);

$str1 = "";
for ($i = 0; $i < $c; ++$i) {
    $str1 = str_repeat(mt_rand(0, 9), $b) . "\n" . $str1;
}
$t1 = microtime(1) - $t1;

$t2 = microtime(1);
$strArr = array();
for ($i = 0; $i < $c; ++$i) {
    $strArr[] = str_repeat(mt_rand(0, 9), $b);
}
$strArr = implode("\n", array_reverse($strArr));
$t2 = microtime(1) - $t2;

echo $t1 . "\n" . $t2 . "\n";


First part of this code does exactly the same as second part, but it's 300 times slower.
 [2012-11-28 14:27 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2012-11-28 14:27 UTC] nikic@php.net
This seems to be a rather rare use case (compared to appending) and would probably also start a move to add the other operators in this same version (so we have not only %= but also =% etc). And taken together with the =.4 issue already mentioned, I really don't think that it's worth it.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC