-1

I don't know why the code below giving an error on my laptop while not at my friend's.

<?php
    function myfunction() : int {
        return 10;
    }
    echo myfunction();
?>

Error

Parse error: syntax error, unexpected ':', expecting '{' in (my location) on line 2.

If I remove the ": int" on line 2 everything is fine, but can someone explain why this code can't run on mine?

GYaN
  • 2,327
  • 4
  • 19
  • 39
Ulais
  • 1
  • 1

1 Answers1

0

Please read the documentation. It is a PHP7+ only feature.

What might be a good idea as a work around, until you migrate to PHP7, is to do the following:

function myfunction() {
    return (int)10;
}

var_dump(myfunction());

That will convert the return to an integer.


It's worth noting, this won't throw any warnings if the return value cannot be resolved.
I.E. If you passed parameters and those parameters were letters in a string, for instance, you'd get Warning: A non-numeric value encountered. However, for now, I think the above solution will suffice.

I strongly recommend upgrading to the latest version of PHP, though.

JustCarty
  • 3,839
  • 5
  • 31
  • 51