php typeof code example

Example 1: typeof php

gettype($var);

Example 2: php get type of object

gettype($object);

Example 3: find type in php

gettype($u)

Example 4: getttype php

gettype ( mixed $var ) : string

gettype ("SALUT") => string
gettype (2) => integer    
  ...etc ...
  
"bool"
"integer"
"double"
"array"
"object"
"resource"
"NULL"
"unknown type"

Example 5: check type in php

gettype ( mixed $var ) : string

Example 6: how work instanceof php method

class MyClass {
}

$o1 = new MyClass();
$o2 = new MyClass();
$name = 'MyClass';

// in the cases below, $a gets boolean value true
$a = $o1 instanceof MyClass;
$a = $o1 instanceof $name;
$a = $o1 instanceof $o2;

// counter examples:
$b = 'b';
$a = $o1 instanceof 'MyClass'; // parse error: constant not allowed
$a = false instanceof MyClass; // fatal error: constant not allowed
$a = $b instanceof MyClass;    // false ($b is not an object)

Tags:

Html Example