www.webdeveloper.com
Results 1 to 7 of 7

Thread: Is there a simpler way to write this?

  1. #1
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739

    Is there a simpler way to write this?

    PHP Code:
    # enter the clause only if more than one of the following is negative. And they are all string values.
    if(false === 
        (
            (
    $uncommittedDiseqcCommand == "0" and $commitedDiseqcCommand == "none") or 
            (
    $toneburst == "none" and $commitedDiseqcCommand == "none") or 
            (
    $uncommittedDiseqcCommand == "0" and $toneburst == "none")
        )
    )
    {
        ...


  2. #2
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    check for negatives, i.e. "0" and "none"?
    PHP Code:
    if(($uncommittedDiseqcCommand === '0' || $uncommittedDiseqcCommand === 'none') || $toneburst === 'none') {
        
    // ...

    Note: "AND" and "OR", and "&&" and "||" behave differently.

  3. #3
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,246
    Not that I really like this, but I figured I'd post it here anyway in case it gives you a different way to think about it, at least:
    PHP Code:
    if(array_sum(array(
        (
    $uncommittedDiseqcCommand == "0") ? 0,
        (
    $toneburst == "none") ? 0,
        (
    $commitedDiseqcCommand == "none") ? 0
    )) < 2)
    {
        
    //

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Thanks for the idea Nogdog. Not much simpler for 3 compared to mine but for 4 or more would be much simpler. Also I don't think you need the array stuff. Something like this...
    PHP Code:
    if(
        (((
    $uncommittedDiseqcCommand == "0") ? 0) +
        ((
    $toneburst == "none") ? 0) +
        ((
    $commitedDiseqcCommand == "none") ? 0))
        < 
    2)
    {
        
    //


  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,246
    See, I knew you could make something useful out of it. Probably more efficient without building the array, too.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    I didn't notice the $commitedDiseqcCommand.
    this works:
    PHP Code:
    if (!(($commitedDiseqcCommand === 'none' && $uncommittedDiseqcCommand === '0') || $toneburst === 'none')) {
        
    // ...

    tests:
    PHP Code:
    <?php
    function test1($commitedDiseqcCommand$uncommittedDiseqcCommand$toneburst)
    {
        return !((
    $commitedDiseqcCommand === 'none' && $uncommittedDiseqcCommand === '0') || $toneburst === 'none');
    }
    function 
    test2($commitedDiseqcCommand$uncommittedDiseqcCommand$toneburst)
    {
        return ((((
    $uncommittedDiseqcCommand == "0") ? 0) + (($toneburst == "none") ? 0) + (($commitedDiseqcCommand == "none") ? 0)) < 2);
    }
    function 
    test3($commitedDiseqcCommand$uncommittedDiseqcCommand$toneburst)
    {
        return (
    false === 
        (
            (
    $uncommittedDiseqcCommand == "0" and $commitedDiseqcCommand == "none") or 
            (
    $toneburst == "none" and $commitedDiseqcCommand == "none") or 
            (
    $uncommittedDiseqcCommand == "0" and $toneburst == "none")
        )
    );
    }
    function 
    x($a$b$c)
    {
        echo 
    '<p>test 1: ', (test1($a$b$c) ? 'true' 'false'), '<br>';
        echo 
    'test 2: ', (test2($a$b$c) ? 'true' 'false'), '<br>';
        echo 
    'test 3: ', (test3($a$b$c) ? 'true' 'false'), '</p>';
    }
    x('none''0''none');
    x('none''1''none');
    x('none''0''something');
    x('something''0''none');
    x('something''1''something');
    x('something''0''something');
    ?>
    Last edited by ShrineDesigns; 05-01-2014 at 07:01 PM.

Thread Information

Users Browsing this Thread

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

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