I got a lot of positive comments about my last tutorial. Do you guys think I should keep writitng these?
Intro To PHP
Tutorial 2 - Variables
Released 4/06/07
By Chris Feilbach aka GhostRider
Contact Info:
E-mail: assembler7@gmail.com
AIM: emptybinder78
Yahoo: drunkonmarshmellows
Website: http://www.ghostrider.trap17.com
It has been a very long time since I have released a tutorial. I've been quite busy working on my newest script for handling news (GAMES), and redesigning my webpage.
This tutorial will cover variables, which are what programmers use to store data in. Make sure that you have read the first tutorial, and that you understand everything in this tutorial as well, as this is one of the core fundamentals in any language that you learn.
Anyway you choose to look at it, computer programming is all about manipulating and displaying data. Even the most simple program possible, has to deal with data, the CPU has to read the data, process it, and then send an output. In a computer game, the computer is inputing data from somewhere (keyboard, mouse, joystick), and outputing data thats based on its inputs (screen and speakers). This makes variables, the things that hold data very very important to use and to understand.
Variables in PHP can either hold data or numbers. Numbers are also sometimes called integers. To tell PHP that your variable is truly a variable, put a dollar sign ($) in front of it, like so.
CODE
<?
$var1 = "Hello.";
?>
$var1 = "Hello.";
?>
Variables can also hold numbers:
CODE
<?
$var2 = 22;
$var3 = 67.43009;
?>
$var2 = 22;
$var3 = 67.43009;
?>
One of the most important things in PHP is understanding the difference between strings (a bunch of letters/numbers) in double quotes ("") and single quotes (''). Strings in double quotes allow you to put other variables inside of the string. PHP will then translate that variable into its value. Single quotes will not do this.
For example, lets say that you have a simple script that asks for the persons first name via a form on another page, and then displays it. The name of the person will be stored in $name.
CODE
<?
$name = "Chris";
print "Hello, $name!"; // Will display Hello, Chris!
?>
$name = "Chris";
print "Hello, $name!"; // Will display Hello, Chris!
?>
HOWEVER, if you put it in single quotes
CODE
<?
$name = "Chris";
print 'Hello, $name!'; // Will display Hello, $name!
?>
$name = "Chris";
print 'Hello, $name!'; // Will display Hello, $name!
?>
This is a very important thing to understand, so make sure you really get it.
Inside a quote you can only have two quote marks, one that signifies the beginning of the string, and another that signifies the end. But what if you to want to use quotes inside of a string?
The solution is simple, lets say you want to put the persons name in quotes (""). Use a backslash (\) in front of the quotes inside of the string to avoid parse errors.
Here would be your code:
CODE
<?
$name = "Chris"; // This could also be $name = 'Chris';
print "Hello, \"$name\"!"; // Will display Hello, "Chris"!
?>
$name = "Chris"; // This could also be $name = 'Chris';
print "Hello, \"$name\"!"; // Will display Hello, "Chris"!
?>
There is one more concept that I want to cover before I conclude this tutorial. Strings can be joined using a period (.). Lets say that you have a last name, too.
CODE
<?
$name = "Chris";
$lname = "Feilbach";
print "Hello, $name" . " " . $lname . "!";
?>
$name = "Chris";
$lname = "Feilbach";
print "Hello, $name" . " " . $lname . "!";
?>
Be sure to review this stuff, and make sure you really know it. Next week, or whenever I release my next tutorial, we will cover form data, and conditional statements.


