Page 1 of 1
Referencing
Posted: Sun Feb 11, 2007 4:24 pm
by amk221
I've never used referencing until now, and I'm starting to think about performance etc...
If there is a constant WEBSITE_ROOT, and I wanted to print this out inside heredoc (which you can't do)
I would do this: $root = WEBSITE_ROOT;
Then inside the heredoc I can print the variable $root instead.
My question is, should I be doing this: $root =& WEBSITE_ROOT;
Also, does the ampersand go after the equals sign =& or next to this &WEBSITE_ROOT;
thanks
Re: Referencing
Posted: Sun Feb 11, 2007 4:28 pm
by alex.barylski
amk221 wrote:I've never used referencing until now, and I'm starting to think about performance etc...
If there is a constant WEBSITE_ROOT, and I wanted to print this out inside heredoc (which you can't do)
I would do this: $root = WEBSITE_ROOT;
Then inside the heredoc I can print the variable $root instead.
My question is, should I be doing this: $root =& WEBSITE_ROOT;
Also, does the ampersand go after the equals sign =& or next to this &WEBSITE_ROOT;
thanks
For starters, you wouldn't reference a constant, you only reference variables.
You reference a variable using the following syntax:
Posted: Mon Feb 12, 2007 2:58 am
by amk221
Ok thanks, but why wouldn't I? Because in that case, my only option is to make a copy of the variable?
Posted: Mon Feb 12, 2007 9:23 am
by Chris Corbyn
amk221 wrote:Ok thanks, but why wouldn't I? Because in that case, my only option is to make a copy of the variable?
What? Hockey just showed you how to copy it. You can't reference a constant because a constant never changes. If you want to reference a variable which holds the value of a constant then assign the value of the constant to the variable and reference the variable.
Code: Select all
define("FOO", "bar");
$foo = FOO;
$bar =& $foo;
$bar = "test";
echo $foo; //test
EDIT | The position of the ampersand doesn't matter. I prefer it on the operator when I'm assigning. But when you pass by-reference you place it on the variable itself.
Posted: Mon Feb 12, 2007 9:37 am
by RobertGonzalez
You don't need a reference in your case, as you are echoing constant data in the heredoc. You adding a pinch of overhead to the app in that you are taking a little more memory by assigning an assigned value into another variable. But to do what you want there is no other way around that. But you do not need references in what you are doing.
Posted: Mon Feb 12, 2007 9:47 am
by Chris Corbyn
Everah wrote:You adding a pinch of overhead to the app in that you are taking a little more memory by assigning an assigned value into another variable.
It's interesting, because that's not true

PHP is copy-on-write which means that by simply assigning the value of one variable to another you use no more memory bcause under the hood, the variable is just a pointer to the original value stored in memory. Once you change that value, it then copies it, but not just because you assigned it to another var
Code: Select all
$x = 1;
$y = $x;
//uses same memory (or there-abouts) as...
$x = 1;
Posted: Mon Feb 12, 2007 9:59 am
by RobertGonzalez
Sorry, I spoke incorrectly

Posted: Mon Feb 12, 2007 12:04 pm
by amk221
ahh cool thats clever stuff, so really - i don't need to concern myself with referencing because under the hood it's doing it already.