Solved

PHP IF STATEMENT NOT WORKING

Posted on 2007-11-30
16
338 Views
Last Modified: 2010-04-21
HI

I think i must be going mad but i cant see why the below code wont work!!!

ANy suggestions

--s--
<?php
 
// THIS WONT WORK
if($type != '' || $type != 'a')
   $search_url .= ',type%3D'.$type;
 
// THIS WONT WORK
if(!empty($type) || $type != 'a')
   $search_url .= ',type%3D'.$type;
 
// THIS WORK AROUND DOES
if($type == '' || $type == 'a'){}else $search_url .= ',type%3D'.$type;

Open in new window

0
Comment
Question by:socross
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 3
16 Comments
 
LVL 17

Expert Comment

by:nplib
ID: 20383927
if(($type != '') || ($type != 'a')) {
   $search_url .= ',type%3D'.$type;
}
0
 
LVL 1

Author Comment

by:socross
ID: 20384072
Already Tried that and its not working, this is driving me crazy!!

--s--

0
 
LVL 17

Expert Comment

by:nplib
ID: 20384093
where are you getting the value for $type,

I don't see it declared anywhere.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:steelseth12
ID: 20384168
// THIS WONT WORK
if($type != '' && $type != 'a')
   $search_url .= ',type%3D'.$type;
 
// THIS WONT WORK
if(!empty($type) && $type != 'a')
   $search_url .= ',type%3D'.$type;
0
 
LVL 1

Author Comment

by:socross
ID: 20384188
nplib

$type is a posted varible.

steelseth12

Ok so if those wont work, what will and why dont they work??

--s--
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20384204
&& (AND) will work the ones i show above .... you had || (OR)
0
 
LVL 1

Author Comment

by:socross
ID: 20384277
As the value for type comes from a drop down. it will never be && i need it to work with ||

Why would it not?

--s--
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20384306
Its a double negative you have to use && ... it just will not work will || ... why do you have to use || ??????
0
 
LVL 1

Author Comment

by:socross
ID: 20384323
well i need to say

if (type does not equal '' or type does not equal 'a') add value to the varible
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20384359
if thats what you need to say then your if statement is returning the correct result.

If type does not equal to '' OR type not equal to a then it means that in the first part it can be any value except '' (INCLUDING a) the second part any value except a (including '') .... so if its either of the 2 then you are getting the correct result.
0
 
LVL 1

Author Comment

by:socross
ID: 20384433
Right ok, so under what circustances would it run the script contained in the if statment.

I only need it to run the script if type does not equal empty or not equal a

Could you write the script that will work for this?

--s--
0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 125 total points
ID: 20384545
You need to use && to get the results you want .

if($type != '' && $type != 'a')
   $search_url .= ',type%3D'.$type;
0
 
LVL 17

Expert Comment

by:nplib
ID: 20384680
Man I go have lunch, and the problem gets solved.
Oh Well.
0
 
LVL 1

Author Comment

by:socross
ID: 20385160
just driven home and it all seemed to make sense, its amazing when you look at something so simple you can miss the really obvious stuff!!!

Thanks for persevering with me.

Thanks

--s--
0
 
LVL 1

Author Closing Comment

by:socross
ID: 31411981
Mind caught up in the end
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20385180
i once try to explain it to a friend of mine that was been programming for over 10 years. After 3 hours 2 packs of cigarettes and waking up the wife we agreed not to bring it up again. :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question