Referencing

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
amk221
Forum Newbie
Posts: 6
Joined: Wed Feb 07, 2007 4:35 pm

Referencing

Post 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
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Referencing

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

Code: Select all

$root = &$somevar;
amk221
Forum Newbie
Posts: 6
Joined: Wed Feb 07, 2007 4:35 pm

Post by amk221 »

Ok thanks, but why wouldn't I? Because in that case, my only option is to make a copy of the variable?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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;
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Sorry, I spoke incorrectly :oops:
amk221
Forum Newbie
Posts: 6
Joined: Wed Feb 07, 2007 4:35 pm

Post 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.
Post Reply