+ 3

Practical use of Call by Reference in PHP

Is there any practical use of call by reference variable in PHP and if then give an example where it is compulsory to use call by reference.

17th May 2020, 1:13 PM
Shailendra
Shailendra - avatar
5 Respuestas
+ 1
This is a detailed article. Hope it helps your problem addressed out. https://www.elated.com/php-references/
17th May 2020, 2:08 PM
James Clark I. Vinarao
James Clark I. Vinarao - avatar
+ 1
Ore Adeleye thanks for the example No matter how many articles I read about any topic related to programming but having just an example is enough for me to understand it Again thank you
18th May 2020, 6:04 AM
Shailendra
Shailendra - avatar
0
if u really want to change value of variable outside the function
17th May 2020, 1:35 PM
durian
durian - avatar
0
It is used to change variable values without creating new variables. For example suppose you want to capitalize all strings in an array. Inefficient pass-by-value method <?php $names = array("dylan", "carlos", "mitch", "fiona"); function capitalize($str){ return ucwords($str); } for($i=0; $i < count($names); $i++){ $names[$i] = capitalize($names[$i]); } var_dump($names); ?> Efficient pass-by-reference approach <?php $names = array("dylan", "carlos", "mitch", "fiona"); function capitalize(&$str){ $str = ucwords($str); } for($i=0; $i < count($names); $i++){ capitalize($names[$i]); } var_dump($names); ?>
17th May 2020, 4:23 PM
Ore
Ore - avatar
0
<?php $x=3; function square(&$x) { $x = $x*$x; } echo $x."\n"; square($x); echo $x."\n"; ?>
19th May 2020, 3:17 AM
Ahmed Muhammed
Ahmed Muhammed - avatar