|
Helping ordinary people create extraordinary websites! |
How to Setup and use Printing Variables in PHPBy Darren W. Hedlund2005-06-21
How to Setup and use Printing Variables A variable, 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.
$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:
Here is how PHP will read the above, step by step: 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:
To use these two variables and declare a whole new third variable I'd do this:
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:
$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:
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:
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. Tutorial Pages: » How to Setup and use Printing Variables |
|