PHP Variables

PHP variables can hold any type of data the language supports, and they don’t need to be declared before they are used. In programmer terms, we refer to PHP as a loosely-typed language.

All PHP variables begin with a $ and are followed by a sequence of characters. Valid characters are letters, numbers, and the underscore. However, a variable may not begin with a number. If it does, the PHP engine will see it and throw a parse error and your script won’t be able to run.

When your script runs, it gets read by the interpreter from top to bottom. A variable won’t have a value until it gets assigned one. Further down in the script you can access that variable’s value.

Assigning a literal value to a variable is pretty simple. You just write something like:

$var1 = ‘I am a variable’;

By the way, when I talk about literals, I mean like a literal 5 or a string inside of quotes. Literals, by themselves, are valid expressions in PHP. They don’t do anything, but they don’t break anything either.

{write ‘next line’; underneath first line}

In this case $var1 now holds the string of characters ‘I am a variable’ that were set inside the single-quotes.

Notice that the statement ends in a semi-colon. All PHP statements end in semi-colons. If you forget them, you’ll trigger a parser error and your script won’t run.

If you’re using an editor like I do, it’ll typically point out any mistakes like that for you. Different editors mark errors differently, but mine just displays a red wavy underline.

{show editor and point out mistake}

In the case of a missing semi-colon, notice the error is actually marked on the next line. This is because PHP ignores white-space. You could stick your semi-colon as far apart from your statements as you like really. So in this case, the error is I’ve started my next statement before closing the previous one.

Now, if you’re familiar with a more strictly-typed language, PHP’s looseness can look pretty strange. There is nothing wrong with suddenly changing my variable from one type to another.

$var1 = 5.8; //now it’s a float!
$var1 = true; //now it’s a boolean
$var1 = 1; //now it’s an int

And if I suddenly start using an undeclared variable as, for example, an array, PHP just creates an empty array in that variable’s place and then starts acting on it however I’ve told it to.

$var2[0] = 5; //$var2 is suddenly an array with key ‘0’ set to the value ‘5’

Likewise, if I use the increment operator – two plus signs together, PHP assumes I want an int, sets its value to 0, and then increments it like I asked, so I end up with the int ‘1’.

$var3++; //when I check it next time, $var3 will equal 1

PHP is so loosely typed that you can even mix operand types in some cases. If you’re unfamiliar with the terminology, an operand is an expression on one side of an operator, like a plus sign. So for instance, you can concatenate a number with a string. It just treats the number as though it were the string representation of it, and adds it to the string:

$var1 = 1;
$var2 = ‘Hello There! ‘ . $var1;  //$var2 is now ‘Hello There! 1’;

It works the other way too. If you try to perform a math operation on a string, it just converts the string to a numeric representation and then proceeds with whatever math you wanted to do. The way it does this is by only looking at the beginning of a string. If there’s a number there, that’s the number it uses it as. If not, then it just gets used as a 0.

$var1 = ‘Hello’;
$var2 = 3+$var1; //$var2 is now 3
$var3 = ’12 cans of rice’;
$var4 = 5*$var3; //$var3 is treated as 12, so $var4 is now 5*12, which is 60

Mixing operand types only works in some cases. It depends on the types and it depends on the operator. We’ll look more at both variable types and operators in later videos.

It might look strange if you’ve ever coded with strict types, but it’s actually pretty freeing once you’re used to it. Of course, it makes it that much easier to make a mistake. If you have a variable called ‘$myWritable’, and you use it all throughout your script, but then in one place you accidentally misspell it as ‘$myWriteable’, PHP will simply assume you want a new variable, default its value to null, and then use it for whatever you tell it to. Unless you use it in a way that will specifically crash if the value is null, you won’t get an error. You’ll simply not get the results you expect, and it can be a pain to have to track it down and fix it.

February 05 2010 09:43 am | Basics and PHP Programming Basics and PHP tutorial scripts

Comments are closed.