benphp Posted May 13, 2008 Share Posted May 13, 2008 This will error out if the example number is a whole number (3, 9 33, etc.). I want to do something like: if (strlen($ex[1]) == NULL) but that doesn't work. How do you refer to an undefined element? or can you? <?php print "<form method=\"post\">"; print "<input type=\"text\" name=\"example\" size=\"6\" maxlength=\"6\">"; print "<input type=\"submit\" name=\"btnSubmit\" value=\"Round It\">"; print "</form>"; $example = ""; if (isset($_POST['example'])) { $example = $_POST['example']/3; $example = round($example, 2); $ex = explode(".", $example); if (strlen($ex[1]) == 0) { $example = $example."00"; } else if (strlen($ex[1]) == 1) { $example = $example."0"; } print "$example"; } ?> Link to comment https://forums.phpfreaks.com/topic/105451-how-to-refer-to-an-undefined-offset-in-an-array/ Share on other sites More sharing options...
Lumio Posted May 13, 2008 Share Posted May 13, 2008 <?php if (isset($ex[1]) === true && strlen($ex[1]) == 0) { //do it! } ?> Also convert $example into a string. That's better for php: $example = strval($example); $ex = explode($example); greetings Link to comment https://forums.phpfreaks.com/topic/105451-how-to-refer-to-an-undefined-offset-in-an-array/#findComment-540079 Share on other sites More sharing options...
benphp Posted May 13, 2008 Author Share Posted May 13, 2008 I've tried variations of that - no good. I still get "Undefined offset" for that line. BTW what does the three === do that == doesn't? I've seen it around but never used it. Link to comment https://forums.phpfreaks.com/topic/105451-how-to-refer-to-an-undefined-offset-in-an-array/#findComment-540083 Share on other sites More sharing options...
sasa Posted May 13, 2008 Share Posted May 13, 2008 just use $example = number_format($_POST['example']/3,2); Link to comment https://forums.phpfreaks.com/topic/105451-how-to-refer-to-an-undefined-offset-in-an-array/#findComment-540149 Share on other sites More sharing options...
benphp Posted May 13, 2008 Author Share Posted May 13, 2008 Yes, that's easier. Link to comment https://forums.phpfreaks.com/topic/105451-how-to-refer-to-an-undefined-offset-in-an-array/#findComment-540167 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.