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, 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'; 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 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'; 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 Link to comment https://forums.phpfreaks.com/topic/252936-check-first-2-digits/#findComment-1296774 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.