Hello... Friends Today we are going to discus about variables & Datatypes in php programming
Variables :
Variables are the storage container in the php we can say if we want to store any value & want to call it in our program but before assigning & calling variables we have various rules here these rules are as follows ...
Scalar datatype are :
Variables :
Variables are the storage container in the php we can say if we want to store any value & want to call it in our program but before assigning & calling variables we have various rules here these rules are as follows ...
- Any variable start with the $ sign, before any name of the variable
- Any variable name must start with letter or underscore character not with any numbers
- Any variable name is case sensitive
- Any variable name only contains alpha-numeric characters and underscores like ( A-Z , a-z , 0-9 and _ ) not any special symbol allows in the variable name
when we assign variable without our rules then it gives us error in our php scripts
Datatypes: All data stores in php variables fall into one of eight basic categories, these are known as data type. a variable datatype determines what operations can be carried out on the variable's data, as well as the amount of memory need to hold the data.
Scalar Data Type -- Contains single value
Compound Data Type -- Contains more then two values
Special Data Type -- Contains no any scalar value not Compound but special values with means
Scalar datatype are :
- Integer -- any whole number like 20
- Float -- any floating number like 9.52
- String -- any string characters like "My name is Govind Singh"
- Boolean -- any true & false ( 1 & clear or 1 & 0 )
Compound Data Type :
- Array -- any map having name or number mapping values
- Object -- any type containing methods & properties
Special Data Type :
- Resource --containing any reference to any external resource like any file or database
- Null -- may only containing null as a value, meaning the variable explicitly does not containing any value
let's if we define any variable then how we can test that what is the type of variable so we can check about the variable by using gettype() function let's see how it works
here in this tut you can see we are getting a variable with there type & see here i want to test about null variable or we can say i'll not define it in our script so it gives me error & also tells me about the variable type that it is a NULL type variable
Let we play little more with php here in variables & datatypes
Suppose i want to test about any variable that it is a string or not an integer variable or not then see how we test about it & how to change a variable type
here is an universal testing method a function ( is_type ( value ) )
example :
is_int ( value ) --- it return true if value is an integer
is_float ( value ) --- it return true if value is an float
is_string ( value ) --- it return true if value is string
is_object (value ) --- it return true if value is object
is_array ( value ) --- it return true if value is array
true = 1 & false = clear
here we can see $name is a string & if i test for string then it gives us 1 & in the next see $age where it is a integer but when i ask from it about is it a string then see it gives nothing means a clear value under variable $age.
let's now as i want to change the type then how can i change that all sit :p
So here we have another function settype( value, "type")
in the previous script we have value as string of $name but i want to change it from string to integer & $age which is an integer type & here i am changing it into a string then see how we can change it
so here you can see that we can change the value too :p
so if you want to store the grater value in any variable then we can change it's type & we are able to increase the storage space for the variable . it is a forcing a variable in any particular type so it is vary interesting
Let we play little more with php here in variables & datatypes
Suppose i want to test about any variable that it is a string or not an integer variable or not then see how we test about it & how to change a variable type
here is an universal testing method a function ( is_type ( value ) )
example :
is_int ( value ) --- it return true if value is an integer
is_float ( value ) --- it return true if value is an float
is_string ( value ) --- it return true if value is string
is_object (value ) --- it return true if value is object
is_array ( value ) --- it return true if value is array
true = 1 & false = clear
here we can see $name is a string & if i test for string then it gives us 1 & in the next see $age where it is a integer but when i ask from it about is it a string then see it gives nothing means a clear value under variable $age.
let's now as i want to change the type then how can i change that all sit :p
So here we have another function settype( value, "type")
in the previous script we have value as string of $name but i want to change it from string to integer & $age which is an integer type & here i am changing it into a string then see how we can change it
so here you can see that we can change the value too :p
so if you want to store the grater value in any variable then we can change it's type & we are able to increase the storage space for the variable . it is a forcing a variable in any particular type so it is vary interesting
+1
ReplyDelete