Jump to content

Help with empty() test


Jawn

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.