How to get my php if, elseif, else statements to work?!

Hi, if I simply echo my variable it displays:

echo $select_page['name'];

but when I try to put it in an elseif statement:

if(isset($select_subject)) {
      // Displays menu names for the current subject
      echo "<h3>" . $select_subject['menu_name'] . "</h3>";
      echo "<div>" . $select_subject['content'] . "</div>";
}
elseif(isset($select_page)) {
      // Displays menu names for the current page
      echo "<h3>" . $select_page['name'] . "</h3>";
      echo "<div>" . $select_page['content'] . "</div>";
}
else {
      // Default menu name
      echo "<h3>Select a page to edit</h3>";      
}

it does not work, nor does the else statement.  Only the first if statement works.  Can anyone tell me why?
if(isset($_GET['subj']) && intval($_GET['subj']) > 0) {
	// Stores the subject id into the variable
	$select_subject = get_subject_by_id($_GET['subj']);
    $select_page = "";
}
elseif (isset($_GET['page']) && intval($_GET['page']) > 0) {
	// Stores the page id into the variable
	$select_page = get_page_by_id($_GET['page']);
    $select_subject = "";
}
else {
      $select_subject = "";      
	  $select_page = "";
}

Open in new window

FairyBusinessAsked:
Who is Participating?
 
Beverley PortlockCommented:
isset works on seeing if a variable is defined so a value of " " returns TRUE

PHP equates NULL as FALSE, but " " is neither NULL nor FALSE

See http://uk2.php.net/isset


"Notice: Undefined index: content in /hermes/web09c/b2950/moo.auroriellacom/content.php on line 31

How do I define the content in side of $select_subject['content']"


This error message simply means that you tried to access an array with a key of 'content' when no such key existed. To set such a key simply assign a value like so
$select_subject = array();     // new array

echo $select_subject['content'];    // error - no key called 'content in the array

$select_subject['content'] = 1234;

echo $select_subject['content'];    // now it works and outputs 1234

Open in new window

0
 
Beverley PortlockCommented:
There is nothing obviously wrong with the code. Therefore we have the following possibilities

1) The code works, but none of the conditions are fulfilled thus no output

2) There is an error but PHP is not showing the message. Add this ini_set('display_errors' ,1); error_reporting(E_ALL); to the start of your PHP script and see if any message appear.

Also there are two code fragments above. Which one has the error in?
0
 
FairyBusinessAuthor Commented:
I got it to work by setting the variables to NULL instead of " "

but I am curious about that error code you gave me.

I got this message even though it worked:

Notice: Undefined index: content in /hermes/web09c/b2950/moo.auroriellacom/content.php on line 31

How do I define the content in side of $select_subject['content']
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
FairyBusinessAuthor Commented:
oh I figured that out too!!

Ok, I'll give you the points if you answer me this question:

(I had never seen that error code before)  Is there anything I should know before putting it onto all of my pages? Any certain ways to work with it??
0
 
FairyBusinessAuthor Commented:
Oh I see thanks.

What does the 1 mean in ini_set('display_errors' ,1);

that it only displays one error message at a time?
0
 
Beverley PortlockCommented:
"(I had never seen that error code before)  Is there anything I should know before putting it onto all of my pages? Any certain ways to work with it??"

E_NOTICE errors like this are very, very common when working with arrays because often you attempt to access an array to test if a key is present and if no key is present then you get a NOTICE error. It doesn't mean that anything is wrong with the code.

On the other hand sometimes you find that a value you were ceratin would exist in an array is not there and in this instance a NOTICE error is pointing out a problem.

It is always worth turning it on every so often and having a look at what turns up. Make a judgement and decide whether the NOTICEs mean anything important and then act accordingly.

In general, 90% of the time, NOTICEs don't mean anything much.
0
 
Beverley PortlockCommented:
"What does the 1 mean in ini_set('display_errors' ,1);"

No - 1, or 'On' or 'true' is needed there to indicate that I want display_errors turned on.  Basically anything that is not NULL, false or zero would do. I could have used 31415926 if It was in the mood because it is neither NULL, zero or false.
0
 
FairyBusinessAuthor Commented:
Thanks for all of your help!
0
 
FairyBusinessAuthor Commented:
Oh, could you also tell me what this means exactly:

error_reporting(E_ALL)

what does the E_ALL represent?
0
 
Beverley PortlockCommented:
It means "show all errors"

http://www.php.net/error_reporting
0
 
Beverley PortlockCommented:
You might also want to look at this

http://uk3.php.net/manual/en/errorfunc.constants.php

Cheers!
0
 
FairyBusinessAuthor Commented:
Thanks again!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.