php - Can't get printTimeLineItemMetadata to pass variables outside of the function -
hello i've been trying mirror api check if timeline item has been deleted , if create new 1 if not update old one. programming in php , know seems should straight forward reason i'm stuck! please help! right have setup , doesn't seem want pass variable outside of function , can't insert inside of function either.
function printtimelineitemmetadata($service, $itemid) { try { $timelineitem = $service->timeline->get($itemid); print 'timeline item id: ' . $timelineitem->getid(); if ($timelineitem->getisdeleted()) { $deleted = "deleted"; echo $deleted; } else { $deleted = "nope"; echo $deleted;} } catch (exception $e) { print 'an error occurred: ' . $e->getmessage(); } }
i can $deleted echo in both cases once try , pass $deleted if statement further down below won't take.
as question asker explained in comment on question, has scoping. functions in php have own scope, variables defined in function default not exist outside of function.
the quick-fix make variable global
, having use many global variables poor software design. if find in situation, try rethinking code make more modular.
Comments
Post a Comment