Please login or register.

Login with username, password and session length
Advanced search  

News:

You need/want an older version of sNews ? Download an older/unsupported version here.

Author Topic: [Tutorial] Variables  (Read 1364 times)

centered

  • Guest
[Tutorial] Variables
« on: December 01, 2009, 02:48:13 PM »

As requested by Sven

Variables.

A variable is a facility for storing data.  In PHP, it is noted as a $ (dollar sign) with the variable name.

A Variable can hold a string
Code: [Select]
<?php

$var 
'string';

?>


A Variable can hold a Array
Code: [Select]
<?php 

$var 
= array();

?>

Or call part of the array
Code: [Select]
<?php

$var 
= array('alpha''beta');
// can also be read or created as
// $var = array( 0 => 'alpha', 1 => 'beta', 'id' => 3 );
$var[0// equals alpha
// $var['id'] equals 3

?>

Variables can be part of either the global or local scope

Variables that are Global are called outside of the local scope (functions/objects) and can be used throughout the script.  We use the following to call global variables in functions:

Code: [Select]
global $var
Example:
Code: [Select]
<?php

$duh 
'duh!';
function 
err() {
  global 
$duh;
  echo 
'uhh '.$duh;
}
err(); // output uhh duh!

?>


A global's value can be altered within a function.  This leads to the global being changed for the rest of the script:
Code: [Select]
<?php

function duhh() {
  global 
$duh// takes the earlier global
  
$duh 'no duhhh!!';
  echo 
'uhh '.$duh
}
duhh();

function 
bleh() {
  global 
$duh// uses duhh() $duh as it was changed
  
echo 'blah '.$duh;
}
bleh();

?>


Static variables are in the local scope.  This means, the variable only exists in the function, but it does not lose it's value after being called.  As with globals, it is called the same way:

Code: [Select]
static $var
Example
Code: [Select]
<?php

function foo() {
static $bar;
if (!$bar) {
$bar = array(0,1);
}
return $bar;
}
$foo foo(); // The array needs to be built
$foobar foo(); // The array is already built and doesn't need to be run again

?>


Variable Function.  Not part of the variable manual, but you may see this from time to time.  The link gives a better explaination.

Variable Functions look like the below and would call the function the $var has been assigned to.
Code: [Select]
$var()
Example
Code: [Select]
<?php

function blah() {
echo 'done';
}

$duh 'blah';
$duh();

?>

Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: [Tutorial] Variables
« Reply #1 on: December 01, 2009, 03:10:35 PM »

A deeper look into PHP variables, courtesy of PHP Variable Basics.

PHP Variables are 'labels' used as a substitute for a value that is used repetitively throughout a PHP function or file. Instead of using the value repeatedly, we use the Variable in its place, This helps us write shorter code especially when the value is long and/or contains several other variables.

All PHP variables start with a dollar sign, followed by the variable-name, and variable-names are case-sensitive.
Examples: $mystory = 'I am Jeremy Radcliffe Roundbottom and I live in a green car with my cat.';
If we needed to use the value 5 times, we would use $mystory each time Instead of  echo 'I am Jeremy Radcliffe Roundbottom and I live in a green car with my cat.';

(done fer now...)
« Last Edit: December 02, 2009, 12:56:20 AM by Keyrocks »
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU