Jump to content
StevenOliver

Arrays: concatenation operator .= vs =

Recommended Posts

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

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.