Home > DeveloperSection > Articles > PHP Data Types and Decision Making

PHP Data Types and Decision Making


PHP PHP 
Ratings:
0 Comment(s)
 220  View(s)
Rate this:

PHP Data Types and Decision Making

PHP supports eight data types:

·         Integer

·         Float

·         String

·         Boolean

·         Array

·         Object

·         Null

·         Resource

PHP integer

 

An integer data types is a non-decimal number  between -2,147,483,648 and 2,147,483,648.They correspond to simple whole numbers , both positive and negative.

Integers can be specified in three formats:

·         Decimal(10-based),

·         Hexadecimal(16-based-prefixed with 0x)

·         Octal(8-based-prefixed with 0)

In the following examole $y is an integer:

<?php

$y=10;

Var_dump($y); // This function returns the data type and value

?>

 

 

Result:

 

int.PNG

 

 

PHP String:

A string is a sequence of character,Following is a valid string example:

<?php

$a=”Hello”;

$b=”world!”;

echo  $a;</br>//echo function is used to print

echo $b;

?>

 

 

Output:

Hello

World!

 

PHP Float

Floating point numbers are also called as double.It has a decimal point or exponential number.

Following is a valid float example:

<?php

$x=6.55;

$y=3.35;

$x=$x+$y;

print($z);

?>

 

Result:

9.9

 

PHP Boolean:

A boolean has two values:true or false.

$a=true;

$b=false;

 

PHP Array

An array is a data structure that store one or more similar type of values in 0ne variable.For example if you want to store 5  numbers in one variable you can use array.

<?php

$x=array(“Ram”,”Shyam”,”Neha”,”Shikha”,”Shalini”);

var_dump($x); // This function returns the data type and value

?>

 

Result:

array(5) { [0]=> string(3) "Ram" [1]=> string(5) "Shyam" [2]=> string(4) "Neha" [3]=> string(6) "Shikha" [4]=> string(7) "Shalini" }

 

PHP Null

Null data type is a variable in which no value is assigned.

It can only have one value: Null

<?php

$a=10;

$a=null;

var_dump($a); // This function returns the data type and value

?>

 

Result:

NULL

 

PHP Object

In PHP,object must be declare explicitly.

Following is a valid object example:

<?php

class text{

    function text(){

        $this->model = "Hello world";

    }

}

$text=new text();// create an object

var_dump($text); // show object properties

?>

 

Result:

object(text)#1 (1) { ["model"]=> string(11) "Hello world" }

 

PHP Resource

The special resource type is not an actual data type. It is the strong of a reference to functions and resources external to PHP.

Decision Making

The if..else..elseif are used to take decision based on some conditions.

This is how decision making statements works:


There are three types of decision making statements:

If..else:Code is executed if condition is true and another code if condition is false.Let’s see some example:

<?php

$x=10;

If($x)

{

echo("if condition is executed");

}

else

{

   echo("else condition is executed");

}

?>

 

Result:

If condition is executed

 

Elseif:this statement is used when you have to execute some code if one of the several conditions are true.

<?php

         $d = date("D");

        

         if ($d == "Sat")

            echo "Have a nice weekend!";

        

         elseif ($d == "Sun")

            echo "Have a nice Sunday!";

        

         else

            echo "Have a nice day!";

      ?>

     

 

Result:

Have a nice weekend!

 

Switch Statement:The switch statement is used to avoid long blocks of if..elseif code.

<?php

$name = "Ram";

 

switch ($name) {

    case "Ram":

        echo "My name is Ram!";

        break;

    case "blue":

       echo "My name is Shyam!";

        break;

    case "green":

        echo "My name is Rakhi!";

        break;

    default:

        echo "Your name is neither Ram,Sham nor Rakhi!";

}

?>

     

 

Result:

My name is Ram!


Don't want to miss updates? Please click the below button!

Follow MindStick