Re: Newbie - Undefined Index Variable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 11:59 AM -0800 3/7/08, revDAVE wrote:
I have this:

<?php if (trim($_SESSION['nowtoctype']) == 'thistype')

{print '<a href="page1.php">page1</a>';}else{print '<a href="
page2.php">page2</a>';} ?>

I will get the error:

Notice: Undefined index: nowtoctype in ...

If the variable has not yet been defined...

Is there a way to check if this var is already defined? ... (and then I can
define it - if not)

This is something I run into often and here is how I solve it.

	$action = isset($_GET['action']) ? $_GET['action'] : null;
	$action = isset($_POST['action']) ? $_POST['action'] : null;
	$action = isset($_SESSION['action']) ? $_SESSION['action'] : null;

If you expect anything to be provided via GET, POST, or SESSION, then use one of these to initialize it. The null, of course can be changed to whatever default value you want.

Cheers,

tedd
--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux