What are the basic data types in php

silencement
Release: 2023-02-24 18:14:01
Original
2976 people have browsed it

What are the basic data types in php

The data types in PHP include strings, integers, floating point numbers, logic, arrays, objects, and NULL. Each type is explained below.

PHP String

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

A string can be any text within quotes. You can use single or double quotes:

Example

"; 
$x = 'Hello world!';
echo $x;
?>
Copy after login

PHP Integers

Integers are numbers without decimals.

Integer rules:

Integers must have at least one digit (0-9)

Integers cannot contain commas or spaces

Integers cannot have decimal points

Integers can be positive or negative

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

In the following In the example, we will test different numbers. PHP var_dump() will return the data type and value of the variable:

Example

"; 
$x = -345; // 负数
var_dump($x);
echo "
"; $x = 0x8C; // 十六进制数 var_dump($x); echo "
"; $x = 047; // 八进制数 var_dump($x); ?>
Copy after login

PHP floating point number

Floating point number has decimal point or exponential form number.

In the example below we will test different numbers. PHP var_dump() will return the data type and value of the variable:

Instance

"; 
$x = 2.4e3;
var_dump($x);
echo "
"; $x = 8E-5; var_dump($x); ?>
Copy after login

PHP logic

The logic is true or false.

$x=true;
$y=false;
Copy after login

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

PHP Array

Array stores multiple values ​​in one variable.

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

Instance

Copy after login

PHP Object

The object is to store data and how to process the data The data type of the information.

In PHP, objects must be declared explicitly.

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

Then we define the data type in the object class and then use this data type in the instance of that class:

Instance

color = $color;
  }
  function what_color() {
    return $this->color;
  }
}
?>
Copy after login

PHP NULL value

The 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 distinguish empty strings from null value databases.

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

实例
Copy after login

The above is the detailed content of What are the basic data types in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!