ajoo Posted February 16, 2020 Share Posted February 16, 2020 Hi, I wish to check for a condition that a string received is not white space or false or null but it should allow the string '0'. This must be a very simple problem but it is definitely quite confusing. Thanks loads. Quote Link to comment https://forums.phpfreaks.com/topic/310044-string-checks/ Share on other sites More sharing options...
ajoo Posted February 16, 2020 Author Share Posted February 16, 2020 Hi, The following seems to work but still allows for a non string 0. (isset($str) && $str !==false && $str !==''){ . . . } Maybe I need to check for an is_string as well. I was hoping to find something simpler if it exists. Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/310044-string-checks/#findComment-1574331 Share on other sites More sharing options...
gw1500se Posted February 16, 2020 Share Posted February 16, 2020 If I understand, you are really using the wrong approach. You really want to use preg_match and a regular expression. I am not a regexp expert so you should ask tor the expression in the regexp forum. Quote Link to comment https://forums.phpfreaks.com/topic/310044-string-checks/#findComment-1574333 Share on other sites More sharing options...
requinix Posted February 17, 2020 Share Posted February 17, 2020 If it's a string then it won't be ===false. Because it's a string. Same for ===null. Maybe what you're looking for is isset($_POST["whatever"]) && trim($_POST["whatever"]) != "" Quote Link to comment https://forums.phpfreaks.com/topic/310044-string-checks/#findComment-1574351 Share on other sites More sharing options...
ajoo Posted February 17, 2020 Author Share Posted February 17, 2020 Hi ! @ gw1500se : While it's complicated I don't think it would require a regex since it's also basic. @requinix : Since this is an input received via ajax, someone can try and doctor the input to try and break the code. Hence I had the == false as well. And since a string won't be equal to false, I am thinking that it may be necessary to check that the input received is indeed a string. Wouldn't that be so ? Would you still go ahead with your code that you pasted above? Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/310044-string-checks/#findComment-1574356 Share on other sites More sharing options...
requinix Posted February 17, 2020 Share Posted February 17, 2020 27 minutes ago, ajoo said: Since this is an input received via ajax, someone can try and doctor the input to try and break the code. Hence I had the == false as well. And since a string won't be equal to false, I am thinking that it may be necessary to check that the input received is indeed a string. Wouldn't that be so ? Would you still go ahead with your code that you pasted above? It will be either a string or an array. If you want to protect against that, a simple is_string() will suffice. Quote Link to comment https://forums.phpfreaks.com/topic/310044-string-checks/#findComment-1574358 Share on other sites More sharing options...
ajoo Posted February 17, 2020 Author Share Posted February 17, 2020 hmm, So I can finally use isset($_POST["whatever"]) && is_string($_POST["whatever"]) && trim($_POST["whatever"]) != "" because, as you said, if it's a string, it won't be false or null. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/310044-string-checks/#findComment-1574360 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.