Let's say I've got this string:
$str = '1-2-3'; and I explode this like so:
$strSplit = explode('-', $str); what would be the best way to check if I've got the correct number of elements and whether they are numeric (because the input can vary)?
Straightforward, I would say something like this:
if (isset($strSplit[0]) && is_numeric($strSplit[0]) && isset($strSplit[1]) && is_numeric($strSplit[1]) && isset($strSplit[2]) && is_numeric($strSplit[2]) { // some code } But you could also do:
if (count($strSplit) === 3 && is_numeric($strSplit[0]) && is_numeric($strSplit[1]) && is_numeric($strSplit[2])) { // some code } You can use some array functions to check for the is_numeric part, but that seems fussy, less readable and is probably slower. I could be wrong, though...