PHP - SPLessons

PHP Data Types

Chapter 7

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

PHP Data Types

PHP Data Types

shape Description

Normally, in PHP we no need to represent the data type to a variable, depending on variable value, it converts the correct data type to variable automatically.

Integer

shape Description

Integer is a positive or negative number without decimal value. Integer is of only number, without comma and decimal value.Integers can be defined in three different formats:

  • Decimal (base-10)
  • Hexadecimal (base 16 – prefixed with 0x)
  • Octal (base 8 – prefixed with 0)

Example: The function var_dump() returns the data type and value.

<?php  
	$a = 258;
	var_dump($a);
?>  

String

shape Description

String is a collection of characters.The string can be enclosed in single or double quotes.

Example:

<?php 
	$a = "Welcome to SPLessons!";
	$b = 'Welcome to SPLessons!';

	echo $a;
	echo "<br>"; 
	echo $b;
?>

Float

shape Description

The numeric value, which consists of decimal value called as floating value.

Example:


<?php  
	$a = 25.365;
	var_dump($a);
?>  

Boolean

shape Description

Boolean is a data type which represents true or false.

Example:

$a = true;
$b = false;

Array

shape Description

Array is a collection of variables, which stores different variable values as a single array.array() keyword is used to define an array.

Example:

<?php  
	$operating_systems = array("Windows","Linux","Unix");
	var_dump($operating_systems);
?> 

Null

shape Description

Null represents no value, this is a special data type which don’t assign any value to the variable.Any variable without any value assigned to it tends to NULL value.

<?php
	$a = "Welcome to SPLessons!!";
	$a = null;
	var_dump($a);
?>

PHP Object

shape Description

Object data type is used to store a data. object data type, must create a class where Class is a structure of methods and it’s properties.

<?php
	class OS {
		function OS() {
			$this->model = "Linux";
		}
	}
	// create an object
	$version = new OS();

	// show object properties
	echo $version->model;
?>

shape Collective
Example


<!DOCTYPE html>
<html>
	<body>

		<?php  
			$a = 5896;
			var_dump($a);
			
			echo "<br><br>";
			
			$b = 10.365;
            var_dump($b);
            
            echo "<br><br>";
            
            $c = "Hello world!";
            echo $c;
            
            echo "<br><br>";
            
            $operating_systems = array("Windows","Linux","Unix");
            var_dump($operating_systems);
            
            echo "<br><br>";
            
            $d = "Welcome to SPLessons!!";
            $d = null;
            var_dump($d);
		?>  

	</body>
</html>

Output:

Summary

shape Key Points

  • PHP Data Types are of various types like Integer,String,Float or double,Array,Boolean,Null and Object.