add a note add a note

User Contributed Notes 5 notes

up
-18
ohcc at 163 dot com
4 months ago
There is a type trap in PHP: false is less than any negative figure.
<?php
    var_dump
(-1>false);
?>
up
-20
shahnaz khan
9 years ago
if we use gettype() before initializinf any variable it give NULL
for eg.

<?php
$foo
;
echo
gettype($foo);
?>

it will show

NULL
up
-22
arjini at gmail dot com
8 years ago
Note that you can chain type castng:

var_dump((string)(int)false); //string(1) "0"
up
-29
shaharia dot azam at gmail dot com
11 months ago
<?php

/*  delcare few variable to check their data type */
$intVar = 10;   //Integar variable

$floatVar = 2.49;   //Float type aka Double

$stringVar = "There are 8 data types in PHP";   // String

$arrayVar = array(
   
'Boolean',
   
'Integar',
   
'Float aka Double',
   
'String', 'Array',
   
'Object',
   
'Resource',
   
'Null'
);  // Array type

$booleanVar = true; // Boolean type

// Make an empty class to check object type
class obj{

}

$objectVar = new obj();

// To check resource type
$resource = mysql_connect();

// Null value
$nullVar = null;

// output the data type of each variable
echo gettype($intVar);
echo
"\n";
echo
gettype($floatVar);
echo
"\n";
echo 
gettype($stringVar);
echo
"\n";
echo
gettype($arrayVar);
echo
"\n";
echo
gettype($booleanVar);
echo
"\n";
echo
gettype($objectVar);
echo
"\n";
echo
gettype($resource);
echo
"\n";
echo
gettype($nullVar);
echo
"\n";
up
-36
Jeffrey
5 years ago
The Object (compound) Type

Like every programming language, PHP offers the usual basic primitive types which can hold only one piece of data at a time (scalar). I am particularly fond of the "object" type (compound) because that allows me to group many basic PHP types together, and I can name it anything I want.

<?php
class Person
{
 
$firstName;                   // a PHP String
 
$middleName;                  // a PHP String
 
$lastName;                    // a PHP String
 
$age;                         // a PHP Integer
 
$hasDriversLicense;           // a PHP Boolean
}
?>

Here, I have grouped several basic PHP types together, (3) Strings, (1) Integer, and (1) Boolean... then I named that group "Person". Since I used the proper syntax to do so, this code is pure PHP, which means that if you run this code, you would have an extra PHP "type" available to you in your scripts, like so:

<?php
$myAge
= 16;                    // a PHP Integer - always available
$yourAge = 15.5;                // a PHP Float   - always available
$hasHair = true;                // a PHP Boolean - always available
$greeting = "Hello World!"      // a PHP String  - always available

$person = new Person();         // a PHP Person  - available NOW!
?>

You can make your own object types and have PHP execute it as if it were part of the PHP language itself. See more on classes and objects in this manual at: http://www.php.net/manual/en/language.oop5.php
To Top