0

The second if statement in the series below keeps returning true and printing out even if estimateId is not the only item in the array. How do I correct this?

The goal is to make sure only one if statement matches at all times in the series below.

For example, right now the second if returns true when the 3rd if does. This should not happen.

https://localhost/data.php?estimateId=1001
https://localhost/data.php?estimateId=1001&proposalsFilter=all

.

<?php 

    # estimates/active/
    if (in_array($_GET["estimatesFilter"], $_GET)) {

        print_r('estimatesFilter: ' . $_GET["estimatesFilter"] . ' url parameter(s) was passed.');
      print_r("<br>");

    }

    # This if keeps returning true even if estimateId is not the only item in the array?
    # estimates/1001/
    if (in_array($_GET["estimateId"], $_GET)) {

        print_r('estimateId: ' . $_GET["estimateId"] . ' url parameter(s) was passed.');
        print_r("<br>");

    }

    # estimates/1001/proposals/
    if (in_array($_GET["estimateId"], $_GET) && in_array($_GET["proposalsFilter"], $_GET)) {

        print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'proposalsFilter: ' . $_GET["proposalsFilter"] . ' url parameter(s) was passed.');
        print_r("<br>");

    }

    # estimates/1001/proposals/3001/
    if (in_array($_GET["estimateId"], $_GET) && in_array($_GET["proposalId"], $_GET)) {

        print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'proposalId: ' . $_GET["proposalId"] . ' url parameter(s) was passed.');
        print_r("<br>");

    }

    # estimates/1001/contracts/
    if (in_array($_GET["estimateId"], $_GET) && in_array($_GET["contractsFilter"], $_GET)) {

        print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'contractsFilter: ' . $_GET["contractsFilter"] . ' url parameter(s) was passed.');
        print_r("<br>");

    }

    # estimates/1001/contracts/3001/
    if (in_array($_GET["estimateId"], $_GET) && in_array($_GET["contractId"], $_GET)) {

        print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'contractId: ' . $_GET["contractId"] . ' url parameter(s) was passed.');
        print_r("<br>");

    }

    # estimates/1001/invoices/
    if (in_array($_GET["estimateId"], $_GET) && in_array($_GET["invoicesFilter"], $_GET)) {

        print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'invoicesFilter: ' . $_GET["invoicesFilter"] . ' url parameter(s) was passed.');
        print_r("<br>");

    }

    # estimates/1001/invoices/4001/
    if (in_array($_GET["estimateId"], $_GET) && in_array($_GET["invoiceId"], $_GET)) {

        print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'invoiceId: ' . $_GET["invoiceId"] . ' url parameter(s) was passed.');
        print_r("<br>");

    }

 ?>
suchislife
  • 4,251
  • 10
  • 47
  • 78

2 Answers2

2

# This if keeps returning true even if estimateId is not the only item in the array?
# estimates/1001/

To solve your issue you can check if that value is the only value

if(in_array($_GET["estimateId"], $_GET))

becomes:

if(\count($_GET) === 1 && \in_array($_GET["estimateId"], $_GET) ){

  /***
    this will only trigger if `$_GET["estimateId"]` is in the 
    array $_GET and is the only value in the array.
   ***/
 }

Which checks the values and also checks a count of values in the array. Because you are looking for just this value, the array count should only be 1.

https://localhost/data.php?estimateId=1001

Above returns true because $_GET contains only one value.

https://localhost/data.php?estimateId=1001&proposalsFilter=all

Above returns false because $_GET array contains 2 values.

Martin
  • 22,212
  • 11
  • 70
  • 132
-1

"The goal is to make sure only one if statement matches at all times in the series below."

Simply use elseif instead of if then.

<?php 

    # estimates/active/
    if (in_array($_GET["estimatesFilter"], $_GET)) {

        print_r('estimatesFilter: ' . $_GET["estimatesFilter"] . ' url parameter(s) was passed.');
      print_r("<br>");

    }

    # This if keeps returning true even if estimateId is not the only item in the array?
    # estimates/1001/
    elseif (in_array($_GET["estimateId"], $_GET)) {

        print_r('estimateId: ' . $_GET["estimateId"] . ' url parameter(s) was passed.');
        print_r("<br>");

    }

    # estimates/1001/proposals/
    elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["proposalsFilter"], $_GET)) {

        print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'proposalsFilter: ' . $_GET["proposalsFilter"] . ' url parameter(s) was passed.');
        print_r("<br>");

    }

    # estimates/1001/proposals/3001/
    elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["proposalId"], $_GET)) {

        print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'proposalId: ' . $_GET["proposalId"] . ' url parameter(s) was passed.');
        print_r("<br>");

    }

    # estimates/1001/contracts/
    elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["contractsFilter"], $_GET)) {

        print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'contractsFilter: ' . $_GET["contractsFilter"] . ' url parameter(s) was passed.');
        print_r("<br>");

    }

    # estimates/1001/contracts/3001/
    elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["contractId"], $_GET)) {

        print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'contractId: ' . $_GET["contractId"] . ' url parameter(s) was passed.');
        print_r("<br>");

    }

    # estimates/1001/invoices/
    elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["invoicesFilter"], $_GET)) {

        print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'invoicesFilter: ' . $_GET["invoicesFilter"] . ' url parameter(s) was passed.');
        print_r("<br>");

    }

    # estimates/1001/invoices/4001/
    elseif (in_array($_GET["estimateId"], $_GET) && in_array($_GET["invoiceId"], $_GET)) {

        print_r('estimateId: ' . $_GET["estimateId"] . ' & ' . 'invoiceId: ' . $_GET["invoiceId"] . ' url parameter(s) was passed.');
        print_r("<br>");

    }

 ?>
Bär Ger
  • 62
  • 5
  • 1
    this does not work because `in_array` does not check if a value is the ***only*** value in any given array – Martin Mar 01 '20 at 20:47