user34790
user34790

Reputation: 2036

Print the length of an array passed by reference in php

How can I print the length of an array passed by reference in php. The variable that I will get is a reference variable. So how can I accomplish it?

Upvotes: 0

Views: 68

Answers (2)

user1477388
user1477388

Reputation: 21430

As far as I know, it works the same:

$arr = array('hi');
$arr2 = &$arr;

var_dump(count($arr2));

Upvotes: 0

Maxim Krizhanovsky
Maxim Krizhanovsky

Reputation: 26699

The same way as printing the length of an array passed by value:

function print_count(&$array){
    echo count($array);
}

References in PHP are a means to access the same variable content by different names. They are not like C pointers; for instance, you cannot perform pointer arithmetic using them, they are not actual memory addresses, and so on. Instead, they are symbol table aliases. Note that in PHP, variable name and variable content are different, so the same content can have different names.

Upvotes: 1

Related Questions