Home > Backend Development > PHP Problem > What are the basic data types in php

What are the basic data types in php

silencement
Release: 2023-02-24 18:14:01
Original
3225 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

<?php 
$x = "Hello world!";
echo $x;
echo "<br>"; 
$x = &#39;Hello world!&#39;;
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

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

<?php 
$x = 10.365;
var_dump($x);
echo "<br>"; 
$x = 2.4e3;
var_dump($x);
echo "<br>"; 
$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

<?php 
$cars=array("Volvo","BMW","SAAB");
var_dump($cars);
?>
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

<?php
class Car
{
  var $color;
  function Car($color="green") {
    $this->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:

实例
<?php
$x="Hello world!";
$x=null;
var_dump($x);
?>
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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template