Home Tutorial PHP Lesson 04 - Setting and printing variables



MSN Random Online

Lesson 04 - Setting and printing variables PDF Print E-mail
Written by Administrator   
Thursday, 15 February 2007 18:10

Lesson 04 - Setting and printing variables

I've explained to you what a variable is; now let me show you how to create a variable.  A variable, as stated in the previous lesson, is something which can hold a dynamic value.  A variable is distinguished from other things because it starts with a dollar sign.  For example, "$name" could be a variable which would most likely hold the name of something, someone, or someplace.  Variable declaration requires the use of the "assignment operator" ("=") which will assign a value to our variable.

What you assign to a variable also requires a little thought.  If you're assigning a number to a variable, then quotes are not required.  If you're assigning a string to a variable, then quotes are required.  The two basic types a variable can be are "string" and "number" and PHP understands which one you want with the presence/absence of quotes.

Code:
$name = 'Parham'; //this will assign 'Parham' to the variable $name, note the quotes

$number = 12; //this will assign 12 to the variable $number, note the absence of quotes

$othernumber = '12'; //this will assign '12' to the variable $number, not the presence of quotes, the variable is now interpreted as a string, not as a number

If you literally want to use quotes in your strings then they will have to be "escaped" so PHP will not get confused:

Code:
$phrase = 'hi my name\'s Parham'; //by placing a \ in front of the single quote, the variable will now be set properly and you will not receive errors

Here is how PHP will read the above, step by step:
-Interpret right side of assignment operator first
-see a single quote, recognize this as a string, continue until you find another quote
-found a quote, but it's escaped, continue looking for another quote
-found another quote
-assign everything from first quote to last quote ('hi my name\'s Parham') to the variable $phrase
-found semi-colon, this line is done with, can now move onto the next line (the semicolon tells PHP when to move onto the next command (usually on the next line).

Hopefully you're following everything well, I've tried to keep everything as simple and clear as possible.  Please don't hesitate to post questions.

The next thing we'll discuss is variable interpolation.  The word "interpolation" means to insert or introduce between other elements or parts.  Variable interpolation therefore means to use one variable inside another.  Let us for example declare two variables:

Code:
$name = 'Parham';
$age = '19';

To use these two variables and declare a whole new third variable I'd do this:

Code:
$phrase = "$name is $age years old";

Note the use of double quotes.  Doubles quotes and single quotes have very different meanings when it comes to declaring variables.  Single quotes take everything literally, meaning no variables are ever interpolated whereas double quotes interpolate variables:

Code:
$phrase1 = '$name is $age years old'; //this would literally be assigned to $phrase

$phrase2 = "$name is $age years old"; //this would find the values of $name and $phrase and stick them in

print() and echo() are two PHP language constructs which allow you to output information to the user.  It is best to demonstrate these two functions without giving too much explanation.  Here is an example that will not only show you everything we've learned about variables above, but it will also show you examples of the print() and echo() functions:

Code:
<?

$name = 'John';
$age = '19';
$location = 'Canada';

//print examples
print('Hello'); //outputs "Hello" to the user
print 'Hello'; //the brackets aren't required, also outputs "Hello" to the user
print 'Hello'.' how are you?'; //the "." is the concatination operator, it will join two strings
                               //this will print "Hello how are you?"

//-----------------------------------------------

//echo examples
echo('Hello'); //outputs "Hello" to the user
echo 'Hello'; //the brackets aren't required, also outputs "Hello" to the user
echo 'Hello'.' how are you?'; //the "." is the concatination operator, it will join two strings
                              //this will print "Hello how are you?"
echo 'Hello',' how are you?'; //the "," can also be used with echo() to output a string
                              //this will print "Hello how are you?"

//-----------------------------------------------

//print with variables
print $name . ' is ' . $age . ' and lives in ' . $location;
//will output "John is 19 and lives in Canada"

//-----------------------------------------------

//echo with variables
echo $name . ' is ' . $age . ' and lives in ' . $location;
//will output "John is 19 and lives in Canada"

echo $name , ' is ' , $age , ' and lives in ' . $location;
//will output "John is 19 and lives in Canada"

//-----------------------------------------------

//print and echo with variable interpolation
print "$name is $age and lives in $location";
//will output "John is 19 and lives in Canada"

echo "$name is $age and lives in $location";
//will output "John is 19 and lives in Canada"

?>

It is also worth mentioning that you only need to escape characters that you use to surround the entire variable's value with.  For example:

Code:
print "I said \"hi\""; //need to escape the double quotes around "hi"
print 'I said "hi"'; //don't need to escape the double quotes around "hi"
print "I said 'hi'"; //don't need to escape the single quotes around 'hi'
print 'I said \'hi\''; //need to escape the single quotes around 'hi'

There are speed differences between single quotes and double quotes and also between the echo() function and the print() option.  First you should know that using concatenation operators (".") with print() and (",") with echo() is MUCH faster than interpolating and letting PHP figure out where the variables are.  You should also note that echo() is slightly faster than print() because print returns a "true" value (in the form of "1") to tell you it has printed.

For more information, please read http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40

 

 
 

Top Members

No top members yet.