Complex operators

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Complex operators

Post 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
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: Complex operators

Post 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.
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: Complex operators

Post by Darkzaelus »

Thanks a lot for the quick reply!
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: Complex operators

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Complex operators

Post 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)'
 
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: Complex operators

Post 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
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: Complex operators

Post 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);
 
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: Complex operators

Post 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 :P

Cheers, Darkzaelus
Post Reply