gamefreak13 Posted May 23, 2008 Share Posted May 23, 2008 I have the below code which checks a text string (which is really a url) for certain occurances. However, I also need to make it check if the string is completely empty. This is being used to transform referer url's in to fancy labels, and if the referer is missing, I need it to say "Referer missing". I know an if/else statement would work, but I'm not sure how to implement that in to a switch/case function... or if there is an easier way? function my_parse($text){ $a = parse_url($text); parse_str($a['query'],$b); $b = array_change_key_case($b); switch ($b['fuseaction']){ case 'user.viewprofile': return 'Profile '.$b['friendid']; break; case 'bulletin.read': return 'Bulletin '.$b['messageid']; break; case 'mail.readmessage': return 'Message '.$b['messageid']; break; default: return 'Unknown Source'; } print_r($b); } Quote Link to comment https://forums.phpfreaks.com/topic/106888-check-if-variable-is-empty/ Share on other sites More sharing options...
smc Posted May 23, 2008 Share Posted May 23, 2008 Multiple options. 1) empty(); (Returns TRUE if it is empty, or FALSE if not) 2) if( $myVariable == "" ){} (Executes inner code if it is empty) 3) If you want to check if the variable is even set... isset( $myVariable ); (Returns TRUE if set, FALSE if not) Quote Link to comment https://forums.phpfreaks.com/topic/106888-check-if-variable-is-empty/#findComment-547895 Share on other sites More sharing options...
gamefreak13 Posted May 23, 2008 Author Share Posted May 23, 2008 Where would I insert that? Would it work if I put it between the second-to-last } and print_r? if($test == "") { return "Referer missing"; } How does empty(); work? I would guess like so.. if(empty($test)) { return "Referer missing"; } Quote Link to comment https://forums.phpfreaks.com/topic/106888-check-if-variable-is-empty/#findComment-547897 Share on other sites More sharing options...
smc Posted May 23, 2008 Share Posted May 23, 2008 Either syntax will accomplish what you're looking for. Where to put it depends on in what order you want to check for it. Personally I don't do much with case-switches so however you would traditionally incorporate a boolean if statement. Quote Link to comment https://forums.phpfreaks.com/topic/106888-check-if-variable-is-empty/#findComment-547900 Share on other sites More sharing options...
gamefreak13 Posted May 23, 2008 Author Share Posted May 23, 2008 Does anyone else know how to implement it? I'm clueless. I wonder if this would work? This would be in the file where the function is called. if(empty($myvar)) { echo "Referer missing"; } else { return my_parse($myvar); } Quote Link to comment https://forums.phpfreaks.com/topic/106888-check-if-variable-is-empty/#findComment-547913 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.