Reputation: 6848
We've coded two validation functions, one is like below (it gets all the fields at once):
function check_fields(&$arr,&$msg,$types,$lens,$captions,$requireds) {}
and the other function is something like below:
function is_valid($field=NULL,$type=0 ,$length=0,$required=true) {}
First function has a few code lines, and reduces code lines dramatically (about 30-35 lines or even more), on the other side the second function without reference increases the code lines (about 30-35 lines or even more).
We have to call the second function for every field we want to validate, but the first function (check_fields
) is vice versa.
I've read in an article long time ago that functions with reference parameters are bad from a performance point of view.
Now we don't know which function to use. Which one is better from a performance perspective?
Upvotes: 0
Views: 1842
Reputation: 1
You just remember this, when you called:
<?php
$a = 1 ;
echo "As initial, the real 'a' is..".$a."<br/>";
$b = &$a ; // it's meaning $b has the same value as $a, $b = $a AND SO $a = $b (must be)
$b += 5;
echo "b is equal to: ".$b." and a equal to: ".$a."<br/>";
echo " Now we back as initial, when a=1... (see the source code) <br/>";
$a = 1 ;
echo "After we back : b is equal to: ".$b." and a equal to: ".$a."<br/>";
echo "Wait, why b must follow a? ... <br/> ..what about if we change b alone? (see the source code)<br/>";
$b = 23;
echo "After we change b alone, b equal to: ".$b." and a equal to: ".$a."<br/>";
echo " WHAT ?? a ALSO CHANGED ?? a and b STICK TOGETHER?!! </br>" ;
echo "to 'clear this, we use 'unset' function on a (see the source code)<br/> ";
unset($a);
$b = 66;
$a = 1;
echo "Now, after unset,b is equal to: ".$b." and a equal to: ".$a."<br/>";
?>
And the Output will be:
As initial, the real 'a' is..1
After the Reference operator...(see the source code)
b is equal to: 11 and a equal to: 11
Now we back as initial... (see the source code)
After we back : b is equal to: 1 and a equal to: 1
Wait, why b must follow a? ...
..what about if we change b alone? (see the source code)
After we change b alone, b equal to: 23 and a equal to: 23
WHAT ?? a ALSO CHANGED ?? a and b STICK TOGETHER?!!
to 'clear this, we use 'unset' function on a (see the source code)
Now, after unset,b is equal to: 66 and a equal to: 1
Review:
Why did $a
change? That's because &
(the ampersand operator) made &
as a reference variable, hence it stored in a 'temporary memory'. When you intitialized $b= &$a
, see my comment $b = $a
and also $a = $b
, that means whenever you modified $b
, $a
was also modified, and vice versa. They are chained! That's the simple meaning of the reference operator.
Maybe at the beginning it feels confusing, but once you become an expert with this, you can handle this operator to make a "switching" function like this:
<?php
function bulbswitch(&$switch)
{
$switch = !$switch;
}
function lightbulb($a)
{
if ($a == true) { echo 'a is On <br/>';}
else { echo 'a is Off <br/>';}
}
$a = true ;
echo 'At the begining, a is On, then.... <br/>';
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
bulbswitch($a);
lightbulb($a);
?>
The output will be:
At the begining, a is On, then....
a is Off
a is On
a is Off
a is On
a is Off
a is On
Upvotes: 0
Reputation: 6470
use the solution that is simpler to use and easier to maintain. You are talking about micro optimization, which is pretty much useless.
Upvotes: 1