PHP MySQL tutorial

PHP Data Types

In PHP, a variable holds different values of various data types. Following data types are supported by PHP:

  • Integers
  • Strings
  • Doubles
  • Booleans
  • Arrays
  • Objects
  • Null
  • Resources

Integers

They are a non-decimal numbers and can be negative, zero and positive values. The range of integers is -2,147,483,648 to 2,147,483,647. Integers can be in three different formats: Hexadecimal (Base 16), Decimal (Base 10) and Octal (Base 8).

In the given below example, variables $KH_Number1 and $KH_Number2 are assigned an integer value. You can know the datatype of variable using var_dump function of PHP.

<?php
	$KH_Number1 = 3120;
	var_dump($KH_Number1);  

	$KH_Number2 = 451 + 98421;
	var_dump($KH_Number2);
?>

Download the code Run the code

Strings

They are sequences of the characters. String can be defined using single quote and double quote. Single quote will consider the string as it is, whereas, double quote can parse the variable and replace its value.

<?php
	$KH_Name = "John";
	$KH_DisaplyName1 = 'My name $KH_Name is not displayed here'; 
	$KH_DisaplyName2 = "My name is $KH_Name"; 

	echo $KH_DisaplyName1 . "<br>"; // Output: My name $KH_Name is not displayed here
	echo $KH_DisaplyName2 ;         // Output: My name is John
?>

Download the code Run the code

Doubles

They are numbers with decimal places like 461.45.

<?php
	$KH_Double = 945.12;
	var_dump ($KH_Double);
?>

Download the code Run the code

Booleans

They have likely two states either true or false. Booleans are frequently used in conditional testing with if statement.

<?php
	$KH_Knowphp = true; 
	if ($KH_Knowphp)
	  echo "I know PHP script";
	else
	  echo "I don't know PHP script";
?>

Download the code Run the code

null

It is a special data type which contains only one value: null. The variable of null data types means that variable is not assigned with any value. It is often used with isset() function.

<?php
	$KH_Name = null;
	if (!isset($KH_Name))
		echo "Variable does not contain value";
	else
		echo "Variable has the value of $KH_Name";
?>

Download the code Run the code

<<< PHP VariablesPHP Constants >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .