Reading Note 3 – PHP 5 Advanced OOP and Design Patterns

phpoo

Basic but you may not know php syntax

1. use variable as variable name

$file1 = "hello";
$file2 = "world";
$file3 = "php";

for($i=1; $i<4; $i++){
   $filename = "file" . $i;
   echo $$filename . "\n";
   //or ${$filename};
}

2. if set a variable as NULL, then this variable will be noset (!isset)

$a = null;
if(isset($a)){
   echo "you cannot see this";
}
else{
   echo "set to null means no set";
}

3. Check if all variable have been defined using isset

if(isset($var1,$var2,$var3)){
   echo "yes, all set";
}

4. The Difference between empty and isset is that empty is to check if a variable is not set or is assigned a false value. isset is check if a variable is set.

5. What's superglobals? As a general rule, PHP does not support global variables. But there are some variables called superglobals, which are predefined by PHP and can be accessed from any scope. For example, $_GET, $_POST, $_COOKIE, $_SESSION, $_ENV, $_SERVER.

6. String Offerset - use to easy manipulate the any char in a string.

 
$str = '17';
$str{1} = '2'; //replace the second position of str to be '2', so the str is '12' now
$str[6] = 7; //php4 syntax, still valid in php5, but should use {} instead in php5
echo $str; //so now the str is '12    7'. after 2 there are 4 empty chars.

7. true or false of Array, Object, Resource.
a. If $arr is an empty array, $arr is false, otherwise, it's true
b. If $obj is an object, and $res is a resource, then $obj and $res are always true value

8.each() and reset()
The each function returns the current key/value pair and advances the internal pointer to the next element. Note that The each function returns an array with 4 pairs of items. For example:

$ages = array("John" => 28, "Barbara" => 67);
//reset($ages); //reset if needed
$person = each($ages);
print_r($person);
/*
Array
(
    [1] => 28
    [value] => 28
    [0] => John
    [key] => John
)
*/

list($name,$age) = each($ages);
echo $name . ": " . $age . "\n";
//You got: Barbara: 67. Because you did not use reset, and indexes 0 and 1 returned by each() are assigned to the list() construct.


$members = array("John", "Barbara");
$person = each($members);
print_r($person);
/*
Array
(
    [1] => John
    [value] => John
    [0] => 0
    [key] => 0
)
*/

9. The cast oprators
It changes the type of variable.

10. Floating point numbers (also known as "floats", "doubles", or "real numbers") can be specified using any of the following syntaxes:

LNUM [0-9]+
DNUM ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)
EXPONENT_DNUM [+-]?(({LNUM} | {DNUM}) [eE][+-]? {LNUM})

(int) $str is the same as (interger) $str
(bool) $str is the same as (boolean) $str

11. @ is the silence oprator, add @ before a function will silences error message.

12 eval() is similar to include, but instead of compiling and executing code that comes from a file, it accepts the code as a string. (Only do it when you can't do without) For example, in the solr,

$code = file_get_contents('http://localhost:8983/solr/select?q=iPod&wt=php');
//code is string, for exmaple,  'array("k1"=>"v1", "k2"=>"v2")...'
eval("\$result = " . $code . ";"); //this will assign array to $result
print_r($result);

13. static variables.
Static variables only run at the first time (and only the first time), so it can be used in a function, where you only trigger something when function is called at the first time.

$count = 5; // "outer" count = 5

function get_count()
{
    static $count = 0; // "inner" count = 0 only the first run
    return $count++; // "inner" count + 1
}

echo $count; // "outer" count is still 5 
++$count; // "outer" count is now 6 (but you never echoed it)

echo get_count(); // "inner" count is now + 1 = 1 (0 before the echo)
echo get_count(); // "inner" count is now + 1 = 2 (1 before the echo)
echo get_count(); // "inner" count is now + 1 = 3 (2 before the echo)

Leave a Reply

Your email address will not be published. Required fields are marked *