Jawn Posted February 16, 2008 Share Posted February 16, 2008 Hello people, can someone please save me before I lose my mind? I am doing the "PHP with MySQL Essential Training" video tutorial by Kevin Skoglund at lynda.com, now I like the tutorials so far and Kevin seems to know what he is talking about. However, there doesn't seem to be a way to get help when you get stuck. During one of the videos he puts in the following code: <?php if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && !is_int($_POST[$fieldname]))) { $errors[] = $fieldname; } ?> Which is supposed to test to see if a posted variable is set, but not empty unless it is an integer - then he runs it in the video and it works for him, but when I run it zero always fails. I have checked the code he is using against mine to make sure I have exactly what he does, about a hundred times now and it seems to be correct. Thinking that I might have errors somewhere else in my file I made a separate script just to test this line of code: <?php if(isset($_POST['submit'])) { $errors = array(); if (!isset($_POST['test']) || (!is_int($_POST['test']) && (empty($_POST['test']) ))) { echo "The post <b>failed</b> the if test,<br /> the value passed was: " . $_POST['test']; } else { echo "The post <b>passed</b> the if test,<br /> the value was: " . $_POST['test']; } } ?> <form action="test.php" method="post"> <p>Value for test:<input type="text" name="test" value="" /></p> </p> <input type="submit" name="submit" value="Send Testpost" /> </form> But when I run this one it also always fails on zero, so I have no idea how to proceed. Any direction would be appreciated, thanks for your time. Link to comment https://forums.phpfreaks.com/topic/91370-help-with-empty-test/ Share on other sites More sharing options...
Chris92 Posted February 16, 2008 Share Posted February 16, 2008 I think this is what you were trying to do: <?php if( empty($_POST['test']) || !is_int($_POST['test']) ) { echo "The post <strong>failed</strong> the if test,<br /> the value passed was: " . $_POST['test']; } else { echo "The post <strong>passed</strong> the if test,<br /> the value was: " . $_POST['test']; { ?> Link to comment https://forums.phpfreaks.com/topic/91370-help-with-empty-test/#findComment-468251 Share on other sites More sharing options...
kenrbnsn Posted February 16, 2008 Share Posted February 16, 2008 You have discovered the flaw with using the emty() test to see if a field is truly empty. The "0" (zero) is equivalent to "false". What I do to see if a passed field is truly empty is <?php if (strlen(trim(stripslashes($_POST['fld']))) == 0) echo 'field is empty; ?> Ken Link to comment https://forums.phpfreaks.com/topic/91370-help-with-empty-test/#findComment-468306 Share on other sites More sharing options...
Jawn Posted February 16, 2008 Author Share Posted February 16, 2008 Thanks guys, that has helped immensely, I thought I was just being dense but it looks like a mistake in the tutorial. Link to comment https://forums.phpfreaks.com/topic/91370-help-with-empty-test/#findComment-468334 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.