Search
Left Quote    Nearly all men can stand adversity, but if you want to test a man's character, give him power.
- Abraham Lincoln    
Right Quote
 
[login] | [Register]
 

Introduction to PHP

by: Jester
Page: 2 of 7
(View All)

Got past the opening page, eh? In this section of the tutorial we’ll learn how to use variables in our PHP scripts and, most importantly of all, why we use them.

Variables

Using variables in your PHP documents is very easy, this is how variables look:


A variable starts with a dollar sign ($) followed by whatever you want to call it, variable names can consist of letters, numbers and the _ character. However, do not start variable names with a number.



What Do They Do?

Variables store a value, whether it be a sentence (string), number (integer) or a letter (char). You assign a value to the variable using the assignment operator (=).



We just assigned the value of 1 to the variable $variable. This $variable is now 1, so if we printed it out, it would print 1.



We can assign a variable a number, a letter or a sentence.



If we then used echo to print out these variables, the output would be whatever the variable contains. Variables are essential and you must learn how to use them, let’s take a look at the semantics of using the echo statement with variables.

Echo and Variables

Up to now we have only used the echo statement with a string and with a single variable. How you use the echo statement, or more specifically if you use single quotes, double quotes or no quotes at all depends on what you are printing out.

Single Quotes

We use single quotes when we want to print out a something that contains no variables (or arrays, we’ll get to them next, ignore for now), take a look:



That works fine, it will print out the string.

No Quotes

If we are printing out a variable, and nothing but a variable, we don’t need to surround it by quotes at all, take a look:


    Note:
this is also valid if you are just echoing or assigning a number value, and only a number value.

Double Quotes

What if we want to print out a variable within a string or something? Then we use double quotes, take a look:



If a variable is to be printed out amongst the words of a string then the whole thing must be contained within double quotes. If we wish to interpolate a variable into a sentence this is how we do it. Why, though? What if we used single quotes?



My name is $variable

Not what we want, is it? We want the value of the variable to be printed, not the name along with the dollar sign, this is the difference between single quotes and double quotes. Use single quotes when just echoing out a normal string, if it contains a variable, use double quotes.

This applies not only to echo, but with everything in PHP, when assigning a variable to another variable we use the same rules.



$myname would contain "My name is $variable", if we wanted to assign the value of the variable to $myname we’d use double quotes:



That’d work as we want, remember that, use quotes only if you need them, and use the correct ones.

An Alternative

Another method we can use when wanting to print out a variable is jumping out of the quotes, take a look:



And again:



The latter of the two examples may not make grammatical sense but it shows you how you use the periods to attach the variable into the string. Close the quotes, type a dot, type the variable and then, if you need to, you can type another dot and open the quotes again. Have a play around with variables, echo and assignments, you’ll see what I’m talking about.

It wasn’t really necessary to show that last method, but if I didn’t I probably would have gotten a comment at the bottom by some PHP “guru”:

<sneer>This tutorial sucks, you didn’t even mention the dot thing, hahaha call yourself a PHP coder? Stop polluting my screen with this crap!</sneer>

Indeed. So there you have it.

But, why?

Well … the reason we used variables in the above examples is still unclear. Why not just write “My name is Jester”? Well what if we wanted to use the value “Jester” through out a script many times? We could just type “Jester” in every place we wanted to use it. What if I changed my name, though? I’d have to go through and change every instance of “Jester” to my new name, if I had it stored in a variable I could just assign the variable a new value and it’d take effect in every instance I used it. This is the power of using variables.

Another point is, what if a user submits the value of “10” to a script? You can’t just store “10” on its own in a program, can you? You need to put it somewhere, and what better place than a variable?

Still unclear? Don’t worry about it, at this point in my PHP lessons I was very hazy too. You got this far, keep reading and you’ll see why we use them. Let’s go on and look at Arrays.



1  |  2  |  3  |  4  |  5  |  6  |  7  |  
Next »


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

320,834 Views
9 Total Comments
5 Rating of 5 (2 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· Simple PHP Tutorial
· One File Website
· Pagination with PHP
· Building a Comments Script
· Complete Members System
· Variable Scope



"AllSyntax.com" Copyright © 2002-2017; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank