PHP complete se...login
PHP complete self-study manual
author:php.cn  update time:2022-04-15 13:53:54

PHP data types



What are the data types of php?

php data types include String (string), Integer (integer), Float (floating point) Type), Boolean (Boolean), Array (array), Object (object), NULL (null value).


PHP String

A string is a sequence of characters, like "Hello world!".

You can put any text in single and double quotes:

Example

<?php 
$x = "Hello world!";
echo $x;
echo "<br>"; 
$x = 'Hello world!';
echo $x;
?>

Run Example»

Click the "Run Example" button to view the online example


PHP Integer

An integer is a number without decimals.

Integer rules:

  • The integer must have at least one digit (0-9)

  • The integer cannot contain commas or spaces

  • Integers have no decimal point

  • Integers can be positive or negative numbers

  • Integers can Specify in three formats: decimal, Hexadecimal (prefixed by 0x) or octal (prefixed by 0).

In the following examples we will test different numbers. PHP var_dump() The function returns the data type and value of the variable:

Instance

<?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);
?>

Run Instance»

Click the "Run Instance" button View online examples


PHP Floating point type

Floating point numbers are numbers with a decimal part, or in exponential form.

In the following examples we will test different numbers. PHP var_dump() The function returns the data type and value of the variable:

Instance

<?php 
$x = 10.365;
var_dump($x);
echo "<br>"; 
$x = 2.4e3;
var_dump($x);
echo "<br>"; 
$x = 8E-5;
var_dump($x);
?>

Run Instance»

Click the "Run Instance" button View online examples


PHP Boolean type

Boolean type can be TRUE or FALSE.

$x=true;
$y=false;

Boolean type is usually used for conditional judgment. You will learn more about conditional control in the following chapters.


PHP Array

Arrays can store multiple values ​​in one variable.

An array is created in the following example, Then use PHP var_dump() The function returns the data type and value of the array:

Instance

<?php 
$cars=array("Volvo","BMW","Toyota");
var_dump($cars);
?>

Run Instance»

Click the "Run Instance" button View online examples

You will learn more about arrays in the following chapters.


PHP Object

The object data type can also be used to store data.

In PHP, objects must be declared.

First, you must declare a class object using the class keyword. Classes are structures that can contain properties and methods.

Then we define the data type in the class and then use the data type in the instantiated class:

Instance

<?php
class Car
{
    var $color;
    function Car($color="green") {
      $this->color = $color;
    }
    function what_color() {
      return $this->color;
    }
}

function print_vars($obj) {
   foreach (get_object_vars($obj) as $prop => $val) {
     echo "    $prop = $val
";
   }
}

// instantiate one object
$herbie = new Car("white");

// show herbie properties
echo "herbie: Properties
";
print_vars($herbie);

?>

Run Example»

Click the "Run Example" button to view the online instance

In the above example, the PHP keyword this is a pointer to the current object instance and does not point to any other object or kind.

You will learn more about objects in the following chapters.


PHP NULL value

The NULL value indicates that the variable has no value. NULL is a value of data type NULL.

The NULL value indicates whether a variable has a null value. It can also be used to distinguish between data null values ​​and NULL values.

You can clear variable data by setting the variable value to NULL:

Instance

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

Click the "Run Instance" button to view the online instance

php.cn