0

This is simple but I am trying to echo a value from function-1 inside function-2

function testing(){
   $files1 = "dir";  
}

function tested() {
    testing();
    echo "testing".$files1;
}

Any help on why its not working would be appreciated.

Answer: https://stackoverflow.com/a/45387323/257705

X10nD
  • 21,638
  • 45
  • 111
  • 152

2 Answers2

3

For this you have to return value from function

function testing(){
   $files1 = "dir";  
   return $files1;
}

function tested() {
    $files1 = testing();
    echo "testing".$files1;
}
tested();
B. Desai
  • 16,414
  • 5
  • 26
  • 47
2

they function run but the second function doesn't understand the local variable from function one.

I would do the following

<?php
function testing(){
   $files1 = "dir"; 
    return $files1;
}

function tested() {
   $files1 =  testing();
    echo "testing".$files1;
}

tested();
?>

or you could use the global variable.

<?php
function testing(){
   $GLOBALS['files1'] = "dir"; 
   // return $files1;
}

function tested() {
    testing();
    echo "testing ". $GLOBALS['files1'];
}

tested();
?>
Võ Minh
  • 155
  • 2
  • 12