PHP Data Types | Tutorial

preview_player
Показать описание
Course Title: Web Development Masterclass

Tutorial Content:

We can store variable data into different types of variables. Each variable data type has its own unique characteristics and can be used for different things.

In this lesson we will explore the following data types:

String
Integer
Float
Array
NULL
Boolean
Object
In this first example, we've declared a variable x and assigned the value Hello World. This is an example of a string variable because the value is a sequence of characters.

We can declare a string variable using single or double quotes.

The second example is identical, only it uses single quotes.

An integer data type is a non-decimal number.

There are certain rules for an integer:

An integer must have at least one digit.
An integer must not have a decimal point.
An integer can be either positive or negative.
Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)
In this example, we have declared a variable called num as an integer with a value set to 6000.

Notice that the value is not enclosed in quotes. Integer variables do not require quotes.

Next we have used the var_dump statement to output the data type and value.

In our browser we can see variable num is an integer with the value 6000.

A float - also known as a floating number point, is a number that is in exponential form or that contains a decimal point.

In this example we have declared variable num2 and assigned the value 10.365. Since the value contains a decimal, our var_dump statement will output the datatype as float.

An array variable can store multiple values.

In this example we have declared the variable sports as an array with four values assigned: Soccer, Tennis, Baseball, and Football.

Using our var_dump statement we can output each value in the array. We will be working much more with arrays later in this course.

The PHP Null value is a data type that can only have one value, null.

The null value type means the variable has no value assigned to it.

We can also empty the value of an existing variable by setting the variable value to null.

In this example, we have the variable color, with the value assigned to blue.

We can empty this value, by setting the variable to null.

We can confirm the variable is null by using the var_dump statement.

A Boolean variable can only hold two values, true or false. Booleans are used primarily in conditional statements such as if, else and elseif. We will be exploring conditional statements later in this course.

As a general rule, the following values would be considered false:

A Boolean that is set to false, as show in this example.
The integer 0 (zero)
The float 0.0 (zero)
The empty string, and the string "0"
An array with zero elements
The special type NULL (including unset variables)
Any other value would be considered true.
With a couple of exceptions, almost all other values would be considered true.
Рекомендации по теме
visit shbcf.ru