How do I make a variable from outside a function work in that function?
<?php
$data = "Nothing to declare";
function nothing() { echo @"I have $data"; }
function todeclare() { global $data; echo "I have $data"; }
nothing(); echo "<br />"; todeclare();
?>
This will print on the first line:
'I have'
and then on the next line
'I have Nothing to declare'
This illustrates that the global keyword is needed to allow the function to access its value from outside the function.
The '@' before the text in nothing supresses a PHP Notice error when it tries to echo an undefined variable.
Comment on this Question and Answer >>>
ASK A QUESTION
More functions PHP Questions
Hello, My question is about general practices. With classes, is error handling usually done inside of the class' functions, or would you just do the bare bones amount of work in the class, and then do error checking when you use the function elsewhere?How can I call online a PHP function. It works local but when I put it online it does nothing. I call online a php function to load pictures into my flex application. Thanks Bart
WHAT IS THE CORRECT WAY TO OPEN THE FILE "time.text" AS READABLE?
How can I access information sent through $_POST on a form?
which function can assign the output to a string variable?
