Eight primitive types, four scalar types
- Boolean (boolean)
- Integer)
- Float (float type, also "double ")
- String (string)
Two composite types:
There are two special types:
<? Php
$ Bool = true;
$ Str = "str ";
$ Int = 1;
$ Float = 1.987;
$ Arr = array ("a" => "123 ");
Echo "<p class = 'red'> basic type </p> ";
Echo gettype ($ bool );
Echo '<br> ';
Echo gettype ($ str );
Echo '<br> ';
Echo gettype ($ int );
Echo '<br> ';
Echo gettype ($ float );
Echo '<br> ';
Echo gettype ($ arr );
Echo '<br> ';
?>
Boolean Type
<? Php
/* Bool */
Echo "<p class = 'red'> Boolean Type </p> ";
Var_dump (bool )'');
Var_dump (bool) 0 );
Var_dump (bool)-1 );
Var_dump (bool) 1 );
Var_dump (bool) 1.25 );
Var_dump (bool) array (12 ));
Var_dump (bool) array ());
Var_dump (bool) "false ");
Echo '<br> ';
If (is_int ($ int ))
{
$ Int + = 10;
Echo $ int;
Echo '<br> ';
}
Echo $ arr ["a"];
Echo '<br> ';
?>
Integer
<? Php
/* Int */
Echo "<p class = 'red'> integer </p> ";
$ Inta = 1000;
$ Intb =-111;
$ Intc = 0123;
$ Intd = 0x1A;
Var_dump ($ inta );
Var_dump ($ intb );
Var_dump ($ intc );
Var_dump ($ intd );
Var_dump (27/7 );
Var_dump (int) (27/7 ));
Var_dump (round (27/7 ));
/* Float */
Echo "<p class = 'red'> floating point </p> ";
?>
String
<? Php
/* String */
Echo "<p class = 'red'> string </p> ";
Echo 'use single quotation marks \'';
Echo '<br> ';
Echo '1 \ n \ r 2 ';
Echo '<br> ';
Echo "1 \ n \ r2 ";
Echo '<br> ';
$ Tmpstr = 'tmpstring ';
Echo '$ tmpstr is string ';
Echo '<br> ';
Echo "$ tmpstr is string";/* replace variable name in double quotation marks */
?>
Array
<? Php
/* Array */
Echo "<p class = 'red'> array </p> ";
$ Arrtmp = array ("foo" => "bar", 12 => true );
Echo $ arrtmp ["foo"];
Echo '<br> ';
Echo $ arrtmp [12];
Echo '<br> ';
$ Arrtmp ["a"] = "";
$ Arrtmp [12] = "12 ";
Echo $ arrtmp ["a"];
Echo '<br> ';
Echo $ arrtmp [12];
Unset ($ arrtmp [12]);
Echo '<br> ';
Foreach ($ arrtmp as $ I)
{
Echo "arrtmp-". $ I. "\ r \ n ";
}
$ Array = array (1, 2, "a", "c ");
$ Count = count ($ array );
For ($ I = 0; $ I <$ count; $ I ++ ){
Echo "Good:". $ array [$ I]. "\ n ";
}
?>
Object
<? Php
Class foo
{
Function do_foo ()
{
Echo 'hello ';
}
}
$ F = new foo ();
$ F-> do_foo ();
?>
Resource: a reference that is saved to an external resource.
<? Php
$ Fp = fopen ("foo", "w ");
Echo "\ $ fp type". get_resource_type ($ fp). "\ n ";
?>
NULL: the variable is considered NULL in the following cases.
The value is NULL.
Not assigned
Unset ()
<? Php
$ Var = NULL;
Echo $ var;
$ Var2;
Echo $ var2;
$ Var3 = 123;
Echo $ var3;
Unset ($ var3 );
Echo $ var3;
?>