Sunday, 19 October 2014

PHP Basics #5 : PHP Data Types

PHP : Data Types


In this tutorial I will tell you about Data Types in PHP.



PHP : Data Types



There are mainly 7 types of data in PHP but we will cover only 5 types of data now and later on we will talk about the rest 2.
So the 5 data types which we are going to talk about now are the following..

  • String
  • Integers
  • Float Numbers
  • Boolean  
  • NULL


i. String



A string in php and all other programming or web developing languages are the alpha-numeric letters and sentences. In php we always have to write strings between single or double quotes.


<?php

echo "Hello I am a String";
echo "<br />";
echo 'Hello I am also a string between single quotes';

?>

The echo "<br />"; line in the code is an html tag for next line and this also shows us that we can also echo html tags if we want but for now dont use any other html tag except <br />.


If you output the above code in a browser the result will be this..

Hello I am a String
Hello I am also a string between single quotes  

That was all about String you must know till now..



ii. Integers (Numbers)



Integers in php and every other computer language represents all numbers. Integers in php does not require quotes around it.


<?php

echo 1;
echo '<br />';
echo 10000;
echo '<br />';
echo 99;

?> 

The output of the above codes in a browser will result..



1
1000
99

iii. Float Numbers



Floats are the numbers like the decimal numbers and thats all I can say about it you will understand better in the example below..


<?php

echo 1.09;
echo '<br />';
echo 0.89;
echo '<br />';
echo 40.6;

?>

If you open the above code in a browser the result will be the following..
1.09
0.89
40.6


iv. Boolean



Boolean is simply 'true' and 'false'. Boolean is mostly used in conditional coding and you will understand it when we start conditional coding in php. For now all you need to know is that boolean is either TRUE or FALSE.


<?php


//Thats how you declare them
$a = true;
$b = false;


?> 


v. NULL 



The special NULL value represents that a variable has no value. NULL is the only possible value of data type NULL.

The NULL value identifies whether a variable is empty or not. Also useful to differentiate between the empty string and null values of databases.

Variables can be emptied by setting the value to NULL


<?php

$a = 'Hello World!';
$a = null;

echo $a;

 ?>

The output of the above code will give you a blank page because  $a was 'Hello World!', but after that you again declared $a to NULL so $a has no value 

No comments:

Post a Comment