Download this Blogger Template by Clicking Here!

Ad 468 X 60

Monday, 11 March 2013

Widgets

Learn Php programming Data Types

What Are Data Types ?


DATA types
DATA types



There are different types of data :

  • Numeric Data (0-9)
  • Alpha-Numeric Data (0-9,A-Z, a-z)

So because of the different types of database, when we are declaring a variable then we also have to define the data type of the variable , Variable gets space in memory according to the data type defined with it. for example in C++ we make and define a variable like this :

int a = 5;

Here you can see in above example that first we have given a datatype to the variable . Here variable " a " is of integer data type and an integer can be stored in this variable. Now because of this integer declaration to the variable " a" we can not assign a character to variable " a " . If we assign a character to an integer variable , we will get an error.

Also Check : 


How Php Deals With Undefined Variables ?


PHP is a simple and loosely typed language. It means that whenever you declare a variable , PHP will define a data type automatically for the variable and you will not have to face any data type mismatch error .

Data Types :



Types Of Data
Integer Type Of Data

Integer


For Example : 5 , 10
Description : Whole number

Double


For Example : 10.235
Description : floating point number or numbers with decimal point

String


For Example : "Pakistan"
Description : Collection of characters

PHP string data types
PHP string data types

Boolean


For Example : true , false
Description : special value that can be either true or false


Object


Description : Collection of variables and functions

PHP data types
PHP data types

Array


For Example : array(1,2,3......)
Description : collection of variables

Null


Description : Variable which is declared but never assigned a value.



Data Types in Urdu
Data Types in Urdu Language



PHP Program To Explain Variables :


For understanding and learning above data types . Lets make a program in text editor and use them in our program so that you can easily understand the usage and working of  data types.

Also Check :



Source Code
Source Code


<?php

// declare without assigning

$test_var ;

echo "The data type of test_var_ variable is  : ".gettype( $test_var );

echo" <br/> ";

$test_var = 5 ; // integer

echo "The data type of test_var_ variable is  now : ".gettype( $test_var );

echo" <br/> ";

$test_var = "Pakistan"; // string

echo "and now the data type of test_var_ variable is  : ".gettype( $test_var );

echo" <br/> ";

$test_var = 9.33; // double

echo "and now the data type of test_var_ variable is  : ".gettype( $test_var );

echo" <br/> ";

$test_var = true; // Boolean

echo "and now the data type of test_var_ variable is  : ".gettype( $test_var );

echo" <br/> ";

?>
         

Output :



Source Code Output
Source Code Output

The data type of test_var variable is : NULL

The data type of test_var_ variable is  now : integer

and now the data type of test_var_ variable is  : string

and now the data type of test_var_ variable is  : double

and now the data type of test_var_ variable is  : Boolean

You can see in the above script that we have declared a variable $test_var and used gettype function to get the data type of the variable. In the next lines of the code , we have assigned different data to the variable and each time we assign a data to the variable , data type of the data is find using the the get type function. The php engine checks the code from top lines to bottom and by processing each line it displays the output.

In the above program we have changed the data type of the single variable at run time and php engines decides the data type at run time. In the same way we can set the data type of variable at run time using settype function .

Understand and see the below program to find how settype function works.

<?php

$myvar = 100 ;

echo " Data type of myvar is : ".gettype ($myvar)." <br/>" ;

settype($myvar , "string" );

echo " The variable is now a : ".gettype($myvar);

?>

Source Code Program
Source Code Program

Here we have declared a variable named myvar and assigned a value of 100 to it . By assigning 100 to it , myvar becomes a integer variable . In the second line of the code we have printed the data type of he variable. By using settype function we have changed the data type of myvar variable from integer to string. At the end of the code we have printed again the data type of variable after changing the data type. Settype function takes two parameters. First parameter is the name of the variable and the second parameter is the new data type that we want to assign to the variable.

Settype(name of variable , " New Data Type ");

Php is a language full of functions  We will explain another way to change the data type of the variable in the next tutorial.



" Sharing Is Caring "

Stay Blessed

SHARE THIS POST   

  • Facebook
  • Twitter
  • Myspace
  • Google Buzz
  • Reddit
  • Stumnleupon
  • Delicious
  • Digg
  • Technorati
Author: Mohammad
Mohammad is the founder of STC Network which offers Web Services and Online Business Solutions to clients around the globe. Read More →

0 comments: