prickett

 

PHP Cheat Sheet

Page history last edited by todd 7 mos ago

PHP Cheat Sheet


 

Constants

 

 

 

<?php 
define( "HOST_SITE", "www.pbwiki.com" );
echo HOST_SITE 
?>

 

 

Results in www.pbwiki.com being printed

 

Strings

 

String values can be delimited using either single or double quotes. Single quotes allow a variable's value to be inserted inside a double quoted string.

 

e.g.

 

 

$firstName = "Bill";
$lastName = "Smith";
echo "First name is '$firstName' and last name is '$lastName'."; 
// previous statement prints:  First name is Bill and last name is Smith.
echo "First name is {$firstName} and last name is {$lastName}."; 
// alternate syntax to previous statement that prints:  First name is Bill and last name is Smith.
echo 'First name is "$firstName' and last name is "$lastName".';
// previous statement prints:  First name is "$firstName" and last name is "$lastName".

 

The escape character is the back slash (e.g. $name = 'Tom\'s Automotive'; )

 

The concatenation character is the period (e.g. $firstName . ", " . $lastName; )

 

Arrays

Arrays don't have to be declared prior to use. The following is permitted:

 

 

$zipCodes[ 0 ] = '45239';
$zipCodes[ 1 ] = '07901';
$zipCodes[ 2 ] = '52100';

 

 

A short cut for the previous example is (notice missing array index numbers):

 

 

$zipCodes[ ] = '45239';
$zipCodes[ ] = '07901';
$zipCodes[ ] = '52100';

 

 

A second short cut for the previous example is:

 

 

$zipCodes = array( '45239' , '07901' , '52100' );

 

 

Arrays can also be indexed by key values:

 

 

$petName[ 'cat' ] = "sparky";
$petName[ 'dog' ] = "fido";
$petName[ 'snake' ] = "larry";
//  Or, using shortcut syntax:
$petName = array( "cat" => "sparky" , "dog" => "fido" , "snake" => "larry" );

 

 

Printing Arrays

The following two commands print out slightly different dumps of the array:

 

 

 

print_r( $petName );
var_dump( $petName );

 

 

Sorting arrays

 

Sorting arrays with numeric indexes: sort( $zipCodes );

Sorting arrays with string indexes: asort( $petName );

 

Walking an array

 

 

 

foreach( $petName as $animal => $name ) {
// $animal is a variable that will contain the key and $name is a variable that will contain the value
echo "{$animal}'s name is {$name}";
}
foreach( $petName as $name ) {
// $name is a variable that will contain the value.  The key is not used.
echo "name is {$name}";
}

 

 

Functions

 

Default parameter values

 

The following function allows for optional parameters, using defaults when parameters are missing:

 

 

function addNumbers( $firstVal = 1 , $secondVal = 2 ) {
return $firstVal + $secondVal;
}
echo addNumbers();  //  prints 3 (uses default vals 1 and 2)
echo addNumbers( 5 );  //  prints 7  (uses passed parm 5 and default val 2)
echo addNumbers( 5 , 10 );  // prints 15  (uses passed parms 5 and 10)

 

 

 

Sessions

Create a session (if one does not already exist)

 

 

 

session_start();

 

 

Session variables

 

 

$_SESSION[ 'favColor' ] = "red";   //  Saves session data
$lastPage = $_SESSION[ 'lastPage' ];  //  Reads session data

 

 

 

PHP4 vs PHP5

 

PHP4

var statement

constructor named for the class

 

class person {

    var $a;

 

    function person() { }

}

 

PHP5

private, protected, public statement

constructor named __construct

 

class person {

    private $a;

 

    function __construct() { }

 

 

 

 

 

 

 

server monitoring

 

 

 

 

 

_uacct = "UA-3717444-1"; urchinTracker();

 

Comments (0)

You don't have permission to comment on this page.