Page 1 of 1

[Q]

PostPosted: 10. December 2009 13:13
by Eng_A_Moktar
I'm used to write my php scripts shortly, like this way
if($_GET['var'])
instead of
if (isset($_GET['var']))

it works fine on my localhost, before i moved to XAMPP,
"I believe XAMPP is better or even the best, so i moved to .."

but when i'm testing my old scripts on xampp i get this error with script like this one
Code: Select all
<?php
   if($_GET['var']){
      echo $_GET['var']   ;
   }else{
      echo "err";   
   }
?>


the error :
Code: Select all
Notice: Undefined index: var in /opt/lampp/htdocs/account/ro.php on line 2
err


I wanna some way to get over this, so not have to change my scripting way or all my files :?:
any ideas ?
thanks a lot :wink:

Re: [Q]

PostPosted: 10. December 2009 13:17
by Wiedmann
Search the PHP Manual for error_reporting.

Re: [Q]

PostPosted: 10. December 2009 13:18
by Eng_A_Moktar
thanks for fast response
it works fine every where, but in xampp it gives me that errors

Re: [Q]

PostPosted: 10. December 2009 14:20
by Altrea
It's best practice not to ignore unset variables/arraykeys.
If you want to shorten your Code this way, you can use an own function.

like this:
http://stackoverflow.com/questions/376623/php-printing-undefined-variables-without-warning

Re: [Q]

PostPosted: 10. December 2009 14:36
by Eng_A_Moktar
no, you did not understand me,
i have something like this

if ($_GET['userid']){
//do something
}elseif($_GET['userlevel']){
//do something
}

or
if ($_SESSION){
//do something
}

all this doesn't work
i'm not seekin for hide the error message

Re: [Q]

PostPosted: 10. December 2009 15:21
by Altrea
in your first example you use this code:

Code: Select all
<?php
   if($_GET['var']){
      echo $_GET['var']   ;
   }else{
      echo "err";   
   }
?>


Looks like a simple "if a variable isset, take this value, else take a default value.
This example can be found in the postet link.

to use isset() is a best practice. thats it.
If you don't want to use it, because its too long and unreadleble for you, you must use an own workaround (or switch off your error_messages with an @ or the error_reporting() function, which both is very sloppy to simply not show existent messages)

An if-statement is simply a boolean true or false choice.
Code: Select all
if ($_GET['userid'])

simply means, if the value of $_GET['userid'] is true...

if you want to check whether or not a variable isset or has some value, use isset() or empy()

Re: [Q]

PostPosted: 10. December 2009 15:25
by Eng_A_Moktar
I'm with you in all
but the simple question is :
why it was not that way in AppServ ??

Re: [Q]

PostPosted: 10. December 2009 15:30
by Altrea
well...

maybe different php default settings.
or different php behavior because of a newer php version?

can't say anything about your old and new webserver package ^^

Re: [Q]

PostPosted: 10. December 2009 15:37
by Eng_A_Moktar
some login pages were made with dreamweaver,
session are not working fine
the $MM_Username is not defined in XAMPP, but it works fine in my AppServ, and my site !

I really wanna move to xampp
thanks for being patient..

Re: [Q]

PostPosted: 10. December 2009 15:42
by Wiedmann
i'm not seekin for hide the error message

Sure. Exactly that's you want in your first post.

Re: [Q]

PostPosted: 10. December 2009 15:55
by Eng_A_Moktar
thanks again
but no o'm not seeking for hide error messages
again
i have :
if ($_GET['cat']){
//do cat thing
}elseif($_GET['userid']){
//do something else
}

this is not working
and the DW login session variables

Re: [Q]

PostPosted: 10. December 2009 16:03
by Wiedmann
Code: Select all
if ($_GET['cat']){
//do cat thing
}elseif($_GET['userid']){
//do something else
}

this is not working

Sure, that is working.
But in a default XAMPP installation (and many other PHP installations) you have additionally a "Notice".

Re: [Q]

PostPosted: 10. December 2009 16:14
by Eng_A_Moktar
Thank so very much
you were very kind and helpful