!= operator question

Posted on 2011-05-04
Last Modified: 2012-05-11
the code works if I remove the or portion but I need to check to make sure its not either 9 or 10.  I am not sure what I am doing wrong
if(($row['cnid'] != '9') || ($row['cnid'] != '10')) {

Open in new window

Question by:iceman19330
    LVL 12

    Expert Comment

    Try && instead of ||
    LVL 27

    Expert Comment

    by:Lukasz Chmielewski
    So to be complete
    if(($row['cnid'] != '9') && ($row['cnid'] != '10'))

    if the cnid is not 9 AND is not 10 - do something
    Mind that you're comparing the string value '9' and '10'
    To check if the $row['cnid'] is not the value and the type integer you would use !==
    LVL 12

    Accepted Solution

    Another way to write it would be
    if (!($row['cnid'] == '9' || $row['cnid'] == 10)) {

    Open in new window

    if that makes more sense.

    It's all about the logic.
    LVL 27

    Expert Comment

    by:Lukasz Chmielewski
    the logic is the same in result :P
    if NOT (9 OR 10) equals if NOT 9 OR NOT 10
    LVL 107

    Expert Comment

    by:Ray Paseur
    The "or" condition is evaluated from left to right in this sequence.  So if you give it 10, the if() statement will be satisfied that 10 is not 9, and no further tests will be done.  That is why you want the && instead of the || here.

    Author Closing Comment

    thank you

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    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 …

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now