Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    60
    Thanks
    1
    Thanked 0 Times in 0 Posts

    writing a IF statement inside a array define

    $array13 [] = if($row_users['cat'] != "Main"){echo '-';} $row_users['message'];

    so it hates the if statment, it works without the if statement, how do I make it work?

  • #2
    New to the CF scene
    Join Date
    Jan 2012
    Location
    PL
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this:

    PHP Code:
    $array13 [] = ($row_users['cat'] != "Main")?'-':$row_users['message']; 

  • #3
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    but without the ()
    PHP Code:
    $array13[] = $row_users['cat'] != "Main" '-' $row_users['message']; 
    although it may work with them... idk

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    () are irrelevant in the evaluation block for ternaries. I typically use them since if you go more than 2 levels in depth without them its horrendously complex to read. Ie:
    PHP Code:
    $array[] = $row_users['cat'] != "Main" $row_users['cat'] != "Category" $row_users['cat'] != "Test" 'Not one of these' 'Is test' 'Is category' 'Is Main'
    Not cool lol.


  •  

    Posting Permissions

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