Основные типы данных
Типы данных PHP используются для хранения различных типов данных или значений.
PHP поддерживает 8 примитивных типов данных, которые можно разделить на 3 типа:
- Скалярный тип
- Смешанный тип
- Специальный тип
Скалярный тип
В PHP есть 4 скалярных типа данных:
- boolean(булевые)
- integer(целочисленные)
- float(числа с плавающей точкой)
- string(символические или строковые)
PHP Boolean
Эти типы данных обеспечивают вывод в виде 0 или 1.
В PHP значение True равно единице, а значение False равно нулю.
Синтаксис
<?php
$foo = True; // assign the value TRUE to $foo
?>
Example 1:
<?php
$a=true;
echo $a;
?>
Output:
1
функция is_bool()
Используя эту функцию, мы можем проверить,
является ли переменная логической или нет.
bool is_bool ( mixed $var )
Возвращает True, если значение $var логическое, иначе False.
Example 1
<?php
$x=false;
echo is_bool($x);
?>
Output:
1
Example 2
<?php
$y=false;
if (is_bool($y))
echo 'This is a boolean type.';
else
echo 'This is not a boolean type.';
?>
Output:
This is a boolean type.
PHP is_int() function
By using this function, we can check the input variable is integer or not. This function was introduced in PHP 4.0.
Syntax
bool is_int (mixed $var)
Parameters
Parameter Description Is compulsory
var_name The variable being checked. compulsory
Return type
This function returns true if var_name is an integer, Otherwise false.
Example 1
Output:
1
Example 2
Output:
56 is not an Integer
Example 3
Output:
12345 is an int!
PHP Float
This data type represents decimal values. The float (floating point number) is a number with a decimal point or a number in exponential form.
Syntax
$a=1.234;
$x=1.2e4;
$y=7E-10;
Example 1
Output:
22.41
Example 2
Output:
11.365
Example 3
Output:
float 6.203
float 23000
float 7.0E-10
PHP is_float Function
By using this function, we can check that the input value is float or not. This function was introduced in PHP 4.0.
Syntax
bool is_float ( mixed $var )
Parameters
Parameter Description Is compulsory
var The variable being evaluated. compulsory
Return type:
The is_float() function returns TRUE if the var_name is float, otherwise false.
Example 1
Example 2
Example 3
';
else
echo 'This is not a float value.
';
var_dump(is_float('javatpoint'));
echo '
';
var_dump(is_float(85));
?>
Output:
This is a float value.
boolean false
boolean false
PHP Data Types: Compound Types
Compound Types
There are 2 compound data types in PHP.
Array
Object
Array:
The array is a collection of heterogeneous (dissimilar) data types. PHP is a loosely typed language that?s why we can store any type of values in arrays.
Normal variable can store single value, array can store multiple values.
The array contains a number of elements, and each element is a combination of element key and element value.
SYNTAX OF ARRAY DECLARATION:
Variable_name = array (element1, element2, element3, element4......)
Example 1
Output:
Array ( [0] => 10 [1] => 20 [2] => 30 )
Example 2
Output:
Array ( [0] => 10 [1] => Hitesh [2] => 30 )
Object:
An object is a data type which accumulates data and information on how to process that data. An object is a specific instance of a class which delivers as templates for objects.
SYNTAX:
At first, we must declare a class of object. A class is a structure which consists of properties and methods. Classes are specified with the class keyword. We specify the data type in the object class, and then we use the data type in instances of that class.
Example 1
car();
?>
Output:
Display tata motors
Example 2
jsk = 100;
}
}
$obj = new student();
echo $obj->jsk;
?>
Output:
100
Example 3
str;
}
}
$obj = new greeting;
var_dump($obj);
?>
Output:
object(greeting)[1]
public 'str' => string 'Hello Developer' (length=15)
PHP Data Types: Special Types
There are 2 special data types in PHP
Resource
Null
Resource Data type:
It refers the external resources like database connection, FTP connection, file pointers, etc. In simple terms, a resource is a special variable which carrying a reference to an external resource.
Example 1
Output:
FTP Buffer
Example 2
Output:
Resource id #2
Example 3
";
$conn= ftp_connect("127.0.0.1") or die("could not connect");
var_dump($conn);
?>
Output:
resource(3, stream)
resource(4, FTP Buffer)
Null Data Type:
A variable of type Null is a variable without any data. In PHP, null is not a value, and we can consider it as a null variable based on 3 condition.
If the variable is not set with any value.
If the variable is set with a null value.
If the value of the variable is unset.
Example 1
Output:
null
Example 2
";
$a2 = null;
var_dump($a2);
?>
Output:
string ' ' (length=1)
null
Example 3
";
$y = "Hello Developer!";
$y = NULL;
var_dump($y);
?>
Output:
null
null
PHP is_null() function
By using the is_null function, we can check whether the variable is NULL or not. This function was introduced in PHP 4.0.4.
SYNATX:
bool is_null ( mixed $var )
Parameter
Parameter Description Is compulsory
var The variable being evaluated. compulsory
Return Type:
The PHP is_null() function returns true if var is null, otherwise false.
Important Note:
We can unset the variable value by using the unset function.
Example 1
Output:
Variable is not NULL
Example 2
Output:
1
Example 3
";
is_null($y) ? print_r("True\n") : print_r("False\n");
?>
Output:
True
False