Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP 5 data type


May 10, 2021 PHP


Table of contents


PHP 5 data type

Although PHP is a weak type language and does not need to indicate its data type when declaring and using variables, you should also understand its data type!

String (string), Integer (integer), Float (floating-point type), Boolean (Boolean type), Array (array), Object (object), NULL (empty value).


PHP string

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

You can put any text in single and double quotes:

<?php

$x = " Hello world! " ;

echo $x ; echo " <br> " ;

$x = ' Hello world! ' ;

echo $x ;

?>


Try it out . . .


PHP integer

An integer is a number without a small number.

Integer rules:

  • The integer must have at least one number (0-9)
  • Integers cannot contain commas or spaces
  • Integers are not indesolytes
  • Integers can be positive or negative
  • Integers can be specified in three formats: hex, hex (prefixed with 0x), or octal (prefixed 0).

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

<?php
$x = 5985;
var_dump($x);
echo "<br>";
$x = -345; N egative
var_dump($x);
echo "<br>";
$x = 0x8C; H ens
var_dump($x);
echo "<br>";
$x = 047; O ctal number
var_dump($x);
?>

Try it out . . .


PHP floating-point type

Floating points are numbers with small parts, or exponential forms.

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

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

Try it out . . .


PHP Boolean type

Boolean can be TRUE or FALSE.

$x=true;
$y=false;

Boolean type is usually used for conditional judgment. You'll learn more about condition control tutorials in the next chapters.


PHP array

Arrays can store multiple values in a variable.

An array is created in the following instance, and then the PHP var_dump() function returns the data type and value of the array:

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

Try it out . . .

You'll learn more about arrays in the next chapters.


PhP object

Object data types can also be used to store data.

In PHP, objects must be declared.

First, you must declare class objects using the class keyword. A class is a structure that can contain properties and methods.

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

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

Try it out . . .

The PHP keyword this in the above example is a pointer to the current object instance and does not point to any other object or class.

You will learn more about objects in the next chapters.


PHP NULL value

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

The NULL value indicates whether a variable is an empty value. The same can be used for the difference between a data null value and a NULL value.

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

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

Try it out . . .

The above is the commonly used data types in PHP, refer to the examples given in this section to familiarize yourself with the use of these data types, which is helpful for future learning!