View Single Post
Old 01-22-2004   #2 (permalink)
Order
Local Biorust Beast
 
Order's Avatar
 
Join Date: Oct 2003
Location: San Diego, CA, USA
Posts: 2,253

Send a message via AIM to Order Send a message via MSN to Order Send a message via Yahoo to Order
This Weeks Guide: January 19th, 2004

I will cover the basics of handling variables, perhaps the most important bit of information to use in PHP. Variables are like cups, that contain water, or in the case of PHP, they hold data. Unlike in C/C++ or a lot of other languages, PHP does not seperate data types (i.e One Vaiable can contain numbers, words, letters, decimals, etc).

Here is a small sample of how to create a variable called hello:

PHP Code:
<?php

  $hello 
"Hello World!";
  echo 
$hello;
?>
If that were to be ran in a *.php page on a server, it would output to the screen the words "Hello World!". The echo function displays the information stored in $hello. All Variables start with $. Now we will do a little math with variables, of course, you cannot add words, so we will use numbers, which are called integers.

PHP Code:
<?php
  $a 
1;
  
$b 3;
  
$c $a $b 3;

  echo 
$c;
?>
This is an interesting peice of code, you can manipulate PHP in such a way as to be able to handle almost any type of math. I chose a simple example here which cancels itself out. First, I assigned a the integer 1, b the integer 3, and then I gave c the value of a * b - 3. PHP will process the math before assigning anything to c, so 1 * 3 - 3 would by 0, hense c would be 0, and that would be what is sent to the screen with echo.

Now, say you have two or three strings that you want to put into one variable, how can you add them together without using math, it is simple, you can combine several variables or strings together using '.', a period! Here is an example.

PHP Code:
<?php
  $name 
"Digital";
  
$hello "Hello, ";
  
$end "!";

  
// Will make those three vars into one.
  
$complete $hello $name $end;

  echo 
$complete;
?>
That simple, you will get "Hello, Digital!" if you were to run it in a php file. As you can see, PHP makes it real easy to handle variables. Next week, we will cover another form of variable, arrays, which bring variables into a new light.
__________________
Order is offline