Thursday, December 25, 2014

What are PHP Data Types ?


PHP supports several data types, they are listed below :

String: A string is sequence of characters it may be any text inside quotes.

Example:

PHP Script (string.php)
 <!DOCTYPE html>  
 <html>  
 <head lang="en">  
   <meta charset="UTF-8">  
   <title>Nirajan Ghimirey's Workshop</title>  
 </head>  
 <body>  
 <h3>Example of String</h3>  
 <?php  
 $var1 = "Hello Joe..!!";  
 echo $var1;  
 ?>  
 </body>  
 </html>


Integer: An integer is a whole number which does not have decimals. There are several rules for integers :-

  1. An integer must have at least one digit and this may be from 0 to 9.
  2. An integer should not contain comma or blanks otherwise it won’t be called an integer.
  3. An integer should not have a decimal point.
  4. An integer can be positive or negative.
  5. Integers can be in three formats and they are hexadecimal, decimal and octal. 

Exampe:
 <!DOCTYPE html>  
 <html>  
 <head lang="en">  
   <meta charset="UTF-8">  
   <title>nirajanghimirey's workshop</title>  
 </head>  
 <body>  
 <h3>Example of Integer</h3>  
 <?php  
 $var1 = 5;  
 $var2 = 4;  
 echo $var1 + $var2;  
 ?>  
 </body>  
 </html>

Float: A float can be defined as a number which has a decimal point or it might also have a number in exponential form.  

Example:
 <!DOCTYPE html>  
 <html>  
 <head lang="en">  
   <meta charset="UTF-8">  
   <title>nirajanghimirey's workshop</title>  
 </head>  
 <body>  
 <h3>Example of Float</h3>  
 <?php  
 $var1 = 5.4;  
 $var2 = 4.6;  
 echo $var1 + $var2;  
 ?>  
 </body>  
 </html> 
 Boolean: Two states are represented by Boolean and those possible states are TRUE or FALSE.
$x = true;     
$Y= false;
The use of Booleans is done in conditional testing.

Example:
 <!DOCTYPE html>  
 <html>  
 <head lang="en">  
   <meta charset="UTF-8">  
   <title>nirajanghimirey's workshop</title>  
 </head>  
 <body>  
 <h3>Example of Boolean</h3>  
 <?php  
 $var1 = true;  
 $var2 = false;  
 echo "var1 = " . $var1;  
 echo "<br/>";  
 echo "var2 = " . $var2;  
 ?>  
 </body>  
 </html> 

Array: Multiple values are stored in an array and in one single variable.

Example:

PHP Script (array.php)

 <!DOCTYPE html>  
 <html>  
 <head lang="en">  
   <meta charset="UTF-8">  
   <title>nirajanghimirey's workshop</title>  
 </head>  
 <body>  
 <h3>Example of Array</h3>  
 <?php  
 $arr = array("HTML", "CSS", "PHP");  
 foreach ($arr as $value) {  
   echo $value . "<br/>";  
 }  
 ?>  
 </body>  
 </html> 



Object: Object scan only be called a data type if it tends to store data and information which describes how to process that data.Object is explicitly declared in PHP.
 First the class of object must be declared and for doing so the class keyword is used. A class can be defined as a structure which can contain properties and methods.

Example:

 <!DOCTYPE html>  
 <html>  
 <head lang="en">  
   <meta charset="UTF-8">  
   <title>nirajanghimirey's workshop</title>  
 </head>  
 <body>  
 <h3>Example of Object</h3>  
 <?php  
 class Food{  
   function cook() {  
     return "mo-mo";  
   }  
 }  
 /*create an object*/  
 $course = new Food();  
 echo $food->mo-mo();  
 ?>  
 </body>  
 </html> 



Null Value:Null is a special data type that can have only one value: NULL.
A variable of data type NULL has functionality and that is it has no value assigned to it.

Example:
 <!DOCTYPE html>  
 <html>  
 <head lang="en">  
   <meta charset="UTF-8">  
   <title>nirajanghimirey's workshop</title>  
 </head>  
 <body>  
 <h3>Example of NULL</h3>  
 <?php  
 $var1 = null;  
 var_dump($var1);  
 ?>  
 </body>  
 </html>

Resource :
Example:
 <?php  
 $conn = mysqli_connect("localhost","root","admin","password");  
 ?> 

No comments:

Post a Comment