T
T
Theory Theory2020-05-28 09:16:47
PHP
Theory Theory, 2020-05-28 09:16:47

Why doesn't a function in php have access to external variables?

let a = 5;

function inc() {
    return a;
}
console.log(inc()); // 5


$a = 5;
function inc() {
  return $a;
}

if(true) {
  echo $a; // 5
}

echo inc();  // undefined

Why doesn't a function in php have access to external variables?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
L
Lander, 2020-05-28
@Narbek

According to the prophecy.
If you want the php function to see an external variable, then you need to import it into the function using
global $a;

N
nokimaro, 2020-05-28
@nokimaro

Variable scope for full immersion
https://www.php.net/manual/en/language.variables.s...

$a = 5;
function inc() {
  return $GLOBALS["a"];
}

var_dump($a); //int(5)

var_dump(inc()); //int(5)

A
Anton, 2014-04-24
@ZombieHamster

If you just need to put devices on the map, then The Dude from Mikrotik is perfect.XYj0E.jpg

U
uafranc, 2014-04-24
@uafranc

Thanks, I'll try!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question