Thursday, March 21, 2013

Good links for PHP


Here's some good links accumulated from the answers below/Found myself for understanding the OO architecture/Unique Features in PHP, of course, it is meant for people from other areas like java and .net to have an overview of PHP features.

OO

Using namespace
http://php.net/manual/en/language.namespaces.basics.php

Fallback for global namespace
http://www.php.net/manual/en/language.namespaces.fallback.php

Class(overidden class, Const, static class) using ::
http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php

Array

http://php.net/manual/en/language.types.array.php

An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments.
array(
    key  => value,
    key2 => value2,
    key3 => value3,
    ...
)


the key can either be an integer or a string. The value can be of any type.
Additionally the following key casts will occur:
  • Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
  • Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
  • Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.
  • Null will be cast to the empty string, i.e. the key null will actually be stored under "".
  • Arrays and objectcan not be used as keys. Doing so will result in a warning: Illegal offset type.
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.
Example #2 Type Casting and Overwriting example
<?php
$array 
= array(
    
1    => "a",
    
"1"  => "b",
    
1.5  => "c",
    
true => "d",
);
var_dump($array);?>
The above example will output:
array(1) {
  [1]=>
  string(1) "d"
}



The key is optional. If it is not specified, PHP will use the increment of the largest previously used integer key.
Example #4 Indexed arrays without key
<?php
$array 
= array("foo""bar""hallo""world");var_dump($array);?>
The above example will output:
array(4) {
  [0]=>
  string(3) "foo"
  [1]=>
  string(3) "bar"
  [2]=>
  string(5) "hallo"
  [3]=>
  string(5) "world"
}


It is possible to specify the key only for some elements and leave it out for others:
Example #5 Keys not on all elements
<?php
$array 
= array(
         
"a",
         
"b",
    
=> "c",
         
"d",
);
var_dump($array);?>
The above example will output:
array(4) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [6]=>
  string(1) "c"
  [7]=>
  string(1) "d"
}

Multidimentional array

Accessing array elements with square bracket syntax

Array elements can be accessed using the array[key] syntax.
Example #6 Accessing array elements
<?php
$array 
= array(
    
"foo" => "bar",
    
42    => 24,
    
"multi" => array(
         
"dimensional" => array(
             
"array" => "foo"
         
)
    )
);
var_dump($array["foo"]);var_dump($array[42]);var_dump($array["multi"]["dimensional"]["array"]);?>
The above example will output:
string(3) "bar"
int(24)
string(3) "foo"


Creating/modifying with square bracket syntax

An existing array can be modified by explicitly setting values in it.
This is done by assigning values to the array, specifying the key in brackets. The key can also be omitted, resulting in an empty pair of brackets ([]).
$arr[key] = value;
$arr[] = value;
// key may be an integer or string
// value may be any value of any type
If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array. This practice is however discouraged because if $arr already contains some value (e.g. string from request variable) then this value will stay in the place and [] may actually stand for string access operator. It is always better to initialize variable by a direct assignment.
To change a certain value, assign a new value to that element using its key. To remove a key/value pair, call the unset() function on it.
<?php
$arr 
= array(=> 112 => 2);$arr[] = 56;    // This is the same as $arr[13] = 56;
                // at this point of the script
$arr["x"] = 42// This adds a new element to
                // the array with key "x"
                
unset($arr[5]); // This removes the element from the arrayunset($arr);    // This deletes the whole array?>

Multidimentional array

Accessing array elements with square bracket syntax

Array elements can be accessed using the array[key] syntax.
Example #6 Accessing array elements
<?php
$array 
= array(
    
"foo" => "bar",
    
42    => 24,
    
"multi" => array(
         
"dimensional" => array(
             
"array" => "foo"
         
)
    )
);
var_dump($array["foo"]);var_dump($array[42]);var_dump($array["multi"]["dimensional"]["array"]);?>
The above example will output:
string(3) "bar"
int(24)
string(3) "foo"
Note:
Both square brackets and curly braces can be used interchangeably for accessing array elements (e.g. $array[42] and $array{42} will both do the same thing in the example above).

Useful functions

There are quite a few useful functions for working with arrays. See the array functions section.
Note:
The unset() function allows removing keys from an array. Be aware that the array will not be reindexed. If a true "remove and shift" behavior is desired, the array can be reindexed using the array_values() function.
<?php
$a 
= array(=> 'one'=> 'two'=> 'three');
unset(
$a[2]);/* will produce an array that would have been defined as
   $a = array(1 => 'one', 3 => 'three');
   and NOT
   $a = array(1 => 'one', 2 =>'three');
*/
$b array_values($a);// Now $b is array(0 => 'one', 1 =>'three')?>
The foreach control structure exists specifically for arrays. It provides an easy way to traverse an array.


Regarding modification of the array:
http://stackoverflow.com/questions/5179606/php-replace-array-value

Note
Array assignment always involves value copying. Use the reference operator to copy an array by reference.



Include and require

http://www.php.net/manual/en/function.include.php
require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas includeonly emits a warning (E_WARNING) which allows the script to continue.


foreach

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, 
foreach (array_expression as $value)
    statement
foreach (array_expression as $key => $value)
    statement
<?php
$arr 
= array(1234);
foreach (
$arr as &$value) {
    
$value $value 2;
}
// $arr is now array(2, 4, 6, 8)unset($value); // break the reference with the last element?>
Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().

You may have noticed that the following are functionally identical:
<?php
$arr 
= array("one""two""three");reset($arr);
while (list(, 
$value) = each($arr)) {
    echo 
"Value: $value<br />\n";
}

foreach (
$arr as $value) {
    echo 
"Value: $value<br />\n";
}
?>

No comments:

Post a Comment