Skip to main content

All Questions

Tagged with
8 votes
3 answers
433 views

Duplicate array keys (Notice: member variable "a" returned from __sleep() multiple times)

The title may seem a bit silly but I'm totally serious with this. Today at work I came across a weird PHP behaviour which I could not explain. Luckily this behaviour is fixed in PHP 7.4, so it seems ...
Benjamin Paap's user avatar
1 vote
2 answers
236 views

Micro optimization on array keys

I have an array of which I am using some items to construct more arrays, a rough example follows. $rows = [ [1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f'], [4, 5, 6, 'g', 'h', 'i'], ];...
php_nub_qq's user avatar
  • 15.7k
5 votes
0 answers
288 views

PHP, Compile foreach and opline

I'm trying to figure out how a foreach statement is interpreted by PHP. That led me to use gdb while executing a dummy foreach script. I end up in zend_compile.c, in the zend_compile_foreach() ...
JesusTheHun's user avatar
  • 1,217
3 votes
3 answers
128 views

Which one of the following PHP array structure would be using less memory?

Which one of the following would be using less memory? $myArray = array(); $myArray[1] = array(1,2,3,4,5,6,7,8,9,10); $myArray[2] = array(1,2,3,4,5,6,7,8,9,10); $myArray[3] = array(1,2,3,4,5,6,7,8,9,...
Dilip's user avatar
  • 1,152
6 votes
1 answer
234 views

Does PHP optimize function arguments of array type, not explicitly passed by reference, when they are not modified?

Would the PHP engine optimize the second example to pass the $arr by reference? function test1(array &$arr) { $arr[] = 123; echo $arr[0]; } function test2(array $arr) { echo $arr[0];...
marsgpl's user avatar
  • 672
22 votes
4 answers
4k views

How does PHP memory actually work

I've always heard and searched for new php 'good writing practice', for example: It's better (for performance) to check if array key exists than search in array, but also it seems better for memory ...
George G's user avatar
  • 7,639
2 votes
3 answers
805 views

What algorithm does array_sum use in PHP?

what algorithm does array sum uses to make it much faster than some looping through? Is it prefix sum / suffix sum or something else?
Ajeet Singh's user avatar
5 votes
0 answers
837 views

Why does accessing a null value in PHP as an array not generate a warning? [duplicate]

I was hit by a bug where a returned DB result was expected to be an array but due to a glitch in the SQL query in one instance the DB wrapper correctly returned null. Unfortunately the code did not ...
Nils's user avatar
  • 189
6 votes
6 answers
4k views

Increasing array elements while in foreach loop in php? [duplicate]

Consider the code below: <?php $arr = array(); $arr['b'] = 'book'; foreach($arr as $key=>$val) { print "key=>$key\n"; if(!isset($arr['a'])) $arr['a'] = 'apple'; } ?> It is ...
Tarun Chabarwal's user avatar
1 vote
2 answers
939 views

PHP uksort function using global variable fails after PHP upgrade to 5.3.3

I have a user defined sort function which uses a 'global' declaration in order to refer to a multi-dimensional array when deciding on the correct sort order. It used to work fine under PHP 5.1.6 but ...
Claud's user avatar
  • 999
14 votes
2 answers
3k views

Why does foreach copy the array when we did not modify it in the loop? [duplicate]

In a blog post "PHP Internals: When does foreach copy", NikiC stated that in a code like this: Snippet 1 $array = range(0, 100000); foreach ($array as $key => $value) { xdebug_debug_zval('...
Pacerier's user avatar
  • 88.6k
2 votes
2 answers
246 views

What's the behavior of count when array has more than 2147483647 elements?

On a 32-bit system, an array can have as much as 4294967295 elements (as per Artefacto's post on another thread). However, count returns the number of elements as an int, and on a 32-bit system, an ...
Pacerier's user avatar
  • 88.6k
2 votes
1 answer
254 views

Why does a PHP array get modified when it's element is reference-assigned?

When ref-assigning an array's element, the contents of the array are modified: $arr = array(100, 200); var_dump($arr); /* shows: array(2) { [0]=> int(100) // ← ← ← int(100) [1]=> int(...
Pacerier's user avatar
  • 88.6k
3 votes
1 answer
284 views

Make last array parameter optional in php class method (C)

I am creating a PHP extension in C to access the SPI interface. So far I have gotten pretty much everything working: php_spi on Github However, I cannot seem to make the $options parameter in the ...
frak's user avatar
  • 878
1 vote
2 answers
244 views

PHP array syntax/operator?

When writing the syntax for an associative array in PHP we do the following $a = array('foo' => 'bar'); I am curious of the relationship of the => syntax, or possibly operator. Does this ...
grep's user avatar
  • 4,008

15 30 50 per page