If a string is not matching one of 3 option in PHP












0















This is the code I used to have to check if $A doesn't match $B



if($A!=$B) {
$set = array();
echo $val= str_replace('\/', '/', json_encode($set));
//echo print_r($_SERVER);
exit;
}


Now I need the opposite of this condition: ($A need to match one of these $B,$C or $D)










share|improve this question





























    0















    This is the code I used to have to check if $A doesn't match $B



    if($A!=$B) {
    $set = array();
    echo $val= str_replace('\/', '/', json_encode($set));
    //echo print_r($_SERVER);
    exit;
    }


    Now I need the opposite of this condition: ($A need to match one of these $B,$C or $D)










    share|improve this question



























      0












      0








      0








      This is the code I used to have to check if $A doesn't match $B



      if($A!=$B) {
      $set = array();
      echo $val= str_replace('\/', '/', json_encode($set));
      //echo print_r($_SERVER);
      exit;
      }


      Now I need the opposite of this condition: ($A need to match one of these $B,$C or $D)










      share|improve this question
















      This is the code I used to have to check if $A doesn't match $B



      if($A!=$B) {
      $set = array();
      echo $val= str_replace('\/', '/', json_encode($set));
      //echo print_r($_SERVER);
      exit;
      }


      Now I need the opposite of this condition: ($A need to match one of these $B,$C or $D)







      php arrays string-comparison






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 0:16









      Funk Forty Niner

      1




      1










      asked Dec 31 '18 at 22:53









      daviddavid

      33




      33
























          2 Answers
          2






          active

          oldest

          votes


















          9














          A simple shortcut to seeing if a value matches one of multiple values you can put the values to be compared against ($B, $C, and $D) into an array and then use in_array() to see if the original value ($A) matches any of them.



          if (in_array($A, [$B, $C, $D])) {
          // ...
          }


          If you don't want it to match any of $B, $C, or $D just use !:



          if (!in_array($A, [$B, $C, $D])) {
          // ...
          }





          share|improve this answer
























          • Genious! Nice work!

            – Patrick Simard
            Jan 1 at 0:16



















          2














          You can use array_search



          $B = 'B';
          $C = 'C';
          $D = 'D';

          //match B
          $A = 'B';

          $options = [$B, $C, $D];

          if (false !== ($index = array_search($A, $options ))) {
          echo "Match: {$index} '{$options[$index]}'";
          }


          Output



          Match: 0 'B'


          Sandbox



          The nice thing here is you can set the $index and use that to tell which one matched later.



          Note you have to use false !== because array search returns the index where the match happened at, so it can happen on the first array element which is index 0. As we know PHP can treat 0 as false (in this case the condition would fail when it should pass). However, when we use the strict type check PHP also compares the type and INT 0 is not BOOL false (which passes the condition).



          for reference.



          http://php.net/manual/en/function.array-search.php



          Another probably the most efficient way is to use isset, and use keys instead of values:



          $options = [$B=>1,$C=>1,$D=>1]; //values don't matter
          if(!isset($options[$A])){
          //....
          }





          share|improve this answer


























          • Why would the index matter if you are building the array solely for the purpose of matching the value against multiple operands?

            – Dharman
            Jan 1 at 0:27











          • This may not be exactly what the OP is looking for, but it works and is handy to know

            – John Conde
            Jan 1 at 0:32






          • 2





            @Dharman Who knows? It was just another way to solve the same problem. You could even do array_flip (or build it with keys) and then do isset($array[$A]) if you really wanted to, or even array_intersect etc..

            – ArtisticPhoenix
            Jan 1 at 0:33













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991977%2fif-a-string-is-not-matching-one-of-3-option-in-php%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          9














          A simple shortcut to seeing if a value matches one of multiple values you can put the values to be compared against ($B, $C, and $D) into an array and then use in_array() to see if the original value ($A) matches any of them.



          if (in_array($A, [$B, $C, $D])) {
          // ...
          }


          If you don't want it to match any of $B, $C, or $D just use !:



          if (!in_array($A, [$B, $C, $D])) {
          // ...
          }





          share|improve this answer
























          • Genious! Nice work!

            – Patrick Simard
            Jan 1 at 0:16
















          9














          A simple shortcut to seeing if a value matches one of multiple values you can put the values to be compared against ($B, $C, and $D) into an array and then use in_array() to see if the original value ($A) matches any of them.



          if (in_array($A, [$B, $C, $D])) {
          // ...
          }


          If you don't want it to match any of $B, $C, or $D just use !:



          if (!in_array($A, [$B, $C, $D])) {
          // ...
          }





          share|improve this answer
























          • Genious! Nice work!

            – Patrick Simard
            Jan 1 at 0:16














          9












          9








          9







          A simple shortcut to seeing if a value matches one of multiple values you can put the values to be compared against ($B, $C, and $D) into an array and then use in_array() to see if the original value ($A) matches any of them.



          if (in_array($A, [$B, $C, $D])) {
          // ...
          }


          If you don't want it to match any of $B, $C, or $D just use !:



          if (!in_array($A, [$B, $C, $D])) {
          // ...
          }





          share|improve this answer













          A simple shortcut to seeing if a value matches one of multiple values you can put the values to be compared against ($B, $C, and $D) into an array and then use in_array() to see if the original value ($A) matches any of them.



          if (in_array($A, [$B, $C, $D])) {
          // ...
          }


          If you don't want it to match any of $B, $C, or $D just use !:



          if (!in_array($A, [$B, $C, $D])) {
          // ...
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 31 '18 at 22:55









          John CondeJohn Conde

          185k80372423




          185k80372423













          • Genious! Nice work!

            – Patrick Simard
            Jan 1 at 0:16



















          • Genious! Nice work!

            – Patrick Simard
            Jan 1 at 0:16

















          Genious! Nice work!

          – Patrick Simard
          Jan 1 at 0:16





          Genious! Nice work!

          – Patrick Simard
          Jan 1 at 0:16













          2














          You can use array_search



          $B = 'B';
          $C = 'C';
          $D = 'D';

          //match B
          $A = 'B';

          $options = [$B, $C, $D];

          if (false !== ($index = array_search($A, $options ))) {
          echo "Match: {$index} '{$options[$index]}'";
          }


          Output



          Match: 0 'B'


          Sandbox



          The nice thing here is you can set the $index and use that to tell which one matched later.



          Note you have to use false !== because array search returns the index where the match happened at, so it can happen on the first array element which is index 0. As we know PHP can treat 0 as false (in this case the condition would fail when it should pass). However, when we use the strict type check PHP also compares the type and INT 0 is not BOOL false (which passes the condition).



          for reference.



          http://php.net/manual/en/function.array-search.php



          Another probably the most efficient way is to use isset, and use keys instead of values:



          $options = [$B=>1,$C=>1,$D=>1]; //values don't matter
          if(!isset($options[$A])){
          //....
          }





          share|improve this answer


























          • Why would the index matter if you are building the array solely for the purpose of matching the value against multiple operands?

            – Dharman
            Jan 1 at 0:27











          • This may not be exactly what the OP is looking for, but it works and is handy to know

            – John Conde
            Jan 1 at 0:32






          • 2





            @Dharman Who knows? It was just another way to solve the same problem. You could even do array_flip (or build it with keys) and then do isset($array[$A]) if you really wanted to, or even array_intersect etc..

            – ArtisticPhoenix
            Jan 1 at 0:33


















          2














          You can use array_search



          $B = 'B';
          $C = 'C';
          $D = 'D';

          //match B
          $A = 'B';

          $options = [$B, $C, $D];

          if (false !== ($index = array_search($A, $options ))) {
          echo "Match: {$index} '{$options[$index]}'";
          }


          Output



          Match: 0 'B'


          Sandbox



          The nice thing here is you can set the $index and use that to tell which one matched later.



          Note you have to use false !== because array search returns the index where the match happened at, so it can happen on the first array element which is index 0. As we know PHP can treat 0 as false (in this case the condition would fail when it should pass). However, when we use the strict type check PHP also compares the type and INT 0 is not BOOL false (which passes the condition).



          for reference.



          http://php.net/manual/en/function.array-search.php



          Another probably the most efficient way is to use isset, and use keys instead of values:



          $options = [$B=>1,$C=>1,$D=>1]; //values don't matter
          if(!isset($options[$A])){
          //....
          }





          share|improve this answer


























          • Why would the index matter if you are building the array solely for the purpose of matching the value against multiple operands?

            – Dharman
            Jan 1 at 0:27











          • This may not be exactly what the OP is looking for, but it works and is handy to know

            – John Conde
            Jan 1 at 0:32






          • 2





            @Dharman Who knows? It was just another way to solve the same problem. You could even do array_flip (or build it with keys) and then do isset($array[$A]) if you really wanted to, or even array_intersect etc..

            – ArtisticPhoenix
            Jan 1 at 0:33
















          2












          2








          2







          You can use array_search



          $B = 'B';
          $C = 'C';
          $D = 'D';

          //match B
          $A = 'B';

          $options = [$B, $C, $D];

          if (false !== ($index = array_search($A, $options ))) {
          echo "Match: {$index} '{$options[$index]}'";
          }


          Output



          Match: 0 'B'


          Sandbox



          The nice thing here is you can set the $index and use that to tell which one matched later.



          Note you have to use false !== because array search returns the index where the match happened at, so it can happen on the first array element which is index 0. As we know PHP can treat 0 as false (in this case the condition would fail when it should pass). However, when we use the strict type check PHP also compares the type and INT 0 is not BOOL false (which passes the condition).



          for reference.



          http://php.net/manual/en/function.array-search.php



          Another probably the most efficient way is to use isset, and use keys instead of values:



          $options = [$B=>1,$C=>1,$D=>1]; //values don't matter
          if(!isset($options[$A])){
          //....
          }





          share|improve this answer















          You can use array_search



          $B = 'B';
          $C = 'C';
          $D = 'D';

          //match B
          $A = 'B';

          $options = [$B, $C, $D];

          if (false !== ($index = array_search($A, $options ))) {
          echo "Match: {$index} '{$options[$index]}'";
          }


          Output



          Match: 0 'B'


          Sandbox



          The nice thing here is you can set the $index and use that to tell which one matched later.



          Note you have to use false !== because array search returns the index where the match happened at, so it can happen on the first array element which is index 0. As we know PHP can treat 0 as false (in this case the condition would fail when it should pass). However, when we use the strict type check PHP also compares the type and INT 0 is not BOOL false (which passes the condition).



          for reference.



          http://php.net/manual/en/function.array-search.php



          Another probably the most efficient way is to use isset, and use keys instead of values:



          $options = [$B=>1,$C=>1,$D=>1]; //values don't matter
          if(!isset($options[$A])){
          //....
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 1 at 0:37

























          answered Jan 1 at 0:16









          ArtisticPhoenixArtisticPhoenix

          15.8k11223




          15.8k11223













          • Why would the index matter if you are building the array solely for the purpose of matching the value against multiple operands?

            – Dharman
            Jan 1 at 0:27











          • This may not be exactly what the OP is looking for, but it works and is handy to know

            – John Conde
            Jan 1 at 0:32






          • 2





            @Dharman Who knows? It was just another way to solve the same problem. You could even do array_flip (or build it with keys) and then do isset($array[$A]) if you really wanted to, or even array_intersect etc..

            – ArtisticPhoenix
            Jan 1 at 0:33





















          • Why would the index matter if you are building the array solely for the purpose of matching the value against multiple operands?

            – Dharman
            Jan 1 at 0:27











          • This may not be exactly what the OP is looking for, but it works and is handy to know

            – John Conde
            Jan 1 at 0:32






          • 2





            @Dharman Who knows? It was just another way to solve the same problem. You could even do array_flip (or build it with keys) and then do isset($array[$A]) if you really wanted to, or even array_intersect etc..

            – ArtisticPhoenix
            Jan 1 at 0:33



















          Why would the index matter if you are building the array solely for the purpose of matching the value against multiple operands?

          – Dharman
          Jan 1 at 0:27





          Why would the index matter if you are building the array solely for the purpose of matching the value against multiple operands?

          – Dharman
          Jan 1 at 0:27













          This may not be exactly what the OP is looking for, but it works and is handy to know

          – John Conde
          Jan 1 at 0:32





          This may not be exactly what the OP is looking for, but it works and is handy to know

          – John Conde
          Jan 1 at 0:32




          2




          2





          @Dharman Who knows? It was just another way to solve the same problem. You could even do array_flip (or build it with keys) and then do isset($array[$A]) if you really wanted to, or even array_intersect etc..

          – ArtisticPhoenix
          Jan 1 at 0:33







          @Dharman Who knows? It was just another way to solve the same problem. You could even do array_flip (or build it with keys) and then do isset($array[$A]) if you really wanted to, or even array_intersect etc..

          – ArtisticPhoenix
          Jan 1 at 0:33




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991977%2fif-a-string-is-not-matching-one-of-3-option-in-php%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'