matleeds Posted December 11, 2011 Share Posted December 11, 2011 Hi, I'd like to know what the most efficient code is for checking the first 2 digits of a string. eg a strings first 2 digits must equal '07' thanks, Quote Link to comment https://forums.phpfreaks.com/topic/252936-check-first-2-digits/ Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 Try that $num = '07654'; $check = substr($num, 0, 2); if($check == '07') echo 'OK'; else echo 'BAD'; Quote Link to comment https://forums.phpfreaks.com/topic/252936-check-first-2-digits/#findComment-1296765 Share on other sites More sharing options...
matleeds Posted December 11, 2011 Author Share Posted December 11, 2011 thanks. how would one use that if ? : statement in this context Quote Link to comment https://forums.phpfreaks.com/topic/252936-check-first-2-digits/#findComment-1296769 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 thanks. how would one use that if ? : statement in this context The ternary operator ? Do you mean it ? $num = '07654'; $check = substr($num, 0, 2); echo $check == '07' ? 'OK' : 'BAD'; Quote Link to comment https://forums.phpfreaks.com/topic/252936-check-first-2-digits/#findComment-1296770 Share on other sites More sharing options...
matleeds Posted December 11, 2011 Author Share Posted December 11, 2011 cheers bud Quote Link to comment https://forums.phpfreaks.com/topic/252936-check-first-2-digits/#findComment-1296774 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.