tags:

views:

246

answers:

3

I know this is embarrassing easy but I cannot get this to work right now, keep getting syntax errors, I just added in a jquery code that pre-fills in a form filed and when you select the form field it will clear the default value. The result though is if a user submits the form without changing the default value, I need to see if it exist in addition to my normal string sanitations

In this snippet below of PHP I need to run 2 conditions on $fname but below will not work, can someone help please

$fname = 'first name';

if (trim($fname) == '') && ($fname != 'first name') {
    $err .= "error";
}else{
    $err .= "all good";
}


For karim79 this code below from your example, exactly like this gives me this error

Fatal Error: Can't use function return value in write context on line 5

<?PHP
$fname = '';

if(empty(trim($fname))) {
    echo "First name is empty";
}
?>
+5  A: 
$fname = 'first name';

if (trim($fname) == '' || $fname != 'first name') {
    $err .= "error";
} else {
    $err .= "all good";
}

I would prefer to use strcmp:

if (trim($fname) == '' || strcmp($fname,'first name') !== 0) {
    $err .= "error";
} else {
    $err .= "all good";
}

If the case of the first name is not important, you should consider using strcasecmp instead. Also note you can use empty to test for the empty string:

$fname = '';
$fname = trim($fname);
if(empty($fname)) {
    echo "First name is empty";
} else {
    echo "Not empty";
}

When using empty, beware the following (from the manual):

Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).

karim79
Amber
I just looked at the page for strcmp and the strcasecmp, these compare the length as in number of characters? If that is the case then I don't think it will work for this, I just need to make sure the $fname variable is not empty and that it contents does not contain the exact word "first name"
jasondavis
@jasondavis - I was just pointing out that strcmp (according to the PHP Certification guide at least) is the proper way to compare strings. You can use strcmp to test for the empty string, but I think empty is more readable for that.
karim79
yes empty looks better, thanks
jasondavis
1 other thing, usung empty is giving me this Fatal error: Can't use function retunr value in write context
jasondavis
@jasondavis - how are you calling it?
karim79
same way in your last example above
jasondavis
@jasondavis - There was a missing parenthesis ')'. Try now.
karim79
still gives me that same error with the extra ) in place
jasondavis
@jasondavis - it has to be to do with the context in which you're using it - can you paste the code you're using?
karim79
@karim79 sure I just added it to bottom of my question post above
jasondavis
@jasondavis - I get it now, it won't work to call trim($fname) *inside* empty(). See my edit.
karim79
+2  A: 
$fname = 'first name';

if (trim($fname) == '' || $fname == 'first name') {
    $err .= "error";
}else{
    $err .= "all good";
}

PS: I assumed you want to raise an error if the string is either empty or the standard value. If that's wrong let me know.

André Hoffmann
thanks I'm not sure why I was thinking this was an AND situation instead of an OR
jasondavis
A: 

I would NOT recommend using empty() for anything. It has some tricky return patterns, including telling you that a 0 is empty, and things of that nature. This, unfortunately, is a shortcoming of PHP.

Instead, try this algorithm (The following assumes your form POSTs):

<?php
    $err = array();
    // this is for sticklers..with E_STRICT on, PHP
    // complains about uninitialized indexes
    if( isset($_POST['name']) )
    {
       $name = trim($_POST['name']);
    }
    else
    {
       $name = '';
    }

    if( strlen($name) == 0 )
    {
      $err[] = "First name is required.";
    }

    // after validation is complete....
    if( count($err) > 0 )
    {
        echo "There are errors!";
        // probably something more elaborate here, like 
        // outputting an ordered list to display each error
        print_r($err);
    }
    else
    {
        echo "It's all good!";
    }
?>
iddqd