iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 15, 2008
Reader Question - Would you host your client's work on your website?
About
 
May 15, 2008
How to Create an Ajax Autocomplete Text Field: Part 6
WebReference.com
 
May 14, 2008
Poll: Are the browser safe colors still needed?
About
 
May 14, 2008
Google Doctype launched
About
 
May 14, 2008
Web Editor Reviews - 6 New Reviews
About
 
May 14, 2008
Build Beautiful Buttons in Photoshop, Part I
SitePoint
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP

Working with PHP datatypes 

  Views:    15014
  Votes:    4
by Steve Adcock 4/17/04 Rating: 

Synopsis:

PHP is an incredible language (and free!). This article introduces the basics of the PHP language by examining datatytes available in the language.
Pages: firstback1 forwardlast
The Article

if this, if that

Let's make some decisions here. PHP's if statements can be used to check the validity of data, integrity or even the presence of data within a variable. Let's use the gettype() function in our first example to check the datatype of a variable.

<?

$i = 10;

if(gettype($i) == "integer")       // Checks if variable i is an integer
{
echo("Alright, the variable i is an integer");       // If integer, then display
} else {
echo("No, sorry, variable i is not an integer");       // If not integer, then display
}

?>  

Since 10 is an integer, and our if statement checks to see if the variable is an integer, the "Alright, the variable i is an integer" string will be displayed. Try changing 10 to 10.1. Since 10.1 is a double instead of an integer, the second statement, "No, sorry, variable i is not an integer" will be displayed.

Two more functions, isset() and unset() are also useful PHP utilities for working with datatypes. isset() checks to see if a variable has been given a value. unset() will destroy the variable, even from memory.

 

<?

$i = 10;

if(isset($i))       // Checks if variable i has a value
{
echo("Variable i has a value");       // If defined, then display
} else {
echo("Sorry, variable i has no value");       // If not integer, then display
}

?>  

Since variable i does have a value, "Variable i has a value" will be printed. If you delete the $i = 10; line, the second echo statement, "Sorry, variable i has no value" will execute. Let's move on to unset, to remove the variable for memory.

<?

$i = 10;

unset($i);       // Destroys variable i

if(isset($i))       // Checks if variable i has a value
{
echo("This should NOT display");       // If not destroyed, then display
}

?>  

In the first part of the coding, we defined a variable, i. Then, we used the unset() function and passed $i into it as an argument, which therefore destroyed the variable. Just to make sure that we did, in fact, destroy the variable, I used the isset() function we just looked at above to check for a value. Since there should not be a value, no echo statement should be printed and you should be left with a blank page, if nothing else is within your document, of course.

We can also perform an action if, and only if, a particular variable is a specific datatype, by using the is_double(), or is_string(), or is_array(), or is_integer, etc functions relative to the respective datatype you are checking for. Let's look at an example.

<?

$i = 10;

if (is_integer($i))
{
echo("This is an integer");

if (is_string($i))
{
echo("This is a string");

if (is_double($i))
{
echo("This is a double");
}

?>  

Since 10 is an integer, the first if statement will be true and "This is an integer" will be displayed. Try adding quotes around the 10, then the string echo value would display, or try adding a .1 after the 10, then it's a double.

Here you have a basic introduction into datatypes and how to manipulate them using the PHP server-side language. A complete mastery of PHP ultimately begins at datatypes, so congratulations on passing the first step. A continuing study of this language can render you an expert in no time. Good luck.

Pages: firstback1 forwardlast

Similar/related articles:


 
  Sponsors