SHARE
Facebook X Pinterest WhatsApp

PHP Tutorial: Variables

Written By
thumbnail
Vince Barnes
Vince Barnes
Jan 4, 2005

Almost everything you want to do in a PHP script is going to
involve moving some information around or otherwise manipulating it.  Much
of this information will be held in variables.  It is therefore a good idea
for us to take a quick look at what they are and the basic rules for their use.

Variables hold one or another "type" of information.  PHP
is "loosely typed," meaning that the type of information to be held in a
particular variable can change on the fly.  JavaScript is that way too, so
maybe you’re already familiar with loose types.  Changing on the fly means
that if you start with a variable holding numerical data and use it in
calculations, for example, and then write an instruction to put text into it,
PHP will then consider it to be a text variable.

Here are the main data types used in PHP:

  • integer
  • double
  • string
  • Boolean
  • array
  • object

whole numbers
real numbers
strings of characters (text, etc.)
true/false
a set or group of variables of the same type
class instances (more later!)
 

An array needs a few words at this point.  A simple example
of an array would be a string array called $month.  There could be twelve
in the set and they could have the values January, February etc.  To
reference the first of the set we use an index value thus:

$month(1)

In our example this would refer to array element with a value
of:

January

The index could also be a variable, thus:

$monthnum = 2;
echo $month($monthnum);

Assuming the array had been defined with the values descried in
our example, this would echo:

February
 

You’ve probably heard of type casting, but maybe not the kind
I’m thinking of here!  This has nothing to do with thespian arts – type
casting in PHP means telling PHP to treat a variable of one type as if it was
another (without actually changing its type.)  It doesn’t really matter
right here why you’d want to do that – it’ll become clearer later.  For
now, just take a look at this example:

$quantity = 0;
$amount = (double)$quantity;

$quantity is set up as an integer with a value of zero. 
The next line treats $quantity as a double type and assigns its value to
$amount.  $amount is now a double with a value of zero and $quantity is
still an integer with a value of zero.
 

In PHP you can even use a variable as the name of a variable. 
Take a look at this:

$varname = ‘quantity’;
$$varname = 5;

The second line uses the $varname variable in the name of the
variable being assigned the value of 5.  Consequently the variable
$quantity is assigned the value 5.  It does exactly the same thing as:

$quantity = 5;

This neat little trick hints at some of the power hidden in PHP. 
The usefulness of this trick will become apparent when we do some processing in
loops later on.

 

Continue to the next part of this Tutorial


Return to the Tutorial Series Index

Recommended for you...

PHP vs Javascript: Compare Web Development Languages
How Laravel Speeds Up App Development
Rob Gravelle
Aug 29, 2018
7 Ways to Start Learning JavaScript as a WordPress Developer
Exploring PhpSpreadsheet’s Formatting Capabilities
Rob Gravelle
Feb 26, 2018
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.