PHP OOP: Can a variable be seen in a function without passing it as a param?
<p>I was wondering if anyone could help me understand this particular aspect of OO PHP as it has caught me out a few times now. </p>

<p>I have specified a var at the top of my file like so</p>

<pre><code>$route = explode('/', $_SERVER["REQUEST_URI"]);
// Shorthand
$r1=$route[0]; $r2=$route[1]; $r3=$route[2];
</code></pre>

<p>I am then tring to use $r1 etc inside a function written just below the above code.</p>

<pre><code>function edit($id)
   {
            $_SESSION['path'] = $r1 . "/" . $r2 . "/" . $r3;
            require 'app/controller/edit.php';
            new Controller($id);
   }
</code></pre>

<p>The $r1,$r2, $r3 vars cannot be seen for some reason inside this function.</p>

<pre><code>Notice: Undefined variable: r1 in C:\wamp\www\options.ex\public_html\app\options.php on line 77
</code></pre>

<p>If I were to pass the $r vars to the function, I'd image there would be no problem, but since they are declared globally I was wondering why they were not visible without doing this as their scope presumably is global?</p>

<p>Thanks.</p>

<p>EDIT - full code.</p>

<pre><code>&lt;?php

require_once 'load.php';


// Clean and prepare query string
$route = explode('/', $_SERVER["REQUEST_URI"]);
    // Trim outer exmpty parameters caused by leading/trailing slash
    if($route[count($route)-1]=='') unset($route[count($route)-1]);
    if($route[0]=='') unset($route[0]);
    $route = array_values($route);
    // If any parameters are undefined, set them to ''
    if(!isset($route[0])) { $route[0]=''; $route[1]=''; $route[2]='';  }
    elseif(!isset($route[1])) { $route[1]=''; $route[2]='';  }
    elseif(!isset($route[2])) { $route[2]='';  }
// Shorthand
$r1=$route[0]; $r2=$route[1]; $r3=$route[2];

// Choose route, else default to dash
if($r1=='dashboard' &amp;&amp; $r2=='' &amp;&amp; $r3=='')          dashboard();
elseif($r1=='staff' &amp;&amp; $r2=='add' &amp;&amp; $r3=='')       add_staff();
elseif($r1=='staff' &amp;&amp; $r2=='edit' &amp;&amp; $r3!='')      edit_staff($r3);

else header("location: http://local.options.ex/dashboard");



// Dashboard: Main entry point after login.
function dashboard()
   {
            require 'app/controller/dashboard/dashboard.php';
            new Controller();

   }

// Staff related interfaces ----------------------------------------------------
function add_staff()
   {
            require 'app/controller/staff/add_staff.php';
            new Controller();
   }

// ----------------------------------------
function edit_staff($staff_id)
   {
            $_SESSION['path'] = $r1 . "/" . $r2 . "/" . $r3;
            require 'app/controller/staff/edit_staff.php';
            new Controller($staff_id);
   }

// ----------------------------------------
</code></pre>

<p>Just to clear up, the $r* variables are not used again beyond here, hence the convenient use of storing in session.</p>
