Common Code

This page contains common code for PHP, with links to pages in the PHP manual


Superglobal Arrays

//To see current values of variables use print_r(SUPERGLOBALVARIABLE)

$_SERVER

  • 'PHP_SELF' - returns the filename of the currently executing script, relative to the document root

$_GET - used to pass values via URL

URLADDRESS?variablename=VALUE
<?php
$variable = array_key_exists ('variablename', $_GET) ? $_GET['variablename'] : "defaultvalue" ;
?>

$_POST - used to pass values from Forms

$_COOKIE - used to set and read cookies

  • Add: setcookie ("NAME","VALUE",time() + (60*60*24*7*365)) ;
  • Read: $Variable = $_COOKIE['COOKIENAME'] ;
  • Edit: setcookie ("NAME","NEWVALUE",time() + (60*60*24*7*365)) ;
  • Delete: setcookie ("NAME","")) ;
  • Determine presence: if (IsSet($_COOKIE['NAME'])) { } ;

$_FILES - handles file uploads to server

$_ENV - server environment

$_REQUEST - $_GET, $_POST, $_COOKIE variables

$_SESSION - session variables

<?php
session_start() ; //Add this to top of each page before header html
$_SESSION['NAME'] = $variable ;
?>


Variable Scope

global - access variables from outside functions
function NAME()
{
global $variable
}

static - retain variable value between function calls
function NAME()
{
static $variable
}

define - constant
define ("CONSTANTNAME", "VALUE") ;


Expressions

variable: $variable = value ;

function
<?php
function Function_Name ($Parameter, $DefaultParameter=Value) {
return Value ;
}
?>

ternary conditional operator
$variable ? value if True : value if False


Operators

string operator - string functions

concatenation
<?php
$variable1 = "Value1" ;
$variable2 = "Value2" ;
$variable3 = $variable1 . $variable2 ;
?>

comparison operator - =,==,===, !, <,>

logical operator - and, or, xor, not, &&, ||

arithmetric operator - math functions

array operator - array functions

<?php
$ArrayName = array(Value1, Value2) ;
echo $arrayName [0] ;
foreach ($ArrayName as $ArrayItemValue) {
echo "$ArrayItemValue" ;
}

<?php
$ArrayName = array("KeyName1" => Value1, "KeyName2" => Value2) ;
foreach ($ArrayName as $ArrayItemKey => $ArrayItemValue) {
echo "$ArrayItemKey ($ArrayItemValue)" ;
}
?>

<?php
$ArrayName = array("KeyName1" => array(Value1, Value2), "KeyName2" => array (Value1, Value2)) ;
}
?>


Control Structures

if - if, else, elseif

<?php
if ($variable1=="Value1") && ($variable2=="Value2") || ($variable3=="Value3") {
CODE ;
} elseif ($variable4=="Value4") {
CODE ;
}
} else {
CODE ;
}
?>

switch

switch ($variable) {
case value1:
CODE ;
break ;
case value 2:
CODE ;
break ;
case value 3:
CODE ;
break;
}

while loop

<?php
$i = 1 ;
while ($i <= VALUE) {
CODE ;
$i++ ;
}
?>

do while loop

<?php
$i = 1 ;
do {
CODE ;
$i++ ;
} while ($i <=VALUE) ;
?>

for loop

<?php
for ($i=1; $i<=VALUE; $i++;) {
CODE ;
}
?>

foreach loop

<?php
foreach ($ArrayName as $ArrayItemValue) {
echo "$ArrayItemValue" ;
}
?>

break

continue

declare

include - also see include-once, require, require-once

<?php
include 'PathName.FileName.Extension ;
?>

Page tags: php web
page_revision: 35, last_edited: 1224348280|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License