1

I have access to two different servers where I'm running the same code:

<?php
   $foo = '#f12345';
   if (isset($foo['acf'])) { 
      echo "acf is set: " . $foo['acf']; 
   } else { 
      echo 'acf not set'; 
   }
?>

On one server, running PHP 5.6.21, the result is:

acf is set: f

On the other server, running PHP 5.6.25, the result is:

Warning: Illegal string offset 'acf' in...

I tried but failed to find an explanation online for this behaviour. Would it be a PHP.ini setting?

It seems that, in the server running PHP 5.6.21, the code is being interpreted as a regex search for either a, c, or f (I changed the "f" in $foo to both "a" and "c" and got similar results) and not a search for the index "acf" of an associative array.

Can someone shed some light on why this happens?

Thanks.

Dentra Andres
  • 371
  • 1
  • 7
  • 18

1 Answers1

0

According to the documentation the behaviour of accessing strings through offsets changed in version 5.4. Before version 5.4 string based offsets like "foo" was silently converted to 0, while after version 5.4 a warning is thrown. It looks like you have found a bug in that change, where 5.6.21 has the old behaviour, and 5.6.25 has the new behaviour. There seems to be a slight variance though, as the character # is really in position [0]

Documentet here in the official docs : PHP.NET Language Types Strings Scroll down to "String access and modification by character"

As of PHP 5.4 string offsets have to either be integers or integer-like strings, otherwise a warning will be thrown. Previously an offset like "foo" was silently cast to 0.

Canis
  • 4,130
  • 1
  • 23
  • 27
  • There's no difference between single and double quotes unless you have variables or escape sequences inside. – Barmar Aug 08 '17 at 16:42
  • See https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php – Barmar Aug 08 '17 at 16:42
  • @Barmar Corrected my answer based on documentation. – Canis Aug 08 '17 at 17:14
  • @Barmar If you still feel my answer is wrong now, please inform me of what the problem is so I can fix it. If not, remove your downvote. – Canis Aug 09 '17 at 06:38
  • Downvote removed. Can you add a link to the documentation you're referring to? – Barmar Aug 09 '17 at 07:14
  • @Barmar Done. Also added a block quote from the relevant section. – Canis Aug 09 '17 at 07:26