Jump to content


Undefined Variables -,-

  • Please log in to reply
1 reply to this topic

#1 CLU



  • Member
  • PipPip
  • 24 posts

Posted 16 February 2012 - 03:29 PM

Hi Guys ok,

Im starting to get the hang of PHP OOP and MVC, what i cannot seem to prevent are Notices: Undefined Variables. I understand i can switch this off, but i just want to know why proper way to get this done. Does anyone know.

Here is my code:

From my model m_settings:
function siteSettings() 
		if ($stmt = $FS->Database->query("SELECT * FROM site_settings WHERE id='1'"))
		$row = mysqli_fetch_array($stmt);

This is just grabbing the results from a database and relaying them onto the view... which is the following:


	<tr><td>Browser Title:
		<input type="text" name="browser_title" value="<?php echo $row['browser_title'];?>" placeholder="Enter your sites browser title here.">									

	<tr><td>Site Meta-Tags:
		<input type="text" name="met_tag" value="<?php echo $row['meta_tag'];?>" placeholder="Site Meta-Tags go here.">					

		<input type="text" name="description" value="<?php echo $row['description'];?>" placeholder="Site Description.">					

All the code above is placed within a table.

This is the controller

$Settings = new Settings();

$FS->Template->load(APP_PATH . 'settings/views/v_index.php');

I can't understand why I'm getting the following Notices :

<br /><b>Notice</b>: Undefined variable: row in <b>/Applications/MAMP/htdocs/ukmocks/app/settings/views/v_index.php</b> on line <b>26</b><br />

The above happens for every $row echoed out...

I know i can add some code to the top of the page that will get rid of the notices, but i love to learn and want to know why this si happening :)

If anyone can help give me a shout

Thanks again
  • 0

#2 Ben



  • Administrators
  • 5,682 posts
  • LocationGainesville, Florida

Posted 18 February 2012 - 08:23 PM

I unfortunately don't have the time to give you a full response, but just for a quick reply: I believe you are getting undefined errors because you are trying to access a variable that is out of the scope. Your $row variable within siteSettings() only exists within that function -- it isn't automatically shared everywhere in the web app. I'd suggest that you do some research on "variable scope" -- understanding this is pretty important and will help you to fix or avoid a lot of bugs.
  • 0
Benjamin Falk
Falken Creative : Twitter

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

★★★★★ 5 Star Rated Web Developer Course - check it out now!