Jump to content

[SOLVED] How to access variables declared outside a function from a function?


obay

Recommended Posts

you should try and NOT use something that depends on state if you can. If there were no session because the user had cookies off then you would get NO joy from that.

 

keeping your code as free from state dependance is very good thing.

 

In this case you either pass the variable to teh function or you decalre the variable as global within the function...

 

<?php

$myvar = 'hello';

// passing the variable explicitly...
function fxn ($myvar)
{
echo $myvar;
}

// defining the variables scope from within the func...

function fxn ()
{
global $myvar;
echo $myvar;
}
?>

 

Pros and cons to each so you decide which fits your requirements best.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.