Page 1 of 1
Complex operators
Posted: Tue Sep 09, 2008 7:22 am
by Darkzaelus
Three quick questions:
Always wondered how the & is used with a parameter in a function, eg:
Code: Select all
function kind_of (&$object_or_class, $class){
...
}
Also, although i can't find an example at the moment, what does the ~ sign do?
Lastly, can the ^ or | operators be used in the same way as the & operator in function declarations?
Cheers!
Darkzaelus
Re: Complex operators
Posted: Tue Sep 09, 2008 7:37 am
by marcth
The & character allows you to pass a variable by reference. Read this:
References Explained.
you can learn how to use the ~ operator by reading this:
Operators.
Re: Complex operators
Posted: Tue Sep 09, 2008 7:38 am
by Darkzaelus
Thanks a lot for the quick reply!
Re: Complex operators
Posted: Tue Sep 09, 2008 12:15 pm
by Darkzaelus
So in short, it seems the with the & operator in a function, the parameter needs to be a declared function.
Also, I can't seem to find where the ~ symbol is being used. Correct me if i'm being stupid.
Cheers, Darkzaelus
Re: Complex operators
Posted: Tue Sep 09, 2008 7:02 pm
by Weirdan
~ is bitwise negation (
http://us2.php.net/manual/en/language.o ... itwise.php).
So in short, it seems the with the & operator in a function, the parameter needs to be a declared function.
& in function declaration is not an operator, it's argument modifier which tells PHP that modifications made by the function to this argument must be reflected outside the function:
Code: Select all
function changeValue(&$val) {
$val = 345;
}
$aValue = 123;
changeValue($aValue);
var_dump($aValue); // prints 'int(345)'
Re: Complex operators
Posted: Wed Sep 10, 2008 10:01 am
by Darkzaelus
Lovely!
So another in short, this uses an existing variable instead of creating a local one within the function scope, so that when it is changed in the function, global changes are made.
This will make my coding so much easier.
Cheers, Darkzaelus
Re: Complex operators
Posted: Wed Sep 10, 2008 10:10 am
by marcth
I would avoid using reference variables, if you can. There are times when they are useful, but more often than none they cause confusion and make the code harder to read.
Instead of:
Code: Select all
function changeValue(&$val) {
$val = 345;
}
$aValue = 123;
changeValue($aValue);
var_dump($aValue); // prints 'int(345)'
Why not do it the traditional way:
Code: Select all
function changeValue($value) {
$value = 345;
return $value;
}
$aValue = 123;
$aValue = changeValue($aValue);
var_dump($aValue); // prints 'int(345)'
See, in the second example, it is quite apparent that the value of $aValue is being changed--in the first example you have no idea. Look what happens If you were to rename the function name to something a little more obscure:
Code: Select all
//Which is clearer?
// This?
$aValue = 345;
zzz($aValue);
// Or this?
$aValue = 345;
$aValue = zzz($aValue);
Re: Complex operators
Posted: Wed Sep 10, 2008 10:24 am
by Darkzaelus
Exactly. I've been using returns all the time, so there doesn't seem to be any point in changing, unless I was specifically writing codethe client shouldn't understand
Cheers, Darkzaelus