Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to bind variables? PHP 5

Tags:

php

Using PHP 5 I would like to know if it is possible for a variable to dynamically reference the value of multiple variables?

For example

    <?php
    $var = "hello";
    $var2 = " earth";
    $var3 = $var.$var2 ;
    echo $var3; // hello earth

Now if I change either $var or $var2 I would like $var3 to be updated too.

    $var2 =" world";  
    echo $var3; 

This still prints hello earth, but I would like to print "hello world" now :(

Is there any way to achieve this?

like image 671
dany Avatar asked Feb 27 '23 21:02

dany


1 Answers

No, there is no way to do this in PHP with simple variables. If you wanted to do something like this in PHP, what you'd probably do would be to create a class with member variables for var1 and var2, and then have a method that would give you a calculated value for var3.

like image 164
Amber Avatar answered Mar 06 '23 20:03

Amber