How to echo a variable from the function? This is an example code.
function test() {
$foo = 'foo'; //the variable
}
test(); //executing the function
echo $foo; // no results in printing it out
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The immediate answer to your question would be to import
$foointo the function’s scope:More on variable scope in PHP here.
this is, however, bad practice in most cases. You will usually want to return the desired value from the function, and assign it to
$foowhen calling the function.