LemonInflux Posted November 4, 2007 Share Posted November 4, 2007 A while back, one of my friends did a website, and they had written a function library. Nothing special, I'd done a few of my own functions, but he said he could use the functions without including the library. He said it was something to do with 'code scope'. I tried to find some easy explanations, but they all seem very complex. Is there a simple way to use this? Or, is it the wrong word? Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 4, 2007 Share Posted November 4, 2007 There is no way to use an external function (stored in a separate library) without including it. You can include it via including the library script, or copy/paste the single function you need from the library directly into your script. Scope is used to define when a variable retains its previous set value, or 'state'. <?php $a = 'this'; $b = 'that'; echo "a = $a<br />"; // prints this echo "b = $b<br />"; // prints that echo "this_n_that says:<br />"; print_this_n_that(); // does nothing, because $a and $b do not have scope within the function echo "this_n_that2 says:<br />"; print_this_n_that2(); // prints this and that because you have passed scope to the function via global. function print_this_n_that() { echo "a = $a<br />"; // prints nothing ($a has no scope) echo "b = $b<br />"; // prints nothing ($b has no scope) } function print_this_n_that2() { global $a, $b; echo "a = $a<br />"; // prints this echo "b = $b<br />"; // prints that } ?> PhREEEk Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.