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
WHAT IS THE CORRECT WAY TO OPEN THE FILE "time.text" AS READABLE?How do I return a value from a function?
Can we use include ("abc.php") two times in PHP page "makeit.php" ?
how can i get the data by using get(),post()methods?
difference between fetch_array function & fetch_row function
