Jump to content

undefined variable in functions.php


David-M
 Share

Recommended Posts

anyone, any ideas how to solve? 

i get following error on a website:

Notice: Undefined variable: out in /var/sites/i/islandmemorials.co.uk/public_html/site/templates/includes/functions.php on line 43

and this is a code in that very functions file:

<?php
  function numberOpen() {
    date_default_timezone_set("Europe/London");
    $currentHour = date('G');
    if($currentHour > 9 && $currentHour < 21) {
      $openFor = 21 - $currentHour;
      if($openFor > 1) {
        echo "We're currently open for another ".$openFor." hours, why not pop in?";
      } else {
        echo "Our lines are open for another hour - <a href=\"tel:+01983 857 600\">01983 857 600</a>";
      }
    } else {
      echo "Unfortunately, our lines are currently closed, but you can still send a message below";
    }
  }

	function getSubNav($pid) {

				$test =  wire('pages')->get("/services/");
				$parents =  wire('page')->parentID;
				$template = wire('page')->template;
				$out = '';

				//echo "pid".$pid."<BR>";
				//echo "parents".$parents."<BR>";
				//echo "template".$template."<BR>";


				$children = $test->children;
                
				foreach($children as $child) {
				     
				    $class = $child->id === $pid ? " active" : 'inactive';
                   

					if ($child->id === $pid ) {
						$class= 'active';
					}

					elseif ($template= "memorials"  &&  $child->id === $parents ) {
						$class= 'active';
					}


					$out .= "<li><a class='{$class}' href='{$child->url}'>{$child->title}</a></li>";


				}
				return $out;

	}

?>

i suppose this has to do with a variable scope, but not sure

Link to comment
Share on other sites

if ($child->id === $pid ) {
	$class= 'active'; // Why are you declaring it here for the 2nd time?
}


elseif ($template= "memorials"  &&  $child->id === $parents ) { // $template == ... missing double equal sign btw
	$class= 'active';
}

I think you get Undefined because you are overwriting class after the first declaration and then sometimes your if statements never run (depending on false conditions) and so $class is not set at that point.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...