// References / Variables in PHP

Posted The: 11/05/2008 - AT: 14:43

Variables in php begins with a dollar sign, and must be followed either by an underscore or a letter.

Variables refer to a specific memory location, so that information stored can be easily accessed and manipulated later.

Use of Variables

You should use variables when you want to save information temporarily for later use.

The below example outputs "My name is BlueBoden" in two different ways, both uses the information we stored in the variable $MyName.

Example:

<?php
$MyName = 'BlueBoden';

echo 'My name is ' . $MyName; // Using String Concatenation

echo "My name is $MyName"; // Using Double Quotes
?>

Storing something in a variable, will allow you to use the variable throughout the script, if you want to store the information for longer then that, you may want to consider using a database, or session variables.

Data Types

The data types of your Variables is automatically determined doing execution of your script, therefor you don't need to worry about this. However it is a good idea to read up on it, as in languages like c/c++ asigning data types is done manually.

PHP Data Types:

  1. Numeric
  2. String
  3. Array

The Numeric types are:

  1. Integer
  2. Double

The Integer Type contain number values, while the Double Type contain floating point values, see below:

<?php
$Num = 100; // Integer
$FloatingNum = 99.5; // Double
?>

The String Data Type

Is used to contain textual information like:

<?php
$MyName = 'BlueBoden'; // Only letters
$WhoIs = 'BlueBoden the 1th'; // A combination of letters and numbers
?>

The Array Data Type

Is used to easily store many values, in a single variable. This can be done the following ways:

<?php
$UserInformation[0] = 'BlueBoden';
$UserInformation[1] = 'The 1th';
$UserInformation[2] = 'Active:Admin';

   // The above can also be set the following way.
$UserInformation = array('BlueBoden','The 1th','Admin');

 // The key value dosn't need to be a number, we can also use a string like below.

$UserInformation['Name'] = 'BlueBoden';
$UserInformation['UserCount'] = 'The 1th';
$UserInformation['Status'] = 'Active:Admin';

?>

Arrays might seam complex at first, but they should be easy to work with, knowing the above.

String Concatenation

Another aspect of strings is refeared to as "String Concatenation", this allow you to connect multiple strings, and variables using the "." dot operator.

Example:

<?php
$MyName = 'BlueBoden';
$MyString = 'My name is: ' . $MyName;

  // With multiple variables
$MyName = 'BlueBoden';
$UserStatus 'Active:Admin';

$MyString = 'My name is: ';

echo $MyString . $MyName . "\n" . 'UserStatus: ' . $UserStatus;

?>

Get the syntax right, and you can enter in just about anything, just like you would in your own language.

The "\n" is just a simple linebreak, the reason i used Double Quotes around it, was to make it apear as a linebreak in the final document; had i simply used Single Quotes, it would apear as the characters "\n" instead of the desired linebreak.

Stumble It!

Author: BlueBoden

Comments: [0]

  1. HTML Tutorial
  2. CSS Tutorial
  3. XHTML Tutorial
  4. RSS Tutorial
  5. PHP Tutorial
  6. htaccess Tutorial
  7. Server Tutorial
  8. SEO Tutorial

Checked At:01:47

3 Guests

0 Users

Online doing the past 15 Minutes!

PageRanking.dk - Flere hits og højere PageRank - GRATIS tilmelding!

This page was created in 0.0728368759155 seconds

Welcome Guest