23

I tried looking up (int) but could only find documentation for the function int() in the PHP manual.

Could someone explain to me what the above code does, and exactly how it works?

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
Simon Suh
  • 10,599
  • 25
  • 86
  • 110

9 Answers9

22

You can find it in the manual in the section type juggling: type casting. (int) casts a value to int and is a language construct, which is the reason that it looks "funny".

jackel414
  • 1,636
  • 2
  • 13
  • 29
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
22

It convert (tries at least) whatever the value of the variable is to a integer. If there are any letter etc, in front it will convert to a 0.

<?php

$var = '1a';

echo $var;               // 1a
echo (int) $var;     //1

$var2 = 'a2';
echo $var2;           //a2
echo (int) $var2;     // 0

?>

John
  • 2,900
  • 8
  • 36
  • 65
3

Simple example will make you understand:

var_dump((int)8);
var_dump((int)"8");
var_dump((int)"6a6");

var_dump((int)"a6");

var_dump((int)8.9);
var_dump((int)"8.9");
var_dump((int)"6.4a6");

Result:

int(8)
int(8)
int(6)

int(0)

int(8)
int(8)
int(6)
Cas Bloem
  • 4,846
  • 2
  • 24
  • 23
3

(int) converts a value to an integer.

<?php
$test = "1";
echo gettype((int)$test);
?>

$ php test.php
integer
Griffin
  • 644
  • 6
  • 18
2

In PHP, (int) will cast the value following it to an int.

Example:

php > var_dump((int) "5");
int(5)

I believe the syntax was borrowed from C.

Adam Wagner
  • 15,469
  • 7
  • 52
  • 66
2

What you are looking at there is known as type casting - for more information, see the manual page on type juggling.

The above piece of code casts (or converts) $_GET['page'] to an integer.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
1

it casts the variable following it to integer. more info from documentation: http://php.net/manual/en/language.types.type-juggling.php

Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast.

The casts allowed are:

  • (int), (integer) - cast to integer
  • (bool), (boolean) - cast to boolean
  • (float), (double), (real) - cast to float
  • (string) - cast to string
  • (array) - cast to array (object) - cast to object
  • (unset) - cast to NULL
Community
  • 1
  • 1
ant7
  • 421
  • 3
  • 6
1

this kind of syntax (int) is called type casting. Basically it takes the variable following it and tries to force it into being an int

Brian Glaz
  • 15,468
  • 4
  • 37
  • 55
1

(int) is same as int()

see http://php.net/manual/en/language.types.integer.php

Dmytro
  • 114
  • 4
  • This is not entirely true: `(int)` is a type cast (see: http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting) while `int()` doesn't even exist. I think you're refering to `intval()` which itself is a function and thus takes a little bit longer to execute than the type cast and offers the ability to provide a base of conversion. There are other differences discussed in this answer: https://stackoverflow.com/questions/1912599/is-there-any-particular-difference-between-intval-and-casting-to-int-int-x. – flu Jun 13 '18 at 07:56