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:
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:
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:
The output of this code is
Fred McGurty, and
Variables are case sensitive and can include the underscore. These variable names:
\$_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 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:
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:
The output will be
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
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
The most recent article is:
View this item in the topic:
and many more articles in the subject:
Mathematics is the most important tool of science. The quest to understand the world and the universe using mathematics is as old as civilisation, and has led to the science and technology of today. Learn about the techniques and history of mathematics on ScienceLibrary.info.
1910 - 1995
Subrahmanyan Chandrasekhar, 1910 - 1995, was an Indian astrophysicist, born in Punjab, and worked in the USA. He made significant contributions to many fields, including General Relativity and Black Holes.
If your experiment needs statistics, you ought to have done a better experiment.
Website © renewable-media.com | Designed by: Andrew Bone