Programming in PHP

4.28 (118)

Variables

A program's main function is to convert inputs to outputs. In order to do this, we need to represent and store data. We usually need to store different types of data, such as integers, fractions and textual data. Hence, we have the concept of variables and variable types.

A variable is simply a space in memory. We use a variable name, to reference to that location in memory so that we can gain the contents of it. So, if we have the variables called height, weight and age, then the physical representation can be thought of as:




As you can see, height is merely an alias to a memory location. In this example we have stored the value 175 in height. If we were to get back the contents of height, we would get 175. We can represent this example in PHP as the following:

Variables
<?php
$height 
175;
$weight 73;
$age 24;
?>


Here we have all three variables represented with their associated values. You will notice that the name of the variable is preceded with a $. This is how we tell PHP that we creating / referencing a variable. The $ is part of the syntax of PHP and must always be used. Another part of the PHP syntax which is crucial to use, is to end each statement with a semi-colon (;). This indicates to PHP that this is the end of a statement, and that it will be starting a new process.

What is you wanted to store somebody's name? Easy! We do the exact same thing, however, this time we must surround the value by single quotes ('). We can actually use double quotes, however, it is recommended to use single, for both clarity and efficiency.

String variables
<?php
$name 
'Bob Bently';
?>


Naturally you may want to change the value of a variable during the program. To do this, you simply change the value of the variable exactly the same was as before. Of course, if we couldn't change the values of variables, they wouldn't be variable and we would once again have static pages. In the below example, we first declare the variable $name to equal Bob Bently and then change it to equal Jane Bently.

Change variable value
<?php
$name 
'Bob Bently';
$name 'Jane Bently';
?>


Text Storing Problems

You may or may not have asked yourself this question, 'What if we want to store text that contains quotes itself?' That is a good question, as currently if you try to, there is probably a 50/50 chance that you will get and error when you try to view the page. The problem is that when PHP is running your script and it comes upon a quote within a quote, it will assume it to be the end of the value you are assigning. Therefore, any text after the quote will be taken as a command, which cannot execute.

Valid program
<?php
$greeting 
"I'm Bob Bently";
?>


Quote error
<?php
$greeting 
'I'm Bob Bently;
?>


The first PHP script works fine because the quotes used are different from each other. The second script will cause an error, and quite rightly so. PHP will see this script as assigning $greeting = 'I' and then will not know what to do with the rest. The solution? Escape sequences.

To escape a character, we use the backslash character \. If the containing quotes are single quotes (') then we must escape \ and ' characters. If the containing quotes are double quotes (") then we must escape the \, & and " characters. So if we want to store these characters, then:

Escape character
<?php
$texta 
'\\, $, \' and "';
$textb "\\, \$, ' and \"";
$othertext $texta;
?>


The $ must be escaped as PHP may think that it is referencing a variable name. The backslash must always be escaped as this is the escape character itself. The above has demonstrated another concept in that we can assign one variable to another variable. So in our example above, the variable $othertext is going to take on the value of $texta.

One last thing to mention before we talk about types, are some of the arithmetic operators we can use on variables. These include +, -, *, /, %, ++ and -- operators.

Arithmetic Operators
<?php
$x 
10
$a 
$x 2;
$b $x 2;
$c $x – 2;
$c $x 2;
$d $x 3;
$e $x++;
$f $x--;
?>


It is obvious what +, -, *  and / do, however, the others may not be so obvious. The modulus % operator performs integer division and returns the remainder. So in this example $d = 1 and 10/3 = 3 remained 1. The ++ and – operators simply add or subtract 1 from the variable, therefore $e = 11 and $f = 9.
Rate this article: BAD 1 2 3 4 5   GOOD
<<     Page 2 of 10    >>

Build Your Own Database Driven Website Using PHP & MySQL

  • Installation instructions for Windows, Linux and Mac OS X
  • Instantly apply working code examples from the book to your Website
  • Build a working Content Management System from scratch
  • Master MySQL database administration
  • Fully updated for PHP 5

       Download FREESample Chapters Now!

Ads

PHPNerds Newsletter