Free Webmaster Resources .info
PHP Introduction and Tutorials Javascript  Introduction and Tutorials HTML  Introduction and Tutorials ASP  Introduction and Tutorials Free Internet Access Guide and Directory Webspace  Directory Webhosting  Directory Adult Webhosting  Directory PHP Hosting  Directory ASP Hosting  Directory
à Navigation
» Home
» Php Introduction
» Php Tutorials
» Submit Scripts
 
Subscribe for free!

Join our mailing list today and recieve free updates monthly with web hosting specials!
 
 

 

 

Free Webmaster Resources .info | Free PHP Tutorials


Variables in PHP

In part 2 we are going to deal with Data Types and Type Casting amongst other things.

Data Types

These are the data types which uses .

  • string ( text )
  • integer (numeric)
  • double(numeric)
  • array
  • object
  • unknown type

As you can see there are not as many data type are there are in some other languages , this in fact is a help to many a programmer . As a programmer you do not worry about setting these data types , PHP does it for you.

Strings

This is the data type that stores text , this can be single characters , words and even complete sentences . How many strings do you think we have below .

$mywages = "not much";
$intloan = "3000";

If you said one you are wrong , if you said two give yourself a pat on the back . remember we enclosed the number with quotation marks so PHP treats this as text information .

Concatenation

This means adding one string to another . If you are coming from another programming language this may (will) catch you out from time to time . In PHP we use the period ( . ) to concatenate strings.For example

$firstname = "Bill";
$secondname = "Gates";
echo $firstname . $secondname;

would print on the screen BillGates

Numeric Values

There are 2 different types of numerical data types in PHP , integers and doubles. Integers are basicly whole numbers and doubles are fractional (floating point ) numbers . Here are some examples .

$firstint = 9;
$firstdouble = 23.67;
$secondint = - 76;
$seconddouble = -1239.7432;

The values these types can hold is platform specific but for example the range for an integer is commonly -32768 to 32767 on Windows machines.

Type Casting

As you are aware PHP automatically decides what data type but you do have the option of specifying this yourself . Here is how you can do this .

$luckynumber = 7;
$lucknumber = (string) $luckynumber;

This would give us a string rather than the integer we had previously.

Using gettype and settype functions

These are two functions provided with PHP which help us to determine the data type and set the data type respectively , lets look at examples of both . Firstly settype which will display the data type of the variable

<?php
$myname = "Iain";
echo gettype($myname);
?>

 

string
Now for settype , this allows you to set the data type . The first parameter is the actual variable , the second is the data type you require) . In this example we set the data type and then use gettype to show it has changed.

<?php
$wages = 1000;
settype($wages, "string");
echo gettype($wages);
?>

string


 
[more] [1] [2]


Recommended Webhosting Company
Find free web hosting
Looking for free web hosting? Review and compare FindStuff's comprehensive resources. Search now!

 

 
Related Websites
 


Free Email
Your source to free email.

Free Casino Downloads
Contains useful free casino downloads.

Free Software Downloads
A complete collection of free software downloads.

Free
Download free stuff from psike.com your free stuff resource.

Free Teen Online Dating
Meet other singles for free online dating.

Coolfreeweb.com
The Free category at coolfreeweb.com

My Directory Listing
A massive directory listing of websites

Website Collation Project
The website collation project contains a huge directory of websites.


 
Top Webhosts
 
 1. Find free web hosting
 2. free web hosting Web Ho
 3. Web Hosting
 4. Free Web Hosting
 5. Webhosting
 
-
- Give us feedback
 
Last updated
Nov 19, 2008
Free Webmaster Resources .info | A resource must for every web master
Untitled Document - Web Hosting
- PHP Hosting
- Free Internet Access