www.webdeveloper.com
Results 1 to 6 of 6

Thread: How to make "0" return as true?

  1. #1
    Join Date
    Jul 2008
    Location
    Bristol
    Posts
    76

    How to make "0" return as true?

    Hi fellow coders.

    Looking for a spot of advice here, hope someone can give me a hand.

    I'm currently in the process of programming a shopping cart class for a site on which customers can purchase tickets for events. There are two main functions in the class: add() and modify() both of which affect the cart contents. If someone adds a new ticket to the cart it uses add() but if that ticket already is in the basket it uses modify() to update the quantity.

    I have an exists() function to check if a cart exists first before processing either function.

    PHP Code:
    $cartid=$cart->exists($theitemid); 
    This will return the id of the cart, starting at 0 (it will be an array).

    PHP Code:
    if ($cartid){
        
    $cart->modify($cartid,$theitemid,$quantity);
    }else{
        
    $cart->add($theitemid,$theitemdescription,$quantity,$price);

    The above code will either add to or modify the cart depending on whether or not that item already exists. Because, say, someone might decide they want more after adding the first one.

    HOWEVER....

    If the $cartid is 0 (which means it's the first item in the cart) then the if clause "if ($cartid)" returns as 0 but I need it to recognise that as TRUE because that means it DOES EXIST in the cart (otherwise it would simply return nothing). I've also tried if($cartid!="") but the same thing happens: it implements add() because it interprets "0" as false.

    Is there something I can do to prevent 0 coming back as false in this instance?

    Any help appreciated, thanks muchly
    Mike

  2. #2
    Join Date
    Nov 2008
    Posts
    2,477
    You need to use the strict comparison operator (=== or !==):

    PHP Code:
    if (== false) {
        
    // this will run
    }
    if (
    === false) {
        
    // this code will never run!

    This adds an additional check to the expression based on the types of the values being compared. Since 0 is an integer and false is boolean, the values are not of the same type and so will not evaluate true when compared.
    The first rule of Tautology Club is the first rule of Tautology Club.

  3. #3
    Join Date
    Mar 2010
    Posts
    2,803
    It's a little confusing because in my own php shopping cart I also have an exists() method in the class which accepts an input product_id.

    My exists() method then returns true or false (1 or 0) depending on whether the product_id already exists in the cart. Product_id is a unique value in the products db table.

    I would have thought that, assuming your $theitemid is also a unique value, then your exists() method only needs to return a value of true or false as well.

    Am I misunderstanding something? because I don't understand why $cartid is an array

  4. #4
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,755

  5. #5
    Join Date
    Apr 2010
    Posts
    227
    Well

    Your code is your own but to me its a tad bizaare.

    Surely your only creating the shopping cart object when the customer adds an item?
    Wouldnt you therefor simply be doing a
    Code:
    if( isset($_SESSION['shoppingcart'] ))
    {
    //modify
    }
    else
    {
    $_SESSION['shoppingcart'] = new cart();
    $_SESSION['shoppingcart']->add($theitemid,$theitemdescription,$quantity,$price); 
    }
    ?
    (i store all objects in sessions since I always use AJAX)
    Last edited by Belrick; 04-18-2010 at 04:52 AM.

  6. #6
    Join Date
    Jul 2008
    Location
    Bristol
    Posts
    76
    Thanks Mindzai, that's exactly what I needed!

    Quote Originally Posted by Mindzai View Post
    You need to use the strict comparison operator (=== or !==):

    PHP Code:
    if (== false) {
        
    // this will run
    }
    if (
    === false) {
        
    // this code will never run!

    This adds an additional check to the expression based on the types of the values being compared. Since 0 is an integer and false is boolean, the values are not of the same type and so will not evaluate true when compared.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles