I'm a intermediate C++ programmer and know that you can pass a constant reference as a parameter in order to prevent editing to the actual variable. I was wondering if I could do this in PHP?
No, there is no equivalent to C++'s const
qualifier in PHP.
Is this what you're talking about:
<?php
$a = 10;
function foo($p_a) {
// passing by value is the default
$p_a++;
}
foo($a);
echo $a; // prints 10
$a = 10;
function bar(&$p_a) {
//-------^ passing by reference
$p_a++;
}
bar($a);
echo $a; // prints 11
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With