...

View Full Version : Parse error:



Dat
02-15-2008, 05:50 AM
I get this error when I go to /add/


if ($_GET['add'] == "news"){
I have this in my code and if it's "?add=news" then whatever but when I got /add/ it displays this error:
Notice: Undefined index: add in **/add/index.php on line 27

When I try to add isset()

if (isset($_GET['add'] == "news")){
with this error
Parse error: syntax error, unexpected T_IS_EQUAL, expecting ',' or ')' in /**/add/index.php on line 27

There must a be a simple solution and reason for this?

rangana
02-15-2008, 07:34 AM
...It should be
if(isset($_GET["add"] )){ instead

There should be no comparison inside the isset (http://us3.php.net/isset)function

See if it helps :D

Dat
02-16-2008, 03:59 AM
Then am I using this correctly?:


if(isset($_GET['add'] )) {

if($_GET['add'] == "news") {
echo 'news';
}elseif($_GET['add'] == "anime") {
echo 'anime';
}elseif($_GET['add'] == "manga") {
echo 'manga';

} else {
echo 'Select something to do!';
}

It work's but is this the only approach, I mean have to say that it looks really irregular to me... It could be because of my inexperience but it look un pro-like?, unorganized?

rangana
02-16-2008, 04:21 AM
It's fine!...nothing could cause a harm:D

Inigoesdr
02-16-2008, 04:26 AM
You're missing a closing brace:

if(isset($_GET['add'] )) {

if($_GET['add'] == "news") {
echo 'news';
}elseif($_GET['add'] == "anime") {
echo 'anime';
}elseif($_GET['add'] == "manga") {
echo 'manga';
} // here
} else {
echo 'Select something to do!';
}

Also, if you're making multiple comparisons of the same value it's generally preferable to use the switch() (http://php.net/switch) statement:

if(isset($_GET['add']))
{
switch($_GET['add'])
{
case 'news':
echo 'news';
break;

case 'anime':
echo 'anime';
break;

case 'manga':
echo 'manga';
break;

default:
// this is optional
break;
}
}
else
{
echo 'Select something to do!';
}

Dat
02-16-2008, 04:27 AM
I guess. How would you approach this type of situation. Would you use the same kind of code I'm using?

_Aerospace_Eng_
02-16-2008, 04:35 AM
He posted an example of what should be used.

Dat
02-16-2008, 04:39 AM
Sorry, I posted 20 seconds after that, thank you. I used that approach, and it work's like a dream.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum