Building high-load website? March 12
Learn how to make it fast.
Yahoo
Highscalability (video inside)
+ for PHP arrays May 28
$a + $b is the same as $b + $a?
No, if $a and $b are arrays.
what does '+' for array arguments? Let's ask man:
The + operator appends the right handed array to the left handed, whereas duplicated keys are NOT overwritten.
Must be understood. If you know how array keys are set by default:
when you are writing something like
If you do not specify a key for a given value, then the maximum of the integer indices is taken, and the new key will be that maximum value + 1. If you specify a key that already has a value assigned to it, that value will be overwritten.
If no integer values are in keys - it sets zero.
This means that you must be very sure about your keys when you use '+' for arrays. Because '+' works basing on keys, not the values.
And how it works with '-'?
Nohow. Sorry, guys. '-' is not defined for PHP arrays.
A little bonus:
Comparison is defined for PHP arrays. Here is how it works:
array vs. array
Array with fewer members is smaller, if key from operand 1 is not found in operand 2 then arrays are uncomparable, otherwise - compare value by valuearray vs. anything
array is always greater
A little note:
'==' and '===' works differently.
yes, both or then checks if arrays have same amount of elements, same keys, and values must be ==/=== (correspondingly) for sure. But '===' also checks the sequence of elements. So
$b = array(1=>1,2=>2,3=>3);
$c = $a==$b; //true
$c = $a===$b; //false
//
$a = array(1=>1,3=>3,2=>2);
$b = array(1=>1,2=>2,3=>3);
$c = $a==$b; //true
$c = $a===$b; //false
//
$a = array(1=>1,2=>2,3=>3);
$b = array(1=>1,2=>2,3=>3);
$c = $a==$b; //true
$c = $a===$b; //true
Magic list() May 22
list() - is a very interesting PHP construct.
Let's read the man:
list — Assign variables as if they were an array
Description
void list ( mixed $varname, mixed $... )Like array(), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation.
So, the usage may be as following:
Outputs:
1-2-3
sure.
And now a little trick. Look:
Outputs:
2-3-4
OK, where should I use it?
First idea - ugly functions like imagettfbbox()
Take a look:
Return Values
imagettfbbox() returns an array with 8 elements representing four points making the bounding box of the text:
0 lower left corner, X position
1 lower left corner, Y position
2 lower right corner, X position
3 lower right corner, Y position
4 upper right corner, X position
5 upper right corner, Y position
6 upper left corner, X position
7 upper left corner, Y position
So, you can make code better using list here. For example:
Generally, it allows to extract and rename few needed variables from an array you don't need.
Here is something to think about:
This outputs:
Array ( [0] => 2 [1] => 1 ) Array-3-4
Take a look at the array $b elements: they are ordered vice versa
and if we will write
instead of corresponding line, we will get
Array ( [1] => 2 [0] => 1 ) Array-3-4
Seems like arguments are stored in opposite order somewhere...
Functions with unknown amount of variables May 22
This feature is very powerful, but not all programmers use it.
OK, so, the trick: we can declare function with some amount of parameters and call it with more parameters then declared. (But not less - this will cause an error).
How to manage this stuff in function?
firstly, let's review three main functions:
func_num_args()
int func_num_args ( void )
Gets the number of arguments passed to the function.
This function may be used in conjunction with func_get_arg() and func_get_args() to allow user-defined functions to accept variable-length argument lists.
func_get_arg()
mixed func_get_arg ( int $arg_num )
Gets the specified argument from a user-defined function's argument list.
func_get_args()
array func_get_args ( void )
Gets an array of the function's argument list.
OK, very good. Seems like understood.
Where it may be useful?
For specific functions which may be performed on any amount of similar data, like, for example, minimum or maximum.
Function will look like following:
$args = func_num_args();
if ($args == 0){
echo 'function average() must recieve at least one argument!';
return NULL;
}
else{
$params = func_get_args();
return array_sum($params)/count($params);
}
}
and then use it like
etc.
I saw several time when someone writes something like
foreach($arg_arr as $param){
//something here
}
}
and then calles the function like
and I think this may be made more nice, tricky and hacky when you know about this things with functions agruments.