Science Library - free educational site

PHP variables

Variables are exactly as their name suggests: variable. If a quantity is not a variable (cannot - or should not - change), then it is a constant (always the same).

To define a constant:

define('LANG', 'english');
echo LANG; //Note: the definition requires quotes, but the use does not

To define a variable in PHP, use a dollar sign ($). e.g. the variable which is the name of a customer being called from a database of many hundreds of customers could be defined as: $name

Concatenation

Variables can be defined (or redefined) at any point in a script. They can also be added together: for example, the full name of a customer could take the two parts of the name and combine them: $fullname = $firstname.$lastname

A problem with this example is that the two name parts will have no space between them. The following example demonstrates how concatenation may be used to include non-variable elements, such as a space or a line break (<br />), into a variable:

<?php

$firstname = 'Fred';

$lastname = 'McGurty';

$fullname = $firstname.$lastname;

echo $fullname;

$fullname = $firstname.' '.$lastname;

echo $fullname;

$fullname = $lastname.', '.$firstname;

echo $fullname;

?>

The output of this code is FredMcGurty, Fred McGurty, and McGurty, Fred

Variable names

Variables are case sensitive and can include the underscore. These variable names: \$Customer, \$customer and \$_customer, are all valid and refer to different stored values.

Variable names may include numbers, but the first character after the dollar sign (\$) must be a letter or an underscore (_). $variable1 and $_variable1 are valid, but $1variable is not.

For clarity, a common technique to make variable names with multiple words more readable is to use the 'camelCase'. Instead of making a variable for 'my next great idea' $mynextgreatidea, we use the camel case to make it more legible: $myNextGreatIdea

Assignment Operator =

The equals sign (=) is used to assign a value to a variable. If the variable does not exist, the assignment also creates a new variable with this name. If the variable exists and has a previous value, the assignment replaces the previous value with the new value in the assignment statement. For example:

<?php

$newVariable = '42';

echo $newVariable;

?>

The output will be 42

<?php

$oldVariable = '42';

$oldVariable = '21';

$newVariable = $oldVariable;


?>

The $oldVariable value (42) is replaced by the new value (21). In this example, a new variable is then created and given the same value as the old variable.

Now, examine the difference between these two echo statements:

<?php

echo 'The value stored in the newly created variable $newVariable is '.$newVariable;

echo "The value stored in the newly created variable $newVariable is ".$newVariable;


?>

The outputs are:
The value stored in the newly created variable $newVariable is 21.

Content © Renewable-Media.com. All rights reserved. Created : October 14, 2015 Last updated :March 5, 2016

Latest Item on Science Library:

The most recent article is:

Trigonometry

View this item in the topic:

Vectors and Trigonometry

and many more articles in the subject:

Subject of the Week

Computing

Information Technology, Computer Science, website design, database management, robotics, new technology, internet and much more. JavaScript, PHP, HTML, CSS, Python, ... Have fun while learning to make your own websites with ScienceLibrary.info.

Computer Science

Great Scientists

Rosalind Franklin

1920 - 1958

Rosalind Franklin, 1920 - 1958, English chemist. She supplied the key photograph of x-ray crystallography which demonstrated the helix shape of the DNA molecule, which allowed other researchers (Wilkins, Crick, Watson) to describe the DNA structure.

Rosalind Franklin
Lugano English

Quote of the day...

Whatever your problems with mathematics, I can assure you that mine are greater.

ZumGuy Internet Promotions

SaraOrdine