Results 1 to 3 of 3

Thread: String Comparison Difficulties

  1. #1
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default String Comparison Difficulties

    My PHP source code contains a variable called $subhead[$here ]. I want to echo that variable if and only if two conditions are satisfied. The first condition is that $subhead[$here] is not empty. The second condition is that $subhead[$here] does not contain the word "Click". My PHP source code tests for those conditions. In the instance giving me problems, $subhead[$here] DOES contain the word "Click". Therefore, it should not be echoing its contents. But it echos anyway.

    I have pasted the relevant source code (containing white space for readability), followed by the generated code. The code also echos html comments (imperfectly formatted), showing the values in the various variables.

    I'm quite sure my error is elementary but I cannot find it. Please help.

    A.




    THE PHP SOURCE:



    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    
    <title>New Jersey Criminal Defense Lawyer
    <?php
    
    
    
    
    
    
    
    
    
    
    /*    test section         */
    
    
    $click = 'Click';
    $bigBoole = strpos( $subhead[$here], $click, 0);
    if ( $subhead[$here] && $bigBoole != 0 && $bigBoole != '0');
    echo ' - ' . $subhead[$here];
    
    
    echo '<!-- $subhead[$here] = ' . $subhead[$here] . '.    -->\n';
    echo '<!-- $click = ' . $click . '.   -->\n';
    echo '<!-- $bigBoole = ' . $bigBoole . '.   -->\n';
    
    
    /*   end test section       */
    
    
    ?>
    
    
    
    
    
    
    
    
    
    
    </title>
    
    
    ********************************************************

    THE GENERATED CODE:



    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    
    <title>New Jersey Criminal Defense Lawyer
     - Click <a href="http://www.MarainLaw.com/quest" style="color : #ff4040;">Here</a> for Your Confidential No-Obligation Case Evaluation<!-- $subhead[$here] = Click <a href="http://www.MarainLaw.com/quest" style="color : #ff4040;">Here</a> for Your Confidential No-Obligation Case Evaluation.    -->\n<!-- $click = Click.   -->\n<!-- $bigBoole = 0.   -->\n
    
    
    
    
    
    
    
    
    
    </title>
    Last edited by ddadmin; 04-29-2012 at 10:59 PM.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by marain View Post
    ... The second condition is that $subhead[$here] does not contain the word "Click". My PHP source code tests for those conditions.
    [ ... ]
    PHP Code:
    $bigBoole strpos$subhead[$here], $click0);
    if ( 
    $subhead[$here] && $bigBoole != && $bigBoole != '0'); 
    actually, you're checking that $subhead[$here] does contain "Click", but does not start with "Click."

    strpos returns either FALSE (if $needle was not found), or an integer (the location of the first $needle found).
    PHP Code:
    $result strpos"_____ here!","Click" );
    // $result is FALSE
    // ($result != 0) is FALSE
    // ($result !='0') is redundant (but also FALSE)

    $result strpos"Click here!","Click" );
    // $result is 0
    // ($result != 0) is FALSE
    // ($result !='0') is redundant (but also FALSE)

    $result strpos" Click here!","Click" );
    // $result is 1
    // ($result != 0) is TRUE
    // ($result !='0') is redundant (but also TRUE) 
    don't check for 0 (or "0"). Check for FALSE:
    PHP Code:
    if( $bigBoole === FALSE ){
       
    // "Click" was not found. (anywhere in the string.)
       // note we use "===" ("exactly equal"), and not "==" ("equivalent").
       // the opposite of this is "!==" ("not exactly equal").

    ---------------------------
    But before any of this will work, you need to clean up your code:
    PHP Code:
    if ( $subhead[$here] && $bigBoole != && $bigBoole != '0');  
    // this is meaningless.  You're checking the condition, but doing nothing either way.
    // I think you meant to do this...?
    if( $subhead[$here] && $bigBoole != && $bigBoole != '0'){
        echo 
    ' - ' $subhead[$here];
    }
    // lesson: always use brackets.  they will help you:
    //    easily identify what your logic is doing
    //    easily identify where you've made mistakes in syntax. 
    PHP Code:
    // HOWEVER, to get the result you want, do it this way instead:
    if( !empty( $subhead[$here] ) && $bigBoole === FALSE ){
        
    // not empty.
        // no "Click".
        
    echo ' - '$subhead[$here];

    Last edited by traq; 04-30-2012 at 12:29 AM.

  3. #3
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default

    Yes. The "if" line should not have concluded with the semi-colon. Problem solved.

    Thank you.

    A.

Bookmarks

Posting Permissions

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