0

I'm trying to return a global variable in my PHP code and I cant seem to figure out why I cant return variables from a function no matter what I try. What am I doing wrong here, go easy I'm a newbie at this.

<?php

$Page = $LoadPageContent = "";

function Loadfunction(){

   $Page = "Home";
   $LoadPageContent = "../content/" . $Page . ".inc"

   // Tried global with no result
   // global $Page = "Home";
   // global $LoadPageContent = "../content/" . $Page . ".inc";

   // Tried return with no result
   // return $Page = "Home";
   // return $LoadPageContent = "../content/" . $Page . ".inc";  

}

Loadfunction();

echo "Page: $Page<br>LoadPageContent: $LoadPageContent<br>";

exit;
?>
Wille G
  • 31
  • 5
  • 1
    To have access to global variables within a function you need insert _global $Page; global $LoadPageContent;_ at the beginning of the function. – lukas.j Feb 04 '22 at 17:58
  • You can pass the variables in with the `&` symbol, so that the variables outside will be updated as well – aynber Feb 04 '22 at 17:59
  • 2
    It's just `global $Page;` not `global $Page = "Home";` – Barmar Feb 04 '22 at 18:00

0 Answers0