Showing posts with label PHP Data types. Show all posts
Wednesday, 13 March 2013
Change Data Type Using Functions And Print Special Characters In Php
Data Type Conversion Using Functions In PHP :
There are some other ways to change the data type of available without using Type casting. There are some functions that change the data type of a variable .
For example
- intval (value) ;
- double (value);
It takes a value (whose data type we want to change ) as input and returns a double data type output .
- strval (value) ;
It takes a value (whose data type we want to change ) as input and returns a string data type output .
Print Special Characters In Php :
One thing I forgot in my previous post that we can use echo or print statement for printing output on screen. If you want to print a special character on screen like double quotes (") or single quote ( ' ) or backslash ( \ ), then we can print these by using a backslash . See the example to understand
<?php
echo "He said : \ " I love Pakistan \" ";
?>
Output
He said : "I love Pakistan "
In above statement you can see that I wanted to print double quotes on the screen so that I used backslash. If you want to print the name of the variable instead of its value then use single quotes in echo statement
<?php
$city = "Karachi";
echo 'City is : $city '. '<br/>;
echo " City is : $city ";
?>
Output
" Share With Others And Care Others "
Read More »
Labels:
PHP,
PHP Data types,
Programming,
Tricks and Tutorials,
Web Development
Tuesday, 12 March 2013
Convert Data Type By Type Casting In Php
What is Data Type Conversion And Type Casting In Php ?
If we want to change a data type of a variable then there is a way that just write the data type in round brackets and write it before the variable. In this way the data type of the variable will be changed to the data type written in brackets.
Difference Between Type Casting And Using Settype Function :
The main difference between Type Casting and Settype function is that in type casting the original data type of the variable does not changes but a new variable copy with a new data type will be created. If you are getting confused then observer the program below to understand type casting
<?php
$undecided = 3.14 ;/* this variable is only declared and it has been assigned a data type*/
$holder ; /* this variable is only declared and it has not been assigned a data type yet */
$holder = (double) $ undecided;
print gettype( $holder ) ; /* This will print the data type of holder variable at this stage
*/
print " -- $ holder <br/> " ; /* This will print the value asigned to holder variable*/
$holder = ( string ) $undecided ;
print gettype( $holder ) ; /* This will print the data type of holder variable at this stage */
print " -- $ holder <br/> " ; /* This will print the value asigned to holder variable */
$holder = ( integer ) $undecided ;
print gettype( $holder ) ; /* This will print the data type of holder variable at this stage */
print " -- $ holder <br/> " ; /* This will print the value asigned to holder variable */
$holder = ( boolean ) $undecided ;
print gettype( $holder ) ; /* This will print the data type of holder variable at this stage */
print " -- $ holder <br/> " ; /* This will print the value asigned to holder variable */
$holder = ( double ) $undecided ;
print gettype( $holder ) ; /* This will print the data type of holder variable at this stage */
print " -- $ holder <br/> " ; /* This will print the value asigned to holder variable */
?>
Output :
double -- 3.14
string -- 3.14
integer -- 3
boolean -- 1
double -- 3.14
In the above script we have made a variable $undecided and the given a floating point value to it. Then with type casting we have made of copy of $undecided variable with different data type and then placed the copy in $holder variable . In this way the data type of $undecided variable remains same and we can just use this variable to make its copies with different data types and then assigning it to others. This is helpful when we want some specific data types like string or integer.
For example If you ask user to enter his/her age in text box and users enters 25 years in the textbox. User has entered a string But Age is usually in integer. So now you have to type cast the string entered by the user. This will be done as follows :
For example If you ask user to enter his/her age in text box and users enters 25 years in the textbox. User has entered a string But Age is usually in integer. So now you have to type cast the string entered by the user. This will be done as follows :
$age = (integer) $ user_age ;
print $age;
The output of above statement will be
Output :27
Other Ways For Data Type Conversion :
Ofcourse There are always many other ways to perdorm a single task. In the same way there is another way to change the data type of given variable without using Type casting. We will discuss those ways in our next Tutorial.
Stay tuned and subscribed for learning Complete Web development. Share It with others. Ask Questions And Queries Via Comments. You feedback will be welcomed. Thanks
" Share It Please . Thanks!!!"
Read More »
Labels:
PHP,
PHP Data types,
Programming,
Tricks and Tutorials,
Web Development
Monday, 11 March 2013
Learn Php programming Data Types
What Are 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 :
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 :
Integer
Description : Whole number
Double
Description : floating point number or numbers with decimal point
String
Description : Collection of characters
Description : special value that can be either true or false
Object
![]() |
PHP data types |
Array
Description : collection of variables
Null
PHP Program To Explain Variables :
Also Check :
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 :
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 |
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
Read More »
Labels:
PHP,
PHP Data types,
Tricks and Tutorials,
Web Development
Subscribe to:
Posts (Atom)