Log in

View Full Version : String Comparison Difficulties



marain
04-29-2012, 09:43 PM
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:




<!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:




<!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>

traq
04-30-2012, 12:17 AM
... The second condition is that $subhead[$here] does not contain the word "Click". My PHP source code tests for those conditions.
[ ... ]

$bigBoole = strpos( $subhead[$here], $click, 0);
if ( $subhead[$here] && $bigBoole != 0 && $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).
$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:

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:

if ( $subhead[$here] && $bigBoole != 0 && $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 != 0 && $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.


// 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];
}

marain
04-30-2012, 01:42 AM
Yes. The "if" line should not have concluded with the semi-colon. Problem solved.

Thank you.

A.