Forum Moderators: coopster

Message Too Old, No Replies

Simple IF statement question

         

angst

8:01 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



Hello,

I'm trying to use a simple if statement to show an error on the page, my code:

if ( $_REQUEST['error'] == 'nick_exists' ) {
echo "The selected nickname already exists, please select a new Nick Name";
}

which works fine, the problem is that,
if there is no error returned in the querystring, then apache/php gives me this error:

Notice: Undefined variable: error in signupform.php on line 98

line 98 is:

if ( $_REQUEST['error'] == 'nick_exists' ) {

how do i solve this issue?

thanks in advance for your time!
-Ken

mcibor

8:28 pm on Jul 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As you see this is not an error, just notice.
change error reporting to 0 - then no error will show up:

error_reporting(0);

This should get rid of the notice, however remember, that you won't see any error at all. Therefore for development reason you should leave error_reporting(E_ALL); and for the ready to use product error_reporting(0);

Or you can correct the statement:

if(isset($_REQUEST['error'])) { if ( $_REQUEST['error'] == 'nick_exists' ) {
echo "The selected nickname already exists, please select a new Nick Name";
}}

No error here
Best regards
Michal Cibor

Birdman

8:30 pm on Jul 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just check to see if the variable exists first. Then, check it's value:

if ( isset($_REQUEST['error']) && $_REQUEST['error'] == 'nick_exists' ) {
echo "The selected nickname already exists, please select a new Nick Name";
}

In the above example, PHP first checks to see if the var is set. If it is not, then the second condition will not even be evaluated. The "&&" basically means "and". It's called a logical operator [php.net].

Added: Or you could do it mcibor's way (with two ifs) :)

mcibor

9:05 pm on Jul 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the Birdman's suggestion. I didn't know that evaluation of if stops when the condition is know for sure as here. The var is not set, then the whole statement is false.

Best regards
Michal Cibor