callback - Can not echo the value outside of the function using PHP -


i need echo value outside of function using php. explaining code below.

complain.php:

generateticketid('w1',$con,function($ticket){            $ticket_id=$ticket; })  echo $ticket_id; 

here using callback function when trying echo value of $ticket_id,i not getting anything.please me resolve issue.

in php, global variables not visible inside functions. also, expected, local variables of function not visible in other functions or in global context.

there several ways use variable in context not visible, depending on usage type (read, write or both).

in particular situation, appropriate way use use language construct:

// external $ticket_id (global or local function contains code) $ticket_id = 'foo';  generateticketid('w1', $con, function($ticket) use (& $ticket_id) {     $ticket_id = $ticket;   // internal $ticket_id, local anonymous function });  echo $ticket_id; 

the use keyword binds variable $ticket_id (visible context code runs) anonymous function created code. copy of variable visible inside anonymous function local variable of function.

the usage of reference operator & makes variable local anonymous function alias of external $ticket_id variable, making possible change value of external variable inside anonymous function's scope.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -