Well, this one is hard to find on Google, so I’m putting it in WhyPad to make it easy to find…at least for me, and I guess for you too since you here. Welcome! You may see PHP code snippets (PHP 5+ only) that have an ampersand, ‘&’, preceding a variable like &$my_variable. So, what does it do? It sets up a reference to the original variable instead of copying it’s value. The following snippet demonstrates:
$original = "foo"; $ref = &$original; echo $ref; \\Prints "foo"....Note that you don't continue to use the '&' after the var is initialized now change $original $original = "bar"; echo $ref; \\Now prints: "bar"
$ref would be unaffected by changes to $original if it had been set using the normal: $ref = $original;
Next to figure out what that @ is doing in PHP code… 😉
[UPDATE] David sheds light on the “@” below…Thanks David!
Cheers,
Byron
Hah, thanks for reminding me about what the “&” does. Hadn’t used it in such a long time I’d forgotten.
As for the “@”, it’s used for operators and functions to tell PHP not report any errors if they should occur. A good example is if you want to delete a file – should that file not exist, PHP would return the error message saying that.
David,
Thanks for dropping by! And thanks for the note on “@”. I keep forgetting these little details since I don’t use them very often.
Byron
hi im using php 5.x and im getting error whenever i try to use &$ref..pls help thanks
Hi Jary,
Here are some alternative methods to try:
$new_var =& $old_var;
or if passing through a function
function my_fun(& $old_var){
…
}
If those don’t work, post your code and I’ll take a look.
Regards,
Byron
I recently installed PHP5.2.8 using EasyPHP 3.0 on an XP machine and have been trying to install PEAR in order to access its templates but I keep getting a warning that the “include_path defined in the currently used php.ini does not contain the PEAR PHP directory you just specified”.
However, even though I have changed the include_path in php.ini (which is located at C:\Program Files\EasyPHP 3.0\php) to “c:\Program Files\EasyPHP 3.0\php\pear”, I still keep getting the warning that the “Current include path is .;C:\php5\pear”.
I have looked all over and have not located the php.ini file on the C: drive with the path .;C:\php5\pear. I cannot imagine where the include path .;C:\php5\pear is being read from during installation.
I would appreciate it if someone could tell me where this path is being read from and how I can change it.
I need to access the PEAR templates to build my application.
Nice! You’re right, it was hard to find on Google, excluding this post.
ty, cool read hard to find!
Help I’ve got a completely random PHP problem!
This is good to know. Google should have a way to find literal code like operators.
Shouldn’t the line:
&$ref = $original;
have the ampersand on the second variable?
$ref = &$original;
Al,
Yep…you’re right…fixed. Thanks!
BB
Hey, thanks for the great tip. I’ve always wondered what the ampersand did. That will definitely be useful! Cheers
@Al http://www.codefetch.com/examples.html
This is a site that lets you query source code of programming books with special characters like ‘&’.
Thanks! Your site was the first that came up in my search. This behavior is like in java when a variable simply contains a pointer to another variable such as
String s = “hello”;
String t = s;
t is simply pointing to the memory location of s and when you change the value of s, t points to s all the same and will return the same new value.
exactly what i want thanks a lot