PDA

View Full Version : if then }else{ doing if not }else{


student101
09-25-2008, 09:58 AM
This is a real life question; :mrgreen:

What does the code below say to anyone who understands what it says.

if ('item' != $_GET['news'] || $_GET['events'] || $_GET['download']){
$path='uploads/file/';
}else{
$path='uploads';
}

My understanding;
If item is not equal to news OR events OR download the path to use is uploads/file/ Otherwise the path to use uploads/

The link would look like this; <a href="?file=image.gif&item=news">IMAGE</a>
The code says what it says and I understand english; but this is german.

The link states that item is EQUAL TO news yet it uses uploads/file/ ???

This is most confusing!?

lux
09-25-2008, 10:20 AM
if ('item' != $_GET['news'] || 'item' != $_GET['events'] || 'item' != $_GET['download']){
$path='uploads/file/';
}else{
$path='uploads';
}


also lookup: http://www.php.net/switch

using a switch statement might be an idea

student101
09-25-2008, 10:27 AM
Thanks, I guess the objective is that it will always be the oppisite.
if x != x then x else y
Which should be;

if x != x then y else x

lux
09-25-2008, 10:34 AM
What does the code below say to anyone who understands what it says.

if ('item' != $_GET['news'] || $_GET['events'] || $_GET['download'])



This code says nothing. You use an if statement to make comparisions but after you compare item to news you don't compare events or downloads to anything.

look at the code i supplied and see that it includes a comparison between item and news,events and download.

student101
09-25-2008, 10:42 AM
Yes I saw that you made it a real version;
My problem is the part that states: if 'item' != $_GET['news'] it says not equal to yet it's equal to news? and the file still is available.
I have cleared all the files and the only folder where this file is in uploads/file/

lux
09-25-2008, 10:58 AM
i think you need to use && instead of ||

if ('item' != $_GET['news'] && 'item' != $_GET['events'] && 'item' != $_GET['download']){
$path='uploads/file/';
}else{
$path='uploads';
}


does that answer your question?

student101
09-25-2008, 11:05 AM
Using the && (AND) or the ||(OR) still does the same thing.
You did understand what I meant or mean though, right?
I mean I'm not crazy or anything, right?

Cheers

lux
09-25-2008, 11:11 AM
i don't really know what your question/aim is???

give me some more code and data and explain what it is you want to do.

when i get back from lunch i'll try to clear it up for up

student101
09-25-2008, 11:13 AM
It was just that piece of code that was confusing the hell out of me.
I wasn't understanding why the file was still available when it was set to != (not equal to)

Cheers

lux
09-25-2008, 11:44 AM
its still available because the other conditions are returning true

when you only test against one condition you will get the result you are looking for.


$item = "sport event";

$news = "sport event";

if($item != $news){
echo "uploads/file/";
}else{
echo "uploads/";
}



you need to revise how you compare the other conditions in the if statement

is that clear?

davidj
09-25-2008, 12:00 PM
$test1 = "testA";
$test2 = "testB";
$test3 = "testC";


if($test1 != 'test1' || $test2 != "test2" || $test3 != "test3"){

echo "This is looking for only ONE NOT TO MATCH before this is printed";

}

if($test1 != 'test1' && $test2 != "test2" && $test3 != "test3"){

echo "This is looking for ALL NOT TO MATCH before this is printed";

}

student101
09-25-2008, 12:26 PM
its still available because the other conditions are returning true
when you only test against one condition you will get the result you are looking for.
you need to revise how you compare the other conditions in the if statement
is that clear?

Yes thanks.

Cheers

student101
09-25-2008, 12:29 PM
$test1 = "testA";
$test2 = "testB";
$test3 = "testC";
if($test1 != 'test1' || $test2 != "test2" || $test3 != "test3"){
echo "This is looking for only ONE NOT TO MATCH before this is printed";}
if($test1 != 'test1' && $test2 != "test2" && $test3 != "test3"){
echo "This is looking for ALL NOT TO MATCH before this is printed";}
You should know me, I always get confused :confused: with if then else stuff.

The || and && parts I fully understand.

Cheers

gavimobile
10-01-2008, 02:24 PM
student101,
maybe i can help you understand it from a noob point of view, this is what dj was trying to post





$var1 = "var1";
$var2 = "var2";
$var3 = "var3";

//beg of if
if($var1 != 'var1' || $var2 != "var2" || $var3 != "var3"){
//ok, lets check if EITHER $var1 is different from var1 OR THAT $var2 is different from var2 OR THAT $var3 is different from var3

echo "if JUST ONE of these $var's do not match than lets, PRINT SOMETHING HERE!!!";

}//end of if

//beg of new if
if($var1 != 'var1' && $var2 != "var2" && $var3 != "var3"){
//ok lets MAKE SURE that $var1 if different from var1 AND ALSO LETS MAKE SURE THAT $var2 is different from var2 AND ALSO LETS MAKE SURE that $var3 is different from var3

echo "if ALL of these (all 3 $var's) do not match, than lets, PRINT SOMETHING HERE";

}//end of if

i hope this was helpful, if you are getting overwelmed, let me know your exact problem, and ill try my best to help!

student101
10-01-2008, 02:42 PM
Thanks, I got it working, always get confused with if then else - ODD I know :roll:
My logic is opposite some(most)times

Thanks :p

gavimobile
10-01-2008, 03:22 PM
Thanks, I got it working, always get confused with if then else - ODD I know :roll:
My logic is opposite some(most)times

Thanks :p

im the exact same way(ADD).... so i started to do sample scripts in a temp folder before i do any scripts. so i get the hands on knowledge!

dont overwelm youself with to much, always start small than once u get it right, apply it to your project!

gavimobile