Newbie needs help with OR php

by hometutor 15 replies
I'm new to php and cannot get this to work. It works with a single strpos but not with || between them. I researched and cannot find the right format

Code:
if (strpos(,)) || (strpos(,))

{

echo ;

fwrite(,); 

  fwrite(,);  

}
Huh the forum striped out my variables, but I think you get the idea
#programming #newbie #php
Avatar of Unregistered
  • Profile picture of the author Big Squid
    Cannot tell without the variables included but it should work:

    IF( strpos($haystack, $needle) || strpos($haystack, $needle) ) {
    // it is found so do something
    }
    {{ DiscussionBoard.errors[10132519].message }}
  • Profile picture of the author David Beroff
    Notice the subtle difference between the use of parenthesis in the first line of each of the two posts. You don't need parens around the individual strpos() calls, but you certainly do need a pair around the entire test. I'm not even sure that the OP's first line would compile correctly, much less run. Try using the structure posed by Big Squid.
    Signature
    Put MY voice on YOUR video: AwesomeAmericanAudio.com
    {{ DiscussionBoard.errors[10132928].message }}
    • Profile picture of the author hometutor
      Originally Posted by David Beroff View Post

      Notice the subtle difference between the use of parenthesis in the first line of each of the two posts. You don't need parens around the individual strpos() calls, but you certainly do need a pair around the entire test. I'm not even sure that the OP's first line would compile correctly, much less run. Try using the structure posed by Big Squid.
      The parentheses continue to elude some logic to me, but I know they must be equal (( ))

      so I'm working on their logic. I got the problem solved and would like to thank everyone.

      Rick
      {{ DiscussionBoard.errors[10146255].message }}
      • Profile picture of the author David Beroff
        Originally Posted by hometutor View Post

        The parentheses continue to elude some logic to me, but I know they must be equal (( ))
        Sure, they must be equal, but that's not enough. Think of it this way: the outer parens are part of the if statement itself, so your original example only had the if testing the first half, before the || operator.

        Taking another look at the documentation for strpos(), there's a big, red warning saying that you can run into issues if you use the return from strpos() as a boolean value, (which is exactly what you are doing with the || operator). Since half your code was stripped out, it's hard to tell what you are trying to do, so... what is it that you are trying to do?
        Signature
        Put MY voice on YOUR video: AwesomeAmericanAudio.com
        {{ DiscussionBoard.errors[10146356].message }}
        • Profile picture of the author hometutor
          Originally Posted by David Beroff View Post

          Sure, they must be equal, but that's not enough. Think of it this way: the outer parens are part of the if statement itself, so your original example only had the if testing the first half, before the || operator.

          Taking another look at the documentation for strpos(), there's a big, red warning saying that you can run into issues if you use the return from strpos() as a boolean value, (which is exactly what you are doing with the || operator). Since half your code was stripped out, it's hard to tell what you are trying to do, so... what is it that you are trying to do?
          Let me try putting in the code again

          Code:
          
          ///////////////////// Checking for keyword filters and duplicates /////////////////////////////////////////////////
          
          if (stripos(,)  || stripos(,) && stripos(,) === false)
          
          ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

          Grrrr erased my code again. Let me try without the code brackets

          ///////////////////// Checking for keyword filters and duplicates /////////////////////////////////////////////////

          if (stripos($description,$keyword1) || stripos($description,$keyword2) && stripos($checkdups,$title) === false)

          ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
          Thank you for your help. This version seems to be working

          Rick
          {{ DiscussionBoard.errors[10147826].message }}
  • Profile picture of the author Adrianhenry
    Originally Posted by David Beroff View Post

    Notice the subtle difference between the use of parenthesis in the first line of each of the two posts. You don't need parens around the individual strpos() calls, but you certainly do need a pair around the entire test. I'm not even sure that the OP's first line would compile correctly, much less run. Try using the structure posed by Big Squid.
    David has it spot on here.

    The bolded red brackets below are not needed. Remove them and the code should run fine.

    Originally Posted by hometutor View Post

    I'm new to php and cannot get this to work. It works with a single strpos but not with || between them. I researched and cannot find the right format

    Code:
    if (strpos(,)   ) || (   strpos(,))
    
    {
    
    echo ;
    
    fwrite(,); 
    
      fwrite(,);  
    
    }
    Huh the forum striped out my variables, but I think you get the idea
    {{ DiscussionBoard.errors[10133093].message }}
  • Profile picture of the author David Beroff
    Could I ask you to please describe your intent in English, without symbols? Sorry to belabor the issue, but, (even with the full code showing), this still doesn't really communicate what it is you are trying to do. Thanks.
    Signature
    Put MY voice on YOUR video: AwesomeAmericanAudio.com
    {{ DiscussionBoard.errors[10147842].message }}
    • Profile picture of the author hometutor
      Originally Posted by David Beroff View Post

      Could I ask you to please describe your intent in English, without symbols? Sorry to belabor the issue, but, (even with the full code showing), this still doesn't really communicate what it is you are trying to do. Thanks.
      I understand, my genius friend whose helping me to learn this says I'm a slob.

      This is a Clickbank rss feed and page maker. This section is filtering for two keywords and checking for duplicates.

      Hope that helps.

      Rick
      {{ DiscussionBoard.errors[10147923].message }}
      • Profile picture of the author David Beroff
        Originally Posted by hometutor View Post

        I understand, my genius friend whose helping me to learn this says I'm a slob.

        This is a Clickbank rss feed and page maker. This section is filtering for two keywords and checking for duplicates.
        Thank you. (I'm not making any judgements.)

        I'll be the first to admit that the nature of these functions is a bit counter-intuitive when you're simply trying to determine "does this keyword occur?", as their main intent is to determine "where does this keyword occur?".

        So, two suggestions:

        1. To correctly address that red warning, ("may also return a non-Boolean value which evaluates to FALSE"), I'd test each instance against FALSE, as the documentation (obliquely) recommends. Otherwise, you're risking special-case bugs when the haystack strings start with the needle strings.

        2. To avoid relying on the PHP order-of-precedence rules, (which state whether the || or the && is evaluated first), I'd recommend an extra set of parens. The idea is that it's always safer to simply force your intended evaluation order, rather than risk a potential error, (by you or by someone in the future).

        Thus:

        Code:
        if ((stripos(description,keyword1) !== false  || 
             stripos(description,keyword2) !== false)
            && stripos(checkdups,title) === false) {
           ...
        }
        Signature
        Put MY voice on YOUR video: AwesomeAmericanAudio.com
        {{ DiscussionBoard.errors[10148224].message }}
        • Profile picture of the author hometutor
          So how about this one

          I can't get the parentheses right. It's the last one giving me a hard time


          if ((stripos($description,$keyword1) !== false || stripos($description,$keyword2) !== false) && (stripos($checkdups,$title) === false) && stripos($exclude1,$description) === false) && (stripos($exclude2,$description) === false))

          Thanks

          Rick
          {{ DiscussionBoard.errors[10151891].message }}
          • Profile picture of the author David Beroff
            Those actually seem to be ok. What seems to be the problem?
            Signature
            Put MY voice on YOUR video: AwesomeAmericanAudio.com
            {{ DiscussionBoard.errors[10151922].message }}
            • Profile picture of the author hometutor
              Originally Posted by David Beroff View Post

              Those actually seem to be ok. What seems to be the problem?
              if ((stripos($description,$keyword1) !== false || stripos($description,$keyword2) !== false) && (stripos($checkdups,$title) === false) && stripos($exclude1,$description) === false) && (stripos($exclude2,$description) === false))




              {


              }


              echo 'OK'

              This way I get no OK

              When I hash the last && out i get the echoed OK


              if ((stripos($description,$keyword1) !== false || stripos($description,$keyword2) !== false) && (stripos($checkdups,$title) === false) && stripos($exclude1,$description) === false)

              //&& (stripos($exclude2,$description) === false))
              {{ DiscussionBoard.errors[10152339].message }}
              • Profile picture of the author David Beroff
                Looks like your parens are still not balanced. Sorry I didn't see that before. The fourth stripos call has a closing paren without a corresponding opening paren. (Frankly, neither is needed, but they can't hurt, either.)

                I strongly recommend that you get an editor like Notepad++, or something similar, which will let you highlight and match parens far more easily.
                Signature
                Put MY voice on YOUR video: AwesomeAmericanAudio.com
                {{ DiscussionBoard.errors[10154802].message }}
                • Profile picture of the author hometutor
                  Originally Posted by David Beroff View Post

                  Looks like your parens are still not balanced. Sorry I didn't see that before. The fourth stripos call has a closing paren without a corresponding opening paren. (Frankly, neither is needed, but they can't hurt, either.)

                  I strongly recommend that you get an editor like Notepad++, or something similar, which will let you highlight and match parens far more easily.
                  Thanks Dave I have started looking for one. The syntax stuff must drive all newbs nuts.

                  In the meantime I found a way to use multiple vars in a select. I use cases with Autoit so I'm used to those. My martial arts battered brain seems to be able to get around this easier for some reason. Interestingly ! == false does not work with this though == false does


                  switch(true) {
                  case stripos($description,$exclude1) :
                  echo $exclude1;
                  break;

                  case stripos($description,$exclude2) :
                  echo $exclude2;
                  break;

                  case stripos($checkdups,$title) :
                  echo $title;
                  break;

                  case (stripos($description,$keyword1)==false) :

                  if (stripos($description,$keyword2) == false) {

                  echo $checkdups;
                  break;

                  }


                  default: // do nothing
                  }



                  Rick
                  {{ DiscussionBoard.errors[10154954].message }}
                  • Profile picture of the author David Beroff
                    Woah.

                    Urm, first: "==" and "===" are not the same thing. This is precisely the problem that the large red warning is trying to highlight.

                    Second, whatever you're trying to do with switch/case, I'm pretty certain that it's not intended to do that.

                    Look, since the whole reverse-logic thing imposed by stripos() is clearly adding to the confusion, just avoid it by defining this function:

                    Code:
                    function contains( %haystack, %needle ) {
                        return (stripos( %haystack, %needle ) !== FALSE);
                    }
                    (Change the four %'s to $'s.)

                    Now you can use positive logic: contains( "Steve", "Eve" ) is true, because (when case is ignored), "Steve" contains "Eve". Similarly, to flip the test, you'd use an "!", (which means "not"), and just write !contains( "pebble", "rock" ), which is true, because pebble doesn't contain rock.

                    This should reduce the amount of parens to the point where it's easier to work, but in any case, you really do need to use the right tool, which is the if() where you started, and not switch/case.
                    Signature
                    Put MY voice on YOUR video: AwesomeAmericanAudio.com
                    {{ DiscussionBoard.errors[10155029].message }}
Avatar of Unregistered

Trending Topics