Jump to content
StevenOliver

Arrays: concatenation operator .= vs =

Recommended Posts

Is there a subtle difference between
$array[] = 'item';
vs
$array[] .= 'item';

Thank you.

Share this post


Link to post
Share on other sites

since the $array[] syntax always appends a new element to the array, it won't contain anything to concatenate to, so, the use of the . has no meaning and is probably producing a php undefined error. what sort of error, symptom, or problem are you having that causes you to ask this?

Share this post


Link to post
Share on other sites
Posted (edited)

Thank you!!
I saw an answer on the net using $array[] .= and I had never seen the concatenation operator used with arrays (the answer had lots of upvotes, too), so I thought I'd look into it further before changing my own code :-)

Edited by StevenOliver

Share this post


Link to post
Share on other sites

It would only make sense if the "[ ]" contained a key value.

$array = [ 'First', 'Second', 'Third'];
$array[0] .= " item";                      // now you can use the concatenation operator

echo '<pre>', print_r($array, 1), '</pre>';
 /* OUTPUTS
         Array
        (
            [0] => First item
            [1] => Second
            [2] => Third
        )
*/

However,

$array[] .= " item";        // surprisingly it works!

 

Share this post


Link to post
Share on other sites
Posted (edited)

Just a reminder, in PHP 7 with strict_types, the second parameter of print_r MUST be a boolean and not an integer or it will fail with a fatal error.

Correct

<?php declare(strict_types=1); 
echo '<pre>', print_r($array, true), '</pre>';  

Fails Fatal Error

<?php declare(strict_types=1); 
echo '<pre>', print_r($array, 1), '</pre>';  

Fatal error: Uncaught TypeError: print_r() expects parameter 2 to be boolean, integer given
TypeError: print_r() expects parameter 2 to be boolean, integer given 

 

Quote

surprisingly it works!

It does "work" but it will give you an Undefined variable notice. It doesn't matter if it is an array or not.

Edited by benanamen

Share this post


Link to post
Share on other sites

Does anyone else find it ironic that you can't

declare(strict_types=true);

 

Share this post


Link to post
Share on other sites
47 minutes ago, Barand said:

Does anyone else find it ironic that you can't


declare(strict_types=true);

 

According to Andrea (the original author of the scalar type hints RFC where strict_types came from),

Quote

I originally had it be a boolean, but considering how often this would be used, I changed it to 1 for terseness. Using 1 is common for INI settings, so I don’t think it’d be that confusing,

Shrug.

Share this post


Link to post
Share on other sites
14 minutes ago, requinix said:

but considering how often this would be used

... or how little?

Share this post


Link to post
Share on other sites
18 minutes ago, Barand said:

... or how little?

Either you use it everywhere or you don't use it at all. I'm in the latter camp: I like the automatic conversions, and I'm already aware of and careful with value types.

I would probably opt-in if I could do it globally, but there were good reasons not to support that and to only make it per-file.

Share this post


Link to post
Share on other sites
5 hours ago, Barand said:

Does anyone else find it ironic that you can't


declare(strict_types=true);

 

Well..., if that isn't a "how about that" LOL. I get the explanation by Andrea, but still.......

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.