skaterdav85 Posted January 30, 2009 Share Posted January 30, 2009 Hi,So i used the strpos method in my condition to see if a state begins with the letter 'c', then make the table cell red. However this doesnt work. I used the substring method to achieve the same thing and this DID work. Anyone know why? <body><?php $state[0] = 'alabama'; $tax[0] = 4;$state[1] = 'alaska'; $tax[1] = 0;$state[2] = 'arizona'; $tax[2] = 5.6;$state[3] = 'california'; $tax[3] = 7.25;$state[4] = 'colorado'; $tax[4] = 2.9;$state[5] = 'connecticut'; $tax[5] = 6;$state[6] = 'delaware'; $tax[6] = 0;$state[7] = 'florida'; $tax[7] = 6;$state[8] = 'georgia'; $tax[8] = 4;$state[9] = 'hawaii'; $tax[9] = 4;$state[10] = 'idaho'; $tax[10] = 5;$state[11] = 'illinois'; $tax[11] = 6.25;$state[12] = 'massachusetts'; $tax[12] = 5;$state[13] = 'oklahoma'; $tax[13] = 4.5;$state[14] = 'oregon'; $tax[14] = 0;function calcTC($taxrate) { $TC = 200.00*(1 + $taxrate/100); return $TC;}?><table><?phpfor($count=0; $count<count($state); $count++) { echo ("<tr>"); if(substr($state[$count] , 0, 1) == 'c') { //if( strpos($state[$count],'c') == '0' ) { echo ("<td bgcolor='red'>"); } else { echo ("<td>"); } echo ($state[$count] . "</td>"); echo ("<td>".$tax[$count]."</td>"); echo ("<td>".calcTC($tax[$count])."</td>"); echo ("</tr>");}?></table></body> Link to comment Share on other sites More sharing options...
jeffman Posted January 30, 2009 Share Posted January 30, 2009 strpos() returns 0 as an index of the first character (if it's a match) and false if it finds nothing. A simple test for 0 returns true for 0 AND for false. You need the equivalency operator, and to get that zero out of quote marks: if( strpos($state[$count],'c') === 0 ) // THREE Link to comment Share on other sites More sharing options...
skaterdav85 Posted February 2, 2009 Author Share Posted February 2, 2009 strpos() returns 0 as an index of the first character (if it's a match) and false if it finds nothing. A simple test for 0 returns true for 0 AND for false. You need the equivalency operator, and to get that zero out of quote marks: if( strpos($state[$count],'c') === 0 ) // THREEwhy is there three equal signs? still confused on that. Link to comment Share on other sites More sharing options...
jeffman Posted February 2, 2009 Share Posted February 2, 2009 It's a similar but different operator. The designers could have used any combination of symbols. They chose === because it's easy to remember.They are different this way. == only checks the value. === checks the vale and the data type. 0 is an integer. false is a Boolean. (And that's not just lipstick on a pig. The different types have different memory requirements.) If you compare only their values, 0 and false are the same, so 0 evaluates to false, and 0 == false returns true. But since their data types are different, 0 === false returns false.The whole point is to provide a solution to situations like this one. strpos returns 0 if it finds the needle at the first character of the haystack. So 0 means it really did find something. It's a positive result. But strpos returns false if it finds nothing. So we need a way to distinguish between false and 0. === is the tool for that. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.