PHP basic type

Source: Internet
Author: User
Eight primitive types, four scalar types
  • Boolean (boolean)
  • Integer)
  • Float (float type, also "double ")
  • String (string)

Two composite types:

  • Array)
  • Object)

There are two special types:

  • Resource)
  • NULL
<? 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;
?>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.