Data Types in PHP
PHP supports the following data types:
- String
- Integer
- Float (floating point numbers - also called double)
- Boolean
- Array
- Object
- NULL
- Resource
There are some examples of data types in PHP, you can use it understand the concept:
String
<?php
$x = "Learn PHP By RK";
$y = 'Learn Wordpress By RK';
echo $x;
echo "<br>";
echo $y;
?>
$x = "Learn PHP By RK";
$y = 'Learn Wordpress By RK';
echo $x;
echo "<br>";
echo $y;
?>
Integer
<?php
$x = 9685;
var_dump($x);
?>
$x = 9685;
var_dump($x);
?>
FLOAT
<?php
$x = 18.635;
var_dump($x);
?>
$x = 18.635;
var_dump($x);
?>
Boolean
$x = true;
$y = false;
$y = false;
Array
<?php
$cars = array("Iron Man","Hulk","Thor");
var_dump($cars);
?>
$cars = array("Iron Man","Hulk","Thor");
var_dump($cars);
?>
Object
In the following example car in Class having properties color and model.
<?php
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
$myCar = new Car("black", "Volvo");
echo $myCar -> message();
echo "<br>";
$myCar = new Car("red", "Toyota");
echo $myCar -> message();
?>
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
$myCar = new Car("black", "Volvo");
echo $myCar -> message();
echo "<br>";
$myCar = new Car("red", "Toyota");
echo $myCar -> message();
?>
NULL
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
$x = "Hello world!";
$x = null;
var_dump($x);
?>
Comments
Post a Comment