Jump to content

References with functions general question


j.smith1981

Recommended Posts

Hi there just wanted to ask you a general question regarding referenced variables and functions in PHP.

 

I have this code:

function theRefFunction(&$var){
$var = $var +1;
return $var;
}

$a = 50;

echo $thisvalue = theRefFunction($a);

 

Just for learning purposes, as allot of times beginning PHP seriously developers who want to improve on memory consumption in PHP find this tricky, is there any point in using an example like this?

 

I.e. would I really need the &$var as the parameter for the function called 'theRefFunction()'?

 

Just wondered thats all, if not what would be a better way for really using it?

 

It's just so I can go onto maybe doing a loop in it and setting it as a real example like working out tax and stuff like that, just for learning purposes, won't yet be using it.

 

Just wanted to build up as I said earlier a library of things I have done and make maybe my own tutorial site.

 

I look forward to any replies,

Jeremy.

Link to comment
Share on other sites

There's no point passing back a referenced variable, because any changes you made locally were done to the original -- the function's local variable is only a reference to the original variable.

 

Something as small as the example you provided has no worth while benefit really, but consider and example where you're working with a large set of data that you need to pass between several functions; passing the data normally would create a new copy of the data for every function you pass it to, then you'd also have to return it and update the original. That can be a large over head given the size of some data (don't know if you've ever had the pleasure of working with BLOBs and CLOBs?), however if you'd referenced it when you passed it instead you'd only have the array in memory once (and several references pointing to it).

 

Of course there's other uses for references not relating to memory, but so long as you understand the concept of them, the benefits will become clear one day when you find yourself in a situation they'd actually be useful for. Just don't include them for the sake of using them..

Link to comment
Share on other sites

If you change;

echo $thisvalue = theRefFunction($a);

 

To;

$thisvalue = theRefFunction($a);
echo $thisvalue;
echo $a;

 

Echo-ing an assignment statement (what you did) doesn't make any sense.

 

As far as preserving memory goes, your example doesn't really make sense. The  &$var will prevent $a being copied when it goes into the function (thus saving memory). But it is then copied anyway when it is returned, negating the memory you saved initially.

 

A more memory efficient function would be;

function theRefFunction(&$var){
$var = $var +1;
}
$a = 50;
theRefFunction($a);

// $a is now 51

 

That being said, the memory benefit will be minimal. You'd need to use the function millions of times for it to make a noticeable difference. Passing by reference to save memory has bigger implications with more complicated data. PHP automatically passes objects by reference for this reason IIRC.

Link to comment
Share on other sites

No thats wonderful though,

 

I did actually notice a few things when I actually submitted it, but yes I will take that on board.

 

Just thought I would nail references though by doing what I have done so far though beyond that example, all good for theory should I need to use them.

 

Want to try and nail nested functions using references at some point too, just to make them allot more advanced like entering say a loan calculator etc, would be quite interesting to get one of those working from a users input.

 

Seen that kind of example in a book, but was way too over complex to understand for me at the time, so thought I would go way back to the basics, get those nailed and then move on (even if it doesnt make any sense, I know to avoid doing the above lol).

 

Thanks for all your help,

Jez.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.