Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

John McKinney
John McKinney
15,331 Points

Undefined Variable in PHP

I keep getting an undefined variable error on lines 14 and 28 of this code (The $pageTitle = $cfstory["location"] and the <h3 id=blog-top-h3"> lines): I have attached the 2 relevant php files below. If anyone could help me understand why this variable is undefined I'd greatly appreciate it.

This file is campfire-blog.php in case it matters:

<?php include("inc\campfire_arr.php");

  if (isset($_GET["location"])){
    $cfstoryname = $_GET["location"];
    if (isset($cfstories[$cfstoryname])){
      $cfstory = $cfstories[$cfstoryname];
    }
  }
  // if(!isset($cfstory)){
  //   header("Location:camp-fire.php");
  //   exit();
  // }

$pageTitle = $cfstory["location"];
?>

<!DOCTYPE html>
<html>
<?php include('inc/header.php');?>
    <header class="contact-header">
      <?php include("inc/name_title_header.php");?>
      <?php include("inc/menu.php");?>
    </header>
    <div id="wrapper">
      <!-- <div class="breadcrumb"><a href="camp-fire.php">Campfire Stories</a> &gt; <?php echo $cfstory ["name"]; ?></div> -->
      <div id="main-left">
        <div class="campfire">
          <h3 id="blog-top-h3"> <?php echo $cfstory["location"];?> </h3>

        </div>
      </div>
    </div>
    <footer class="contact-footer">
      <?php include("inc/footer.php"); ?>
    </footer>
    <?php include('inc/js_scripts.php');?>

This one is camp-fire.php:

<?php include("inc/campfire_arr.php");?>

<!DOCTYPE html>
<html>
<?php
  $pageTitle="Campfire Stories";
  include("inc/header.php");
?>
  <header class="contact-header">
      <?php include("inc/name_title_header.php");?>
      <?php include("inc/menu.php");?>
  </header>
    <div id="wrapper">
      <div id="main-left">
        <div class="campfire">
          <h3>Campfire Stories</h3>
          <ul class="cts">
            <?php foreach($cfstories as $cfstoryname => $cfstory)
              echo display_campfire_html($cfstoryname, $cfstory);
            ?>
          </ul>
        </div>
      </div>
    </div>
    <footer class="contact-footer">
      <?php include("inc/footer.php"); ?>
    </footer>
    <?php include('inc/js_scripts.php');?>

This one is inc/campfire_arr.php:

<?php
  function display_campfire_html($cfstoryname,$cfstory){
    //build HTML output here
    $output = "";
    $output = $output . "<li>";
    $output = $output .'<a href="campfire-blog.php?php='.$cfstoryname.'">';
    $output = $output ."<figure>";
    $output = $output .'<img src="'.$cfstory["img"] .'" alt="'.$cfstory["name"].'">';
    $output = $output ."<figcaption>".$cfstory["location"]."</figcaption>";
    $output = $output ."</figure>";
    $output = $output ."</a>";
    $output = $output ."</li>";

    return $output;
  }

  $cfstories = array();
  $cfstories["clearwater-lake"] = array(
    "img" => "../img/cl-onf.jpg",
    "name" => "Clearwater Lake, Ocala National Forest",
    "location" => "Clearwater Lake"
  );
  $cfstories["denali"] = array(
    "img" => "../img/denali.jpg",
    "name" => "Denali National Park, Alaska",
    "location" => "Denali National Park"
  );
  $cfstories["arches"] = array(
    "img" => "../img/arch.jpg",
    "name" => "Clearwater Lake, Ocala National Forest",
    "location" => "Clearwater Lake"
  );
  $cfstories["yosemite"] = array(
    "img" => "../img/yosemite.jpg",
    "name" => "Yosemite National Park, California",
    "location" => "Yosemite National Park"
  );
?>

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi John,

Your code was in need of some markdown love so I fixed that up for you, if in the future you need to post code again I recommend you read Posting Code to the Forum which explains how to use markdown.


The reason you're getting a undefined variable message is because the link you have setup within your display_campfire_html function isn't correct, at the moment your query string is php but your code is looking for location. Simply change this and your code will behave as expected for known values in the $cfstories array.

$output = $output .'<a href="campfire-blog.php?location='.$cfstoryname.'">';

On another note your technique for concatenating the $output string is verbose, what I mean by that is PHP offers a helpful shortcut in which we can use .= to append new string data onto an existing string without the need to call the variable first and assign it back to the same variable which can get messy, instead we can do the below.

<?php

function display_campfire_html($cfstoryname, $cfstory) {
  $output  = "";
  $output .= "<li>";
  $output .= '<a href="campfire-blog.php?location='.$cfstoryname.'">';
  $output .= "<figure>";
  $output .= '<img src="'.$cfstory["img"] .'" alt="'.$cfstory["name"].'">';
  $output .= "<figcaption>".$cfstory["location"]."</figcaption>";
  $output .= "</figure>";
  $output .= "</a>";
  $output .= "</li>";

  return $output;
}

Hope that helps.

John McKinney
John McKinney
15,331 Points

Thank you so much Chris! Ya I figured there was a better way to post the code but didn't know where to go to find how to post it in a better way.

As for the function, I could not figure that out. That makes sense now.

And thanks for the advice on making my code less verbose. I'm still pretty new to web dev as a whole and am just starting on php so still learning the tricks so thanks for that.

Chris Shaw
Chris Shaw
26,676 Points

You're very welcome, we're always here to help.