PHP Variables

Posted: January 29, 2009

Variables? Like algebra?
Yep. Like algebra.

Just like in algebra, variables in PHP will substitute for a value. But a variable in programming is different from in math, because a variable doesn’t just stand for numbers. You can use a variable to stand for just about everything from numbers, to characters of the alphabet, strings of text, entire sets of different values, even other variables!

Every programming language has variables. PHP and JavaScript both have variables. PHP makes it easier to identify variables than a lot of other languages because you start them with a special character, $.

Another thing that is different about PHP’s variables is that with some other programming languages, you have to declare what kind of datatype a variable is in advance, and then it can only be that kind of data. So you would have to declare a variable as being an integer, and then you would not be able to put any other kind of data, like text strings, in that variable. Not so with PHP. You can keep changing the values of a variable from an integer to text or date, and PHP will keep track automatically of what kind of data is in the variable.

This isn’t necessarily all good though, because it means you have to be very careful when you try and access data that you’re treating it right, and you’re not trying to use a text string in a math formula after you changed the variable data.

Let’s take our original code, and change it a bit:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My First PHP Project</title>
</head>
<body>
<?php
$message = ‘Hello, world!’;
echo $message;
?>
</body>
</html>

See what we did there? We stored the value “Hello, world!” inside the variable $message. Then whenever we used message, it treated it just like “Hello World”.

All about PHP Quotes

There is something important to notice about the example in the last PHP lesson. When we called $message later, we didn’t use quotation marks around it. That’s because even though the variable is referring to a text string, it’s still a variable and not a string itself. If we put “$message” with the quotation marks, you might literally send $message to the browser, so your visitor would see…

$message

…on their screen. Actually, if you use double quotes around $message, the PHP interpreter would try and interpret any variables it sees inside of the quotes. So you could put:

echo “$message It is good to meet you.”;

and the php interpreter would send to your browser:

Hello, world! It is good to meet you.

However, if you use single quotes, like:

Echo ‘$message It is good to meet you.’;

Your visitor would literally see:

$message It is good to meet you.

Which wouldn’t make very much sense at all.

So the difference between using single quotes and double quotes, is that PHP will try and interpret the double quotes, including any variables included inside the text string. But with single quotes, PHP will use whatever is literally in between the quotes.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Yahoo! Buzz
  • Propeller
  • Twitter

Leave a Reply