Wednesday, October 05, 2005

PHP Tips -- Array & Class

Array
* Create a numerical array with more than one elements:
$colors = array("red", "yellow", "black", "green");

* Create a associative array with more than one elements:
$member = array(
"name" => "Bob",
"age" => 30,
"occupation" => "superman",
);

* Array functions:
array_merge();
// different with ( $array1 + $array2 ).
// the later will completely preserve the arrays and just append them to each other

$arrA+$arrB : combine two arrays

array_unique();
// Two elements are considered equal if and only if
// (string) $elem1 === (string) $elem2.
// In words: when the string representation is the same.


$codes = implode(",", $codesarray);
// Join array elements with a string

$codesarray = explode(',',$codes);
// Split a string by string

$num1=count($codes);
// Count elements in an array, or properties in an object

split -- split string into array by regular expression
array split ( string pattern, string string [, int limit] )


Class

class myclass{
var $name;

function myclass($n) { $this->name =$n; }

function sayhi(){
echo "Hi! My name is ". $this->name;
}
}

class childclass extends myclass {

function myclass($n)
{
parent::myclass($n);
}

function sayhi(){
echo "Hi! My name is ". $this->name .". How are you?";
}

}

$obj1 = new childclass("Bob");
$obj1->sayhi();

No comments: