Basics
Creating a Document
Head and Body Elements
Formatting Text
Creating Lists
Embedding Images
URLs Explained
Linking to Documents
Creating Tables
Forms Form Basics
Input and Textarea
Select Lists
Frames Framesets
The Frame Element
Nested Framesets
Targetting Frames
Inline Frames
Tips and Tricks Meta Tags
Transition Effects
HTML Generators Create a Document
Create a List
Create a Table
HTML
Basics
Implementing CSS
CSS Syntax
Pseudo Classes/Elements
CSS Classes
CSS Properties Font Properties
Color and Background
Text Properties
Border Properties
Margins and Padding
Size and Position
Tips and Tricks Menu Buttons
Special Effects
CSS
Basics
Running a Script
Variables
Expressions and Operators
Objects.Properties.Methods
The Date Object
Strings
Regular Expressions
Defining RegExp Patterns
Branches and Conditions
Loops
Arrays Array Basics
Array Methods
Sorting Arrays

User-Defined Functions
Cookies
Windows
Frames
Tips and Tricks Image Replacement
Using Includes
Form Validation
Debugging
JavaScript
Basics
Creating a Script
Running a Script
Variables
Expressions and Operators
Strings Strings Basics
Strings and Substrings
Replacing Substrings
Regular Expressions
Branches and Conditions
Loops
Arrays Array Basics
Array Functions
Sorting Arrays
User-Defined Functions
Include and Require
Uploading Files
File Functions
Session Variables
Tips and Tricks Page Templates
Form Reply Scripts
Form Validation
JavaScript to PHP
PHP
Basics
Create and Drop
Show and Describe
Insert, Update and Delete
Querying
Join Queries
Functions
Table Locking
PHP/MySQL Functions Accessing a Database
Querying with PHP
Create and Drop with PHP
Insert and Update with PHP
Frequently Used Functions MySQL
Basics
Layout and Navigation
Page Content Style
Web Page Copy
Graphics and Animation
HTML Forms
Accessibility
Legal Requirements
MySQL

Home > PHP > Variablesprinter version

PHP Variables

If you have no programming experience then you may be unfamiliar with the concept of variables. A variable is a value, stored in the computer's memory, which can be varied.

To be able to read a variable's value or alter its value we don't need to know where it's stored in memory - PHP takes care of that - we just need to know its name.

Variable Names

There are naming conventions that must be followed. Variable names ...



Variable names are usually chosen to reflect their use and if two words make things clearer then underscores or capitalization is normally used e.g. $customer_count or $CustomerCount.

Variable Types

PHP supports four scalar types of variable and two compound types. A scalar variable can only hold one value at a time whereas a compound variable can hold more than one value (compound variables are covered in a later tutorial).

Scalar Variable Types



Using Variables

With many scripting languages a variable's name and type must be declared before it can be used. PHP does not require this and although it's convenient, it can lead to hard-to-find bugs.

For example, you may have created a string variable called $convenient and at some point in the script you wish to print out the value of this variable.

When using echo($convenient) you make a typing error and use echo($conveinent) instead. The script will treat the typing error as a new empty variable and print out nothing.

This wouldn't happen with languages that require a variable to be declared since the script would stop with an "undeclared variable" error.

Variable Assignment

In PHP variables are created and a value assigned in one statement. The assignment of the value implicitly defines the variable's type i.e. integer, float, string etc.

Examples of Valid Variable Declarations
<?php
    $var_1 = 12;
    $var_2 = 6 +3;
    $var_3 = (2 + 3) / 4;
?>


In the examples above the $var_1 and $var_2 assignments implicitly define the variables as integer type. With $var_3 the assignment implicitly defines the variable as a float.

Operating on Variables

It's common to want to add or subtract 1 to or from a variable (increment or decrement) and this can be achieved in different ways.

Examples of Variable Operations
<?php
    // Different ways to increment $var_1
    $var_1 = $var_1 + 1;
    $var_1 += 1;
    $var_1 ++;

    // Different ways to decrement $var_1
    $var_1 = $var_1 - 1;
    $var_1 -= 1;
    $var_1 --;
?>


<?php
    // Different ways to double $var_1
    $var_1 = $var_1 * 2;
    $var_1 *= 2;

    // Different ways to halve $var_1
    $var_1 = $var_1 / 2;
    $var_1 /= 2;
?>


Single and Multi-line Comments
<?php
    // A single line comment

    /* An example of a multi-line
    comment that stretches
    over more than one line */
?>


Multi-line comments are useful when debugging scripts as they allow you to comment out whole sections of code rather than delete them.

String Variables

A string variable consists of one or more characters
e.g. $my_string = "Hello there";.

Strings can be added together (concatenated) using the . character.

Examples of Concatenation
<?php
    $my_string = "The beginning" . " and the end";

    $first_name = "Charles";
    $second_name = "McDonald";
    $age = 40;
    $full_name = $first_name . " ". $second_name;
    $about_name = $full_name . " is aged " . $age;
?>


Constants

Constants, as the name suggests, are values that don't change. A constant is used to set a constant parameter that can be accessed from any point in the script (for example the maximum number of names or addresses to display on one page).

Names for constants follow the same rules as for variables but are not preceded with a $ sign and by convention are in upper case.

Example of PHP Constants
<?php
    define("MAXIMUM_RESULTS", 100);
    define("MINIMUM_RESULTS", 10)
?>


Previous - running a script Previous - Running a Script     Next - Expressions and Operators Next - expressions and operators


Privacy | Terms | Contact | Links | Sitemap | RSS Feeds RSS and JavaScript Feeds
©2010 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2010
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
PHPBasicsCreating a ScriptRunning a ScriptPHP VariablesVariablesExpressions and OperatorsStringsStrings and SubstringsReplacing SubstringsRegular ExpressionsBranches and ConditionsLoopsArraysArray FunctionsSorting ArraysUser-Defined FunctionsInclude and RequireUploading FilesFile FunctionsSession Variables
Recommended Reading
PHP and MySQL web development

programming PHP

PHP cookbook
Tips and TricksPage TemplatesForm Reply ScriptsForm ValidationJavaScript to PHP
Got any PHP Tips?
Send me your tip and if it's suitable I'll put it on the site, credit it to you and add a link back to your site.