3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Example { public $length, $height, $width, $weight; public function __construct($length, $height, $width, $weight) { $this->length = $length; $this->height = $height; $this->width = $width; $this->weight = $weight; } public function get_price() { return 1.23; } } $sample_data = [ 42 => ['quantity' => 2, 'data' => new Example(42, 42, 42, 42)], 69 => ['quantity' => 1, 'data' => new Example(69, 69, 69, 69)], ]; $i = 0; $totalweight = 0; $totalprice = 0; foreach ( $sample_data as $item_id => $values ) { $_product = $values['data']; $q = 1; while ( $values['quantity'] >= $q ) { $products[ $i ]->length = $_product->length; $products[ $i ]->height = $_product->height; $products[ $i ]->width = $_product->width; $products[ $i ]->weight = $_product->weight; $totalweight += $_product->weight; $totalprice += $_product->get_price(); $q++; $i++; } } var_dump($products);
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
Fatal error: Uncaught Error: Attempt to assign property "length" on null in /in/1Imu2:31 Stack trace: #0 {main} thrown in /in/1Imu2 on line 31
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Warning: Creating default object from empty value in /in/1Imu2 on line 31 Warning: Creating default object from empty value in /in/1Imu2 on line 31 Warning: Creating default object from empty value in /in/1Imu2 on line 31 array(3) { [0]=> object(stdClass)#3 (4) { ["length"]=> int(42) ["height"]=> int(42) ["width"]=> int(42) ["weight"]=> int(42) } [1]=> object(stdClass)#4 (4) { ["length"]=> int(42) ["height"]=> int(42) ["width"]=> int(42) ["weight"]=> int(42) } [2]=> object(stdClass)#5 (4) { ["length"]=> int(69) ["height"]=> int(69) ["width"]=> int(69) ["weight"]=> int(69) } }

preferences:
115.92 ms | 409 KiB | 5 Q