1

On PHP7 we know that a variable has to be inizialized with its type, differnet from where we did on php5 that the type was changed according the value set.

If we test this code

  <?php

  /* EAMPLE A */
  $tLisTim="";
  $i=0;
  $tLisTim[$i]=100;
  $i=$i+1;
  $tLisTim[$i]=200;

  var_dump("A");
  var_dump($tLisTim);

  /* EAMPLE B */
  $tLisTim=[];
  $i=0;
  $tLisTim[$i]=100;
  $i=$i+1;
  $tLisTim[$i]=200;

  var_dump("B");
  var_dump($tLisTim);

  ?>  

we will get this results:

  PHP 5.6

  string 'A' (length=1)
  array (size=2)
    0 => int 100
    1 => int 200

  string 'B' (length=1)
  array (size=2)
    0 => int 100
    1 => int 200


  PHP 7.1

  string 'A' (length=1)
  string '12' (length=2)

  string 'B' (length=1)
  array (size=2)
    0 => int 100
    1 => int 200

The problem is that in PHP7 there is not warning to help us to migrate correctly all this differences.

How can I detect when we try to use a variable with an incorrect type ?

Thanks,

6
  • I would say is_* where * is the type you would expect ? like php.net/manual/en/function.is-array.php. But to be honest why do you have in your code an empty string and expect to change it into array ? Commented Feb 11, 2019 at 17:36
  • 1
    These are probably either notices or warnings, so may not throw an error depending on your error suppressions. Try enabling warning/notice errors in your test environment while migrating to php7. Commented Feb 11, 2019 at 17:43
  • Weird, that discrepancy appears to have been introduced in PHP >= 7.1 specifically Commented Feb 11, 2019 at 17:47
  • @aynber I have all errors and notices enabled and I don't get any messages Commented Feb 11, 2019 at 17:50
  • @MonkeyZeus Ah, good to know. It was a guess, since it wasn't something I had run into before. Commented Feb 11, 2019 at 17:53

1 Answer 1

1

There is no warning because PHP 7.1 did not remove support for your code structure:

String modification by character on an empty string now works like for non-empty strings, i.e. writing to an out of range offset pads the string with spaces, where non-integer types are converted to integer, and only the first character of the assigned string is used. Formerly, empty strings where silently treated like an empty array.

http://php.net/manual/en/migration71.incompatible.php#migration71.incompatible.empty-string-modifcation-by-character

This is still perfectly valid:

$tLisTim="";
$tLisTim[0]=100;
$tLisTim[1]=200;

but it behaves differently than before.

Instead of silently converting to an array it sets the first position of the string to 1 and the second position to 2.

This will throw an error:

$tLisTim="";
$tLisTim[]=100;

This will throw a warning:

$tLisTim="";
$tLisTim['g']=100;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.