Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Constant References

Tags:

php

constants

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?

like image 786
lolraccoon Avatar asked Jan 20 '23 16:01

lolraccoon


2 Answers

No, there is no equivalent to C++'s const qualifier in PHP.

like image 119
Jacob Relkin Avatar answered Jan 31 '23 19:01

Jacob Relkin


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
?>
like image 37
Salman A Avatar answered Jan 31 '23 19:01

Salman A