PHP – Variables

A variable is simply a container that’s used to store both numeric and non-numeric information. And just as with any container, you can move it from  place to place, add stuff to it, or empty it out on the floor in a pile and fill it with something completely different.
PHP has some simple rules for naming variables. Every variable name must be preceded with dollar($) symbol and must begin with a letter or underscore character, optionally followed by more letters, numbers, or underscore character. Common punctuation characters, such as commas, quotation marks, or periods, are not permitted variable names; neither are spaces. So, for example, $root, $_num and $query are all valid variable names, while $58%, $1day, and email are all invalid variable names.
Assigning Values to Variables
Assigning value to a variables in PHP is quite easy: use the equality(=) symbol, which also happens to be PHP’s assignment operator. This assigns the value on the right side of the equation to the variable on the left.
To use a variable in a script, simply call it by name in an expression and PHP will replace it with its value when the script is executed. Here’s an example:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
<html xmlns=  xml:lang=”en” lang=”en”>
// assign value to variable
$name= ’Anuj’;
<h2> Welcome to<? Php echo $name; ?>’s Blog!</h2>

In this example, the variables $name is assigned the value ‘Simon’. The echo statement is then used to print the value of this variable to the Web page.
You can also assign a variable the value of another variable, or the result of calculation.
The following example demonstrates both these situations:

//assign value to variable
// assign value to another variable
$currentYear = $now;
//perform calculation
$lastYear = $currentYear-1;
//output: ‘2007 has ended. Welcome to 2008!’
echo “$lastYear has ended.  Welcome to $currentYear”;

Destroying Variables
To destroy a variable, pass the variable to PHP’s aptly named unset() function, as in the following example:

//assign value to variable
$car = ‘Porsche’
//print variable value
//output: ‘Before unset(), my car is a Porsche’
Echo “Before unset(), my car is a $car”;
//destroy variable
//print variable vale
//this will generate an ‘undefined variable’ error
//output: ‘After unset(), my car is a’
echo “After unset(), my car is a $car;

Inspecting Variable Content
PHP offers the var_dump() function, which accepts a variable and X-ray it for you.
Here’s example:

//define variables
$name = ‘Fiona’;
$age = 28;
//display variable contents

Recommended Tutorials for you

Related posts

How to fetch data from mysql using PHP

Anuj Kumar

How to Fetch data from database Using OOPS In PHP

Anuj Kumar

How to check Email and username availabilty live using jquery/ajax

Anuj Kumar

JQuery Ajax Tutorial – Learn About Developing Effective Next Generation Web Application

Anuj Kumar

PHP Echo / Print

Anuj Kumar

PHP Variables

Anuj Kumar

Conditional Statement

Anuj Kumar


Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.