Is it possible to implement pointers (links) to data in php?

<?php session_start(); function foo(&$boo){ $_SESSION['boo']=$boo; } $boo=1; foo($boo); $boo=2; var_dump($boo); // int(2) var_dump($_SESSION['boo']); // int(1) ?> 

In this case, the value is written to the session, not the pointer.

  • There are no pointers for general use in php, there are analogues: <? php $ boo = 1; $ _SESSION ['boo'] = '$ boo'; $ boo = 2; echo 'boo =',% boo; echo '$ _SESSION [boo] =', "$ {$ _ SESSION [boo]}"; ?> - mega

2 answers 2

Were before.

 <?php $a = 1; $b = &$a; $a = 2; echo $b; //2 ?> 

Already deprecated like; But according to your code:

 function foo(&$boo){ $_SESSION['boo']=$boo; } 

In this case, the value is written to the session, not the pointer.

WAT?

 $_SESSION['boo']=&$boo; 

Is not it so? And the answer will be:

int 2

int 2

  • As far as I remember, in php.ini there is an option that controls the permission to use the & operator, so the solution may be correct, but on someone else’s hosting it will cause problems. - mega
  • @stck I didn’t take the dumps out of my head, but previously ran the code on my typewriter :) - zenith
  • @stck sorry did not look at your option. Need to try. - zenith
  • one
    > Like this. There is a great function ini_set (). allow_call_time_pass_reference PHP_INI_SYSTEM | PHP_INI_PERDIR this means that the allow_call_time_pass_reference can be supplied only from php.ini or from .htaccess, which is regulated by the host. So no, ini_set does not help. In addition:> Since PHP 5.4.0, passing a variable by reference has become impossible, so using this technique will result in a fatal error. - mega
  • 2
    @stck, and with ini_set there are no problems, it just allows you to set not all php flags . The mark PHP_INI_USER means the availability of a flag from ini_set . For allow_call_time_pass_reference this option is not available. - mega

So you pass the value and not the pointer, so that the pointer would be necessary to do so.

 session_start(); function foo(&$boo){ $_SESSION['boo']=&$boo; } $boo=1; foo($boo); $boo=2; var_dump($boo); // int(2) var_dump($_SESSION['boo']); // int(1) 

Learn more about pointers with usage examples.