3v4l.org

run code in 300+ PHP versions simultaneously
<?php function batchDelete(): \Generator { // $entityManager = $this->getEntityManager($class); $queryResults = range(0, 102); $i = 0; $removedObjects = []; foreach ($queryResults as $pos => $object) { // $entityManager->remove($object[0]); $removedObjects[] = $object; if (0 === (++$i % 20)) { yield from $removedObjects; $removedObjects = []; // $entityManager->flush(); // $entityManager->clear(); } } yield from $removedObjects; // $entityManager->flush(); // $entityManager->clear(); } function callerFunctionBatchDelete() { foreach (batchDelete() as $deletedObject) { // Fire any event you may need regarding `$deletedObject`. echo 'Some operation with object "'.$deletedObject."\".\n"; } } callerFunctionBatchDelete();
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.13
Some operation with object "0". Some operation with object "1". Some operation with object "2". Some operation with object "3". Some operation with object "4". Some operation with object "5". Some operation with object "6". Some operation with object "7". Some operation with object "8". Some operation with object "9". Some operation with object "10". Some operation with object "11". Some operation with object "12". Some operation with object "13". Some operation with object "14". Some operation with object "15". Some operation with object "16". Some operation with object "17". Some operation with object "18". Some operation with object "19". Some operation with object "20". Some operation with object "21". Some operation with object "22". Some operation with object "23". Some operation with object "24". Some operation with object "25". Some operation with object "26". Some operation with object "27". Some operation with object "28". Some operation with object "29". Some operation with object "30". Some operation with object "31". Some operation with object "32". Some operation with object "33". Some operation with object "34". Some operation with object "35". Some operation with object "36". Some operation with object "37". Some operation with object "38". Some operation with object "39". Some operation with object "40". Some operation with object "41". Some operation with object "42". Some operation with object "43". Some operation with object "44". Some operation with object "45". Some operation with object "46". Some operation with object "47". Some operation with object "48". Some operation with object "49". Some operation with object "50". Some operation with object "51". Some operation with object "52". Some operation with object "53". Some operation with object "54". Some operation with object "55". Some operation with object "56". Some operation with object "57". Some operation with object "58". Some operation with object "59". Some operation with object "60". Some operation with object "61". Some operation with object "62". Some operation with object "63". Some operation with object "64". Some operation with object "65". Some operation with object "66". Some operation with object "67". Some operation with object "68". Some operation with object "69". Some operation with object "70". Some operation with object "71". Some operation with object "72". Some operation with object "73". Some operation with object "74". Some operation with object "75". Some operation with object "76". Some operation with object "77". Some operation with object "78". Some operation with object "79". Some operation with object "80". Some operation with object "81". Some operation with object "82". Some operation with object "83". Some operation with object "84". Some operation with object "85". Some operation with object "86". Some operation with object "87". Some operation with object "88". Some operation with object "89". Some operation with object "90". Some operation with object "91". Some operation with object "92". Some operation with object "93". Some operation with object "94". Some operation with object "95". Some operation with object "96". Some operation with object "97". Some operation with object "98". Some operation with object "99". Some operation with object "100". Some operation with object "101". Some operation with object "102".

preferences:
91.98 ms | 418 KiB | 5 Q