PHP references
Introduction to PHP References
PHP เคฎें references เคा เค เคนเคฎ role เคนोเคคा เคนै। References เคो เค เคฒเค เค เคฒเค purposes เคे เคฒिเค program เคฎें เคฏूเค़ เคเคฐ เคธเคเคคे เคนै। เคฏे pointers เคी เคคเคฐเคน เคจเคนीं เคนोเคคे เคนै। เคเคฆाเคนเคฐเคฃ เคे เคฒिเค เคจिเคे เคฆिเค เคนुเค program เคो เคฆेเคिเคฏे।
<?php
// Creating reference
$a =& $b;
$a = "Indian";
// It will print Indian
echo $a;
// It will also print Indian
echo $b;
?>เคเคชเคฐ เคฆिเค เคนुเค program เคฎें เคเคฌ เคฆोเคจों variables เคी value print เคी เคाเคคी เคนै เคคो เคฆोเคจों same value print เคเคฐเคคे เคนै เค्เคฏोंเคि เคฆोเคจों เคเค เคนी value เคो refer เคเคฐ เคฐเคนे เคนै।
Passing Undeclared Variable
เคฏเคฆि เคเคช เคिเคธी เคเคธे variable เคो assign, pass เคเคฐ return เคเคฐเคจे เคी เคोเคถिเคถ เคเคฐเคคे เคนै เคो เค्เคฐिเคเค เคจเคนीं เคिเคฏा เคเคฏा เคนै เคคो เคตเคน variable automatically create เคนो เคाเคคा เคนै। เคเคฆाเคนเคฐเคฃ เคे เคฒिเค เคจिเคे เคฆिเค เคนुเค program เคो เคฆेเคिเคฏे।
<?php
function myFunction(&$a)
{
$a = 2;
echo $a;
}
// Passing a without creating it
myFunction($a);
?>เคเคชเคฐ เคฆिเค เคเค เคเคฆाเคนเคฐเคฃ เคฎें myFunction()เคो call เคเคฐเคคे เคธเคฎเคฏ เคเค เคเคธा variable pass เคिเคฏा เคा เคฐเคนा เคนै เคिเคธเคो เค्เคฐिเคเค เคนी เคจเคนीं เคिเคฏा เคเคฏा เคนै। เคฒेเคिเคจ เค्เคฏोंเคि function เคฎें เคเคธ variable เคा reference เคชाเคธ เคिเคฏा เคा เคฐเคนा เคนै เคเคธเคฒिเค PHP เคเคธ variable เคो automatically เค्เคฐिเคเค เคเคฐ เคฆेเคคी เคนै।
If in For Loop
เคฏเคฆि เคเคช for each เคฒूเคช เคฎें เคญी referenced variables เคฏूเค़ เคเคฐเคคे เคนै เคคो เคฆोเคจों variables เคी value update เคนोเคคी เคนै। เคเคฆाเคนเคฐเคฃ เคे เคฒिเค เคจिเคे เคฆिเคฏा เคนुเค program เคฆेเคिเคฏे।
<?php
$arr = array(1,2,3,4,5);
// Creating reference
$number =& $value;
foreach($arr as $value);
{
echo $value;
}
echo $number;
echo $value;
?>เคเคชเคฐ เคฆिเค เคเคฆाเคนเคฐเคฃ เคฎें foreach เคฒूเคช เคฎें $value variable เคो เคฏूเค़ เคिเคฏा เคเคฏा เคนै เคฒेเคिเคจ เคเคธเคे เคธाเคฅ เคธाเคฅ $number variable เคฎें เคญी array เคी value update เคนोเคคी เคนै। เคเคฐ เคเคฌ เคฆोเคจों variables เคो เคช्เคฐिंเค เคिเคฏा เคाเคคा เคนै เคคो เคฆोเคจों เคนी iteration เคी last value 5 print เคเคฐเคคे เคนै।
| Next class 13 |
Comments
Post a Comment