3

Im including almost every page in page_protect(); . I have made an variable for the userid, $userid, with the user´s id. So it will be much easier for me to get the id than calling SESSION_id each time on every page.

How can i use a variable inside that function outside the function?

Karem
  • 16,343
  • 69
  • 163
  • 271

1 Answers1

12
function page_protect()
{
  global $id;
  $id = 1234;
}

page_protect();
echo $id;
Bill Karwin
  • 462,430
  • 80
  • 609
  • 762
  • I know this answer was written almost 4 years ago, but I'd like to add this as a note for future visitors: You shouldn't use `global`s anymore. [They're evil](http://stackoverflow.com/a/5166527/1438393). – Amal Murali Mar 31 '14 at 07:52
  • @AmalMurali, agreed, it's better to use function parameters and return values. But let people walk before they run. – Bill Karwin Mar 31 '14 at 18:02