PHP Lesson 3 – Data Types
In PHP, as in other languages, there are different types of data. These data types have different uses.
- Integer: “All Numbers”
- Float: “Numbers like 4.2”
- String: “Texts”
- Array: “Arrays can contain more than one data type.”
- Boolean: “A binary value system. Like True, False.”
- Constant: “Unchangeable constants.”
- Objects: “Objects of the OOP system.”
Integer
They are numbers. Mathematical operations can be performed with this data.
<?php $degisken1 = 5; $degisken2 = 10; echo $toplam = $degisken1 + $degisken2; ?>
String
They are text. Anything can be used as text. Numbers within them cannot be used for mathematical operations. If we perform an addition operation on this data, it will perform a side-by-side operation. For example, if we multiply 1+1, the result will be 11. We can write HTML code within string data. We can also use this to print HTML code.
<?php $string = "<p>Merhaba Dünya</p>"; echo $string; ?>
Float
This data type is decimal numbers.
<?php $float = 1.5; ?>
Boolean (Bool)
It's a data type that can only take values of false or true. In PHP, 1 is true, 0 is false, empty is false, and true if the data contains even spaces. For example, the isset() function checks whether the data is present, while the empty() function checks whether the data is present.
<?php $booltrue = TRUE; $booltrue2 = 1; $booltrue3 = "Merhaba Dünya"; $boolfalse = False; $boolfalse2 = 0; $boolfalse3 = "";
Constants
These data are immutable constants. Once you define them, they cannot be changed to anything other than that definition. Their names must be in uppercase letters.
<?php define("ISIM", "Erhan"); echo ISIM; ?>
Array
Arrays are a data type that can hold multiple data items. We can store any number of items within them. They have various definitions.
<?php $array = array("Erhan", "Kılıç", 1987, false);
Another method for defining elements is as follows. This method adds data to the last row.
$array[] = "How are you";
We can access the data within the array using its keys. Keys representing the location of data in arrays start at 0. For example, to access the first data item in our first array, we proceed as follows:
<?php echo $array[0];
Associative Array
In this series, we determine the keys ourselves.
<?php $array = array( "isim" => "Erhan", "surname" => "Sword", "age" => 28 ); echo "Name: " . $array["name"] . "<br/>Surname: " . $array["surname"];