+ 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.
Break and Continue May 21
Do you know that operators 'break' and 'continue' have one optional int parameter?
I didn't.
The trick is: when you have multiple nested cycles, you may want to break or to continue not only the current loop iteration, but also parent loop iteration. This optional parameter gives a clue of how many loop layers to break (or to continue).
Looks like following:
Something like that. I mean - if you still can't understand the sense of parameter - try to execute it and change 'continue 2' to 'continue';
Yes, it's senseless code.
Logical tricks in PHP May 21
Do you know, how 'and' and 'or' operators works?
Probably yes. (And probably you know, what is the difference between 'and' and '&&')
What's tricky here?
The trick is: 'and' returns true when both operands are true. If the first operand is false, operator returns false immediately.
So, we can use it instead of 'if':
it works. Really.
Absolutely the same with 'or': it returns true immediately if the first operand is true (so second will not be calculated)
I bet everyone seen the following code:
When I first seen this I was thinking - what an interesting language, this looks like simple English...
Now I know what's the trick. And you too.
Variables naming in PHP May 21
Surely you know, that variables in PHP always starts with $ and then must be a character [a-Z] or underscore.
That's the rule.
How official man says:
Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
(http://www.php.net/manual/en/language.variables.php)
BUT!
There is one more tricky way to name the variables - using variable variables. To those, who don't know what is it: read about it .
Now, the trick:
Outputs:
abcd
Sure.
But variable variables gives us one more cute opportunity: access by name, anyway what is the name
Here is how it goes:
Outputs:
100
So, the original ruse is just not complete - there is one more way to name variables. And this way has no limitations of first and any characters.
Is this useful? I don't know. Just interesting.