Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP difference between int and integer

Tags:

Is there any difference between int and integer in PHP?

Which is the newer or more recommended use?

$a = (int)"3 euros";
echo $a;  // $a==3

$a = (integer)"3 euros";
echo $a; // $a==3
like image 446
Freeman Avatar asked May 17 '12 13:05

Freeman


2 Answers

The difference arises when we use type hinting from php 7.0+

this is valid

function getId(): int 
{
 return $id;
}

this is not

function getId(): integer
{
 return $id;
}

the second one will expect you to return an object of a 'class integer', which will cause a strange sentence:

Uncaught TypeError: Return value of getId() must be an instance of integer, integer returned in ...
like image 193
hvertous Avatar answered Sep 18 '22 08:09

hvertous


No.

They are the same, they both cast the value to an integer, one is just terser by four characters.

Source.

like image 34
alex Avatar answered Sep 17 '22 08:09

alex