The Brave Programmer - Blogging and coding
Not for the faint hearted
 

Blog Posts From The Brave Programmer

Minimize

PHP For Beginners – Introduction to Variables

Apr9

Written by:
2010/04/09 07:26 AM RssIcon

PHPVariables are an integral part of any programming language. PHP is no exception. While different programs have different rules with regards to variable use and declaration. They are all used for the same thing, that is storing data. Any type of data.

Image via Wikipedia

Image via Wikipedia

What is a variable?

Think of a variable as a little box in memory where you can store stuff. Like a shoe box.  A shoe box might have one shoe or two shoes in it. But then it might have eggs in it, or it might have money in it. What ever data you can think of, your variable should be able to hold it.

Variables are used for storing values, like text strings, numbers or arrays. The beauty about variables is that you can use them over and over again without needing to know what they contain.

Remember algebra in school where a letter stood for some value. Well variables are just like that. x=5, y=10, z=x^y.

A good example is storing the value of Pi in a variable. Then using that simple variable in calculations. Because Pi is a difficult value to remember, putting it into a variable with an easy to remember name, makes it easier to program with.

For example, if you wanted to work out the area of a circle and you had to do it many times in your program. Using a variable is the sensible thing to do.

<?php
$pi = 3.14159265358979323846264338327950288419716939937510582097
4944592307816406286208998628034825342117067;
r = 5;
$AreaOfCircle = $pi*r*r;
?>

Fortunately PHP has a function to return pi to 11 decimals.

Naming rules for variables

Just like any programming language. PHP has some specific rules when naming variables. They are:

  • All variables in PHP start with a $ sign symbol.
  • A variable name must start with a letter or an underscore "_"
  • A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
  • A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)

The correct way of declaring a variable in PHP would be:

$Variable_Name = value;

PHP is a loosely typed language

A strongly typed language like C# means that you have to specifically define the data type of the variable before you use it. Then you can only store that data type in that specific variable.

For example if you want to store an integer value you would have to declare a variable of type integer.

In PHP, a variable does not need to be declared before adding a value to it, nor do they need to be specifically typed.

In the example above, you see that you do not have to tell PHP which data type the variable is.

PHP automatically converts the variable to the correct data type, depending on its value.

In PHP, the variable is declared automatically when you use it.

Using Variables

So now that we now what a variable is, how do we use them?  There are three basic things you can do with variables:

  1. Set them (give them one or more values);
  2. Re-set them if they were set before;
  3. Access them (read the value of a variable and then do something useful with it).

To assign a value to a variable, you use the assignment operator: the = symbol. This is used to assign a value (the right side of the equation) to a variable (the left side). The value being assigned need not always be fixed; it could also be another variable, an expression, or even an expression involving other variables.

To set a variable:

<?php
$age = 42;
$name = "Robert";
?>

To re-set a variable:

<?php
$age = 24;
?>

Access and use the variable

<?php
$age = 42;
$name = "Robert";
echo "My name is ".$name." I am ".$age." years old";
?>

or

<?php
$age = 42;
$name = "Robert";
echo "My name is $name I am $age years old";
?>

Would print out:

My name is Robert I am 42 years old

Conclusion

There are so many uses for variables that it is beyond the scope of this post. Suffice to say that you need to familiarise yourself with variables and their use as you will undoubtedly use them everywhere in your PHP programming.

Next lesson we will be looking at more uses of variables as well as the different  data types you can use with variables.

BTW, here is a question for you. What is the value of z?

Reblog this post [with Zemanta]

Remember you can win a weeks worth of free ad space just for commenting. The best comments during the week will get a weeks free advertising space. Check out: Win free advertising for your blog

New here, or perhaps you've been here a few times? Like this post? Why not subscribe to this blog and get the most up to date posts as soon as they are published.

Get involved in our community. Help promote other bloggers. List your blog in our directory for bloggers. Blog Directory for Bloggers

 



Tags:
Categories:
blog comments powered by Disqus
 
Blog Updates Via E-mail
 Blog Updates Via E-mail
Minimize

Do you want to receive blog updates via e-mail. Then just click on the link below. You will be redirected to Google's feed burner, where you can fill out a form. Supplying your e-mail address.

The subscription is managed entirely by Google's Feedburner. We cannot and do not collect your email address.

Subscribe to The Brave Programmer by Email

Print  
 

 

Latest Comments
 Latest Comments
Minimize
Powered by Disqus

Sign up with Disqus to enjoy a  surprise box of features

Print  
 
Blog Roll
 Blog Roll
Minimize
Print  
 
Categories
 Categories
Minimize
Print  
 
<h1>Search Blogs From The Brave Programmer</h1>
 

Search Blogs From The Brave Programmer

Minimize
Print  
 
Archive
 Archive
Minimize
Archive
<March 2017>
SunMonTueWedThuFriSat
2627281234
567891011
12131415161718
19202122232425
2627282930311
2345678
Monthly
Go
Print  
 
<h1>News Feeds (RSS)</h1>
 

News Feeds (RSS)

Minimize
Print  
 

Follow robertbravery on Twitter

Blog Engage Blog Forum and Blogging Community, Free Blog Submissions and Blog Traffic, Blog Directory, Article Submissions, Blog Traffic

View Robert Bravery's profile on LinkedIn

Mybyte

 

Robert - Find me on Bloggers.com

Tags
 Tags
Minimize
Print  
 
Contact Us Now
 Contact Us Now
Minimize
 

Email  us now or call us on 082-413-1420,  to host your website.

We design and develop websites. We develop websites that make a difference. We do Dotnetnuke Module development.

Web Masters Around The World
Power By Ringsurf
Print