Home arrow PHP arrow Page 3 - Working With PHP Data Types

Working With PHP Data Types

Using and manipulating variables in PHP is extremely easy because PHP includes several functions to help us compare and contrast them. In this article, Steve introduces us to the range of PHP data types. He also teaches us how to work with and manipulate PHP data types. This article also presents several code examples that you can test and learn from.

Author Info:
By: Steve Adcock
Rating: 4 stars4 stars4 stars4 stars4 stars / 16
January 23, 2002
  1. · Working With PHP Data Types
  2. · Defining variables and constants
  3. · Manipulating a variables data type
  4. · If this, if that
  5. · Conclusion

print this article

Working With PHP Data Types - Manipulating a variables data type
(Page 3 of 5 )

PHP includes a great function, gettype that we can use to display the type of a variable, and not its actual value. You can use gettype like this:


$i = 10;

$j = 10.3;

$k = "Broncos";

echo(gettype($i) . "<br>"); // Displays the string "integer"

echo(gettype($j) . "<br>"); // Displays the string "double"

echo(gettype($k) . "<br>"); // Displays the string "string"


Notice our use of the period operator to separate our call to the gettype function from the HTML code contained within the quotes for each echo function. By simply changing the variable we pass as the parameter to the gettype function, we can check the data type of any other variable. We can also set the data type of a variable after it has been created. The following example first defines a double, and uses PHP's settype function to redefine it to an integer.


$k = 12.1;

echo("$k = " . gettype($k) . "<br>"); // Displays "$k = double"

settype($k, "integer"); // Changes the data type of variable k

echo("$k now = " . gettype($k)); // Displays "$k now = integer"


As you can see, we are actually passing in two different parameters to the settype function: the name of the variable whose type we want to change, and the variable type we want to change it to. Keep in mind that if you change a string to an integer, then the value of that string will equal 0, instead of the actual string of characters that it was initially defined to hold.
blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials