3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function getCollectionParentIDFromChildID($cID) { if(is_numeric($cID)) { if($cID > 1) { return '1'; } if($cID == 1) { return '0'; } } return null; } function populateParentIDArray_current($cID) { global $cParentIDArray; $cParentID = getCollectionParentIDFromChildID($cID); if($cParentID > -1) { if(!in_array($cParentID, $cParentIDArray)) { $cParentIDArray[] = $cParentID; } populateParentIDArray_current($cParentID); } } function populateParentIDArray_new($cID) { global $cParentIDArray; $cParentID = getCollectionParentIDFromChildID($cID); if(is_numeric($cParentID)) { if(!in_array($cParentID, $cParentIDArray)) { $cParentIDArray[] = $cParentID; } if($cParentID > 0) { populateParentIDArray_new($cParentID); } } } function runTest($cID) { global $cParentIDArray; echo "Test case for "; var_dump($cID); $cParentIDArray = array(); populateParentIDArray_current($cID); echo "CURRENT: \n"; var_dump($cParentIDArray); $cParentIDArray = array(); populateParentIDArray_new($cID); echo "CURRENT: \n"; var_dump($cParentIDArray); } runTest('3'); runTest('1'); runTest('0'); runTest(3); runTest(1); runTest(0); runTest(null); runTest('');
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Test case for string(1) "3" CURRENT: array(2) { [0]=> string(1) "1" [1]=> string(1) "0" } CURRENT: array(2) { [0]=> string(1) "1" [1]=> string(1) "0" } Test case for string(1) "1" CURRENT: array(1) { [0]=> string(1) "0" } CURRENT: array(1) { [0]=> string(1) "0" } Test case for string(1) "0" CURRENT: array(0) { } CURRENT: array(0) { } Test case for int(3) CURRENT: array(2) { [0]=> string(1) "1" [1]=> string(1) "0" } CURRENT: array(2) { [0]=> string(1) "1" [1]=> string(1) "0" } Test case for int(1) CURRENT: array(1) { [0]=> string(1) "0" } CURRENT: array(1) { [0]=> string(1) "0" } Test case for int(0) CURRENT: array(0) { } CURRENT: array(0) { } Test case for NULL CURRENT: array(0) { } CURRENT: array(0) { } Test case for string(0) "" CURRENT: array(0) { } CURRENT: array(0) { }