Basic
Advanced
Examples
PHP 8 - Data Types
Last modified: April 02, 2022Variables can store data of different types. PHP support below Date Type
- String
- Integer
- Float
- Array
PHP is a loosely typed language; it does not have explicit defined data types, meaning if we define a variable as String first and then later we can later we can find as Integer given value support it. Here is an example of such logic.
- echo syntax is used to print code to the HTML/browser
<?php
$five = '5';
$fiveNumber = 1 + $five;
echo $fiveNumber;
?>
Output 6
- PHP String
It can be any text withing " or ', single or double quotes for example, "Hello World"
Example
<?php
$name = "kafle";
$city = "London";
echo $name;<br />
echo $city;
?>
Outcome
- PHP Integer
Example
<?php
$amount = 50000;
echo $amount;
?>
Outcome
- PHP Float
Example
<?php
$amount = 50000.59;
echo $amount;
?>
Outcome
- PHP Boolean
Example
<?php
$IsLondonInUK = true;
echo $IsLondonInUK;
?>
Outcome
- PHP Array
Example
<?php
$countries = array("HK", "UK", "USA");
var_dump($countries);
echo "<br />";
echo "I live in ";
echo $countries[0];
?>
Outcome
- PHP Multidimensional Array
Country | continent |
---|---|
China | Asia |
Nepal | Asia |
India | Asia |
UK | Europe |
France | Europe |
<?php
$countries = array (
"Asia" => array("China", "Nepal", "India"),
"Europe" => array("UK", "France"),
);
print_r($countries);
?>
Outcome
Constants
Constants are like variables except that once they are defined they cannot be changed or undefined.
<?php
define("DBSERVER", "192.168.1.10");
echo DBSERVER;
?>