Data Types in PHP






The value assigned to a PHP variable can be of different data types, which have complex data types such as simple strings and numerical types like array and objects.

PHP supports eight primitive data types: integer, floating point number or float, string, boolean, array, object, resource and tap. These data types are used to create variables, we now discuss each one of them in detail.




Integers  Data Type


<?php
$a = 123; // decimal number
var_dump($a);
echo "<br>";

$b = -123; // a negative number
var_dump($b);
echo "<br>";

$c = 0x1A; // hexadecimal number
var_dump($c);
echo "<br>";

$d = 0123; // octal number
var_dump($d);
?>

Strings Data Type

The strings are the sequence of characters, where each character is like a byte.

<?php
$a = 'Hello world!';
echo $a;
echo "<br>";

$b = "Hello world!";
echo $b;
echo "<br>";

$c = 'Stay here, I\'ll be back.';
echo $c;
?>

Floating or Doubles Data Type


<?php
$a = 1.4;
var_dump($a);
echo "<br>";

$b = 8.2e3;
var_dump($b);
echo "<br>";

$c = 5E-10;
var_dump($c);
?>

Booleans Data Type


<?php
// Assign the value TRUE to a variable
$show_error = true;
var_dump($show_error);
?>

Objects Data Type


An object is a data type that not only allows data to be stored but also information about how to process that data. An object is a specific example of a class that works as an object for templates. is. Objects are created based on this template through new keywords.


<?php
// Class definition
class greeting{
// properties
public $str = "Hello World!";

// methods
function show_greeting(){
return $this->str;
}
}

// Create object from class
$message = new greeting;
var_dump($message);
?>

NULL Data Type


<?php
$a = NULL;
var_dump($a);
echo "<br>";

$b = "Hello World!";
$b = NULL;
var_dump($b);
?>

Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment