{"id":2350,"date":"2004-12-13T10:00:25","date_gmt":"2004-12-13T10:00:25","guid":{"rendered":"http:\/\/www.soulhuntre.com\/items\/date\/2004\/12\/13\/headspace-a-little-crazy\/"},"modified":"2004-12-13T10:00:25","modified_gmt":"2004-12-13T10:00:25","slug":"headspace-a-little-crazy","status":"publish","type":"post","link":"http:\/\/legacyiamsenseiken.local\/2004\/12\/13\/headspace-a-little-crazy\/","title":{"rendered":"$headspace == “a little crazy!”"},"content":{"rendered":"

Here is the hard won PHP<\/a> knowledge of the weekend. Contemplate the following…<\/p>\n

$haystack = “Hello World!”;<\/p><\/blockquote>\n

Now, let’s imagine you want to test to see if it contains the string “Hello”. You would think that the following code would do it:<\/p>\n

<?php <\/p>\n

\/\/ this code will not always work as expected
$haystack = “Hello World!”;
$needle = “Hello”;
if( strpos( $haystack, $needle ) ){
    echo “Found it!”;
}else{
     echo “Not found!”;
 }<\/p>\n

?><\/p>\n<\/blockquote>\n

You would be wrong. See, the strpos()<\/a> function is a bit  funky. It returns either false<\/b><\/i> if the string could not be found, or the position of the string<\/i><\/b> if it could be found. Soulhuntre I hear you cry, that will work perfectly!<\/p>\n

Except of course if the tested string is at the beginning of the larger string (needle and haystack respectively). If so, then the return from strpos() is 0<\/i><\/b> and chaos ensues. Thanks to the normally incredibly helpful implicit conversions of PHP the string found at the beginning will be at position 0 and will evaluate to false<\/i><\/b> in the if()<\/a> above.<\/p>\n

What you need in this case is the === operator<\/a>. That is three “=”‘s in a row. This says that something is equal only if it is the same data type and has the same value. No implicit conversions. The following code will work as expected:<\/p>\n

<?php <\/p>\n

\/\/ this code will work as expected $haystack = “Hello World!”;
 $needle = “Hello”;<\/p>\n

if( strpos( $haystack, $needle ) === false ){
    echo “The string was not found!”;
}else{
    echo “Found it!”;
}<\/p>\n

if( strpos( $haystack, $needle ) !== false ){
    echo “The string was found!”;
}else{
    echo “The string was not found!”;
} if( strpos( $haystack, $needle ) === 0 ){
    echo “The string was at the beginning!”;
}else{
    echo “The string was not there, or not at the beginning!”;
}<\/p>\n

 ?><\/p>\n<\/blockquote>\n

Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"

Here is the hard won PHP knowledge of the weekend. Contemplate the following… $haystack = “Hello World!”; Now, let’s imagine you want to test to see if it contains the string “Hello”. You would think that the following code would do it: <?php \/\/ this code will not always work as expected$haystack = “Hello World!”;$needle […]<\/p>\n","protected":false},"author":3,"featured_media":53158,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[278],"tags":[],"_links":{"self":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/posts\/2350"}],"collection":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/comments?post=2350"}],"version-history":[{"count":0,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/posts\/2350\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/media\/53158"}],"wp:attachment":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/media?parent=2350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/categories?post=2350"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/tags?post=2350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}