How to perform conditional?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • brett

    #1

    How to perform conditional?

    I'd like to check these two conditions:

    if ((is_page() or ($category->cat_ID === $cat)){
    do something
    }

    but keep getting an "unexpected {..." error. If I do this, it works
    fine:

    if ($category->cat_ID === $cat){
    do something
    }

    I'm guessing the array access has something to do with it. Any
    suggestions?

    Thanks,
    Brett

  • MsKitty

    #2
    Re: How to perform conditional?

    Hi Brett -
    You have unbalanced parentheses - one extra one on the left = try
    using fewer ones so it is easier to see:
    if (is_page() || $category->cat_ID === $cat) {
    do something
    }

    Kitty
    San Diego web design, development, training, and programming - we set up membership sites, Blogs, Ecommerce, Dynamic Content, and easily maintained web sites


    brett wrote:
    I'd like to check these two conditions:
    >
    if ((is_page() or ($category->cat_ID === $cat)){
    do something
    }
    >
    but keep getting an "unexpected {..." error. If I do this, it works
    fine:
    >

    Comment

    • brett

      #3
      Re: How to perform conditional?

      You have unbalanced parentheses

      Ah! Thanks.

      Comment

      • Erwin Moller

        #4
        Re: How to perform conditional?

        MsKitty wrote:
        Hi Brett -
        You have unbalanced parentheses - one extra one on the left = try
        using fewer ones so it is easier to see:
        if (is_page() || $category->cat_ID === $cat) {
        do something
        }

        Correct, but advising to drop parentheses is not a good advise.
        Use it everywhere, and use it right.
        Dropping parentheses leads to possible confusion because tests can be
        performed in a order not ment by the programmer.

        I prefer this notation above all shorter ones:

        if ((is_page()) || ($category->cat_ID === $cat) ) {
        do something
        }

        That way you don't have to worry about precedence of the used terms.
        eg: Is || or === evaluated first?

        just my 2 cent...

        Regards,
        Erwin Moller
        >
        Kitty
        San Diego web design, development, training, and programming - we set up membership sites, Blogs, Ecommerce, Dynamic Content, and easily maintained web sites

        >
        brett wrote:
        >I'd like to check these two conditions:
        >>
        >if ((is_page() or ($category->cat_ID === $cat)){
        >do something
        >}
        >>
        >but keep getting an "unexpected {..." error. If I do this, it works
        >fine:
        >>

        Comment

        • brett

          #5
          Re: How to perform conditional?

          if ((is_page()) || ($category->cat_ID === $cat) ) {

          What is the notation for equivalency? Is it "==" or "==="?

          Thanks,
          Brett

          Comment

          • Erwin Moller

            #6
            Re: How to perform conditional?

            brett wrote:
            >if ((is_page()) || ($category->cat_ID === $cat) ) {
            >
            What is the notation for equivalency? Is it "==" or "==="?
            == is more loose than ===

            === also checks if the data compared is of the same type.
            eg

            $a = 1;
            $b = "1";
            if ($a == $b){
            // evaluates to true
            }

            if ($a === $b){
            // evaluates to false
            }

            This can be a very important extra = in some situations. :-)

            A common situation where === makes sense is a functioncall that can return 0
            as valid answer, eg:

            // This function returns the number of elements in an array
            // or it returns false if not an array is passed
            function countNumber($so meArray){
            if is_array($someA rray){
            return count($someArra y);
            } else {
            return false;
            }
            }

            Now if you use this function and feed the following 3 variables to it:
            $test = array("bla",3,2 4);
            $result = countNumber($te st);
            if ($result){
            echo "number of elements $result";
            } else {
            echo "Not an array!";
            }

            That works ok, it returns 3.

            suppose $test was:
            $test = 23;

            Which is also ok, it would respond that $test is not an array.

            Now the problem:
            $test = array();

            If you feed this, PHP will correctly count 0 elements, and thus return 0.

            However, when used as an expression, 0 evaluates to false, thus PHP will
            incorrectly say it was not an array.

            If you use the following construct, this will NOT happen.

            if ($result === true){
            echo "number of elements $result";
            } else {
            echo "Not an array!";
            }


            Regards,
            Erwin Moller

            >
            Thanks,
            Brett

            Comment

            Working...