Referencing
Moderator: General Moderators
Referencing
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
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
For starters, you wouldn't reference a constant, you only reference variables.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
You reference a variable using the following syntax:
Code: Select all
$root = &$somevar;- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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.amk221 wrote:Ok thanks, but why wouldn't I? Because in that case, my only option is to make a copy of the variable?
Code: Select all
define("FOO", "bar");
$foo = FOO;
$bar =& $foo;
$bar = "test";
echo $foo; //test- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
It's interesting, because that's not trueEverah 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.
Code: Select all
$x = 1;
$y = $x;
//uses same memory (or there-abouts) as...
$x = 1;- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA