PHP Basic Tutorial-data types

Source: Internet
Author: User
Tags getcolor type null

PHP supports eight primitive types (type).

Four types of scalar:

String (String)

Integer (integer type)

Float (floating point type, also double)

Boolean (Boolean)

Two kinds of composite types:

Array (arrays)

Object (Objects)

Two special types:

Resource (resources)

Null (empty)

How to view variable types

The GetType () function makes it easy to see the type of a variable:

<?php$var_bool = TRUE; A boolean$var_str = "foo"; a string$var_int = 12; An Integerecho GetType ($var _bool); Output Booleanecho GetType ($var _str); Output Stringecho GetType ($var _int); Output integer?>

Tips

For historical reasons, if the data is float type, the GetType () function returns a double instead of float.

If you want to see the value and type of an expression, use the Var_dump () function.

If you want to cast a variable to a type, you can use a cast or settype () function on it.

PHP string

The string is a sequence of characters, such as "Hello world!".

The string can be any text within quotation marks. You can use single or double quotation marks:

Instance
1234567 <?php $x"Hello anxia.com!";echo $x;echo "<br>"$x ‘Hello anxia.com!‘;echo$x;?>
PHP integer

Integers are numbers that have no decimals.

Integer rule:

    • Integers must have at least one number (0-9)

    • Integers cannot contain commas or spaces

    • Integers cannot have decimal points

    • Integers can be both positive and negative

    • Integers can be specified in three formats: decimal, hexadecimal (prefix 0x), or octal (prefix 0)

In the example below, we will test for different numbers. PHP Var_dump () returns the data type and value of the variable:

Instance
12345678910 <?php $x= 5985;var_dump($x);echo"<br>"$x = -345; // 负数var_dump($x);echo"<br>"$x= 0x8C; // 十六进制数var_dump($x);echo"<br>";$x= 047; // 八进制数var_dump($x);?>
PHP floating-point number

Floating-point numbers are digits that have decimal or exponential form.

In the example below, we will test for different numbers. PHP Var_dump () returns the data type and value of the variable:

Instance
12345678910 <?php  $x  = 10.365; var_dump ( $x echo   "<br>" ;&NBSP; $x  =&NBSP;2.4E3; var_dump ( $x echo   "<br>" ;&NBSP; $x  = 8e-5; var_dump ( $x ?>
PHP Logic/Boolean type

The logic is true or false.

12 $x=true;$y=false;

Logic is often used for conditional testing. You'll learn more about conditional testing in the chapters later in this tutorial.

PHP arrays

An array stores multiple values in a variable.

In the following example, we will test different arrays. PHP Var_dump () returns the data type and value of the variable:

Instance
123456789 <?php $cars=array("Volvo","BMW","SAAB");var_dump($cars);?>$myarr=array("one"=>"first","two"=>"second");foreach($myarras$key=>$value){    echo"Key:".$key."<br>"."Value:".$value."<br>";}

You'll learn more about arrays in the chapters later in this tutorial.

PHP Object

Objects are data types that store data and information about how to work with the data.

In PHP, you must explicitly declare an object.

First we must declare the class of the object. For this, we use the class keyword. A class is a structure that contains properties and methods.

We then define the data type in the object class, and then use this data type in an instance of the class:

Instance
12345678910111213 <?phpclassCar{    var$color;    functionCar($color="black"){        $this->color=$color;    }    function getcolor(){        return$this->color;    }}$aa=newCar();echo$aa->getcolor();?>

You will learn more about objects in the chapters later in this tutorial.

PHP NULL Value

A special NULL value indicates that the variable has no value. Null is the only possible value for the data type NULL.

The null value indicates whether the variable is empty. Also used to differentiate between empty strings and null-valued databases.

You can empty the variable by setting the value to NULL:

Instance
<?php$x= "Hello world!"; $x =null;var_dump ($x);? >

PHP Basic Tutorial-data types

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.