What is a PHP variable? 02/11/11
A variable is something that stores a value. It can hold text or numbers and reused as many times as you need throughout your code.
Brandon Waring - Ma, Boston
Web Design: Web Design & SEO Company
What is a variable
A variable is something that stores a value. It can hold text or numbers and reused as many times as you need throughout your code.
Here are a few examples of variables
$var = ‘George’;
$Var = ‘Washington’;
echo “$var $Var”; // output “George Washington”
$5steps = ‘invalid’; // Invalid – Cannot start with a number
$_5steps =’valid’; //Valid – Starts with an underscore
$hello = ‘Hello World’;
$hello = ‘Goodbye World’;
echo $hello; // output Goodbye World
?>
Notice how the two variables, despite the same letters, hold different values. That is because they are case sensitive. You should always stay away from having variables with the same name, it can get confusing.
Variables cannot start with a number, they can however start with an underscore. If you use an underscore before a number, then the the variable can contain a number in front of the name.
You can overwrite variables by re-declaring it. Notice how the variable $hello defined “Hello World”, then it was redefined as “Goodbye World”. This brings me back to using similar names. If you accidentally use the wrong variable, you can redefine it and mess up the works.
A variable should be named something that will be easy to remember when you are writing your code. Good examples of variables are:
$value_1 = 12;
$value_2 = 34;
$add_values = $value_1 + $value_2;
echo $add_values; // output 46;
?>
Media Monster webdesign offers custom website design for all size businesses. Our full service website design company only hires exceptional website designers. Please contact us today at 978-921-0012 for your next web site design project.
Did you like this article? Share it with your networks!
You can leave a response, or trackback from your own site.

Leave a Reply
You must be logged in to post a comment.