tags:

views:

52

answers:

4

In php 5, all variable and objects are passed by reference, but i can't get my codes work

My codes is:

$arrayA = array();

$array = $arrayA;
...
if(!in_array(thedata, $array)
    $array[] = thedata;
var_dump($arrayA);

The result is empty, am i missing something simple?

A: 
$array =& $arrayA;
Moak
It is $array =
Ivo Sabev
+1  A: 

In PHP5 all objects are passed by reference (more or less), not all variables.

$array =& $arrayA;
Simon
+1  A: 

Only objects are passed by reference. If you want to make a reference to simple types, you have to use =& for assignment:

php > $var1 = 'xxxxx';
php > $var2 =& $var1;
php > $var1 = 'yyyyy';
php > echo $var2;
yyyyy
WishCow
+3  A: 
<?php
    $arrayA = array();
    $arrayB =& $arrayA;
    $arrayB = array(1,2,3);
    var_dump($arrayA);

Read more here:

http://php.net/manual/en/language.types.array.php (Search for Reference)

http://www.php.net/manual/en/language.references.php

Ivo Sabev