1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Quick question? Why doesn't this work?

Discussion in 'PHP' started by -bank-, Jun 30, 2007.

  1. #1
    Dear All,

    I'm currently reading a book to learn php, although haven't got too far (now on chapter 3) any way, basically, I've tried to write a basic contact form with validation, I've decided to use the empty function on mulitple items, although this line doesn't seem to want to work:

    if (empty($subject && $email && $message)) {
    PHP:
    All the rest after that is complete, so code after that is correct, and the else is ok, etc.

    Thanks
     
    -bank-, Jun 30, 2007 IP
  2. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think that will only work if all three variables are empty. Did you want that, or for it to be if any one of them is empty?
     
    egdcltd, Jun 30, 2007 IP
  3. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if ( empty($subject) && empty($email) && empty($message) ) {
    PHP:
    If you want to pass multiple values to a function (or language construct that behaves like a function), you separate the values with a comma. Additionally, empty() can only take one argument anyway.

    The && indicates both conditions must be satisfied - effectively it's the same as:
    if ( empty($subject) ) {
     if ( empty($email) ) {
      if ( empty($message) ) {
    
       // code to evaluate here
    
      }
     }
    }
    PHP:
    and if you write your code like that, it should be clear why it doesn't work.
    if ( empty($subject ) {
     if ( $email ) {
      if ( $message) ) {
    
       // code here
    
      }
     }
    }
    PHP:
     
    rodney88, Jun 30, 2007 IP
  4. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What the other posters are getting at is that empty() does not work when you tie values together in the manner in which you have done so.

    You need to test each individual variable. Also, if one of the variables were set the value of 0 or a zero, empty would say it was empty. This may not be the correct result insofar as an email message body or subject could legitimately have the value 0 -- as far fetched as that may seem ;)

    You look like you are testing out a mail form. If I were doing that I might just test to see if they were NULL with the expression !$subject or "NOT $subject" as in

    if( !$subject) { do_something();}
    Code (markup):
    If you want to ask the user for a correction if any one of the values is not set, you might try:

    if ( !$subject || !$email || !$message ) { do_something();}
    Code (markup):
    You code needed all the values to be NULL or set to '0' before it would do_something() The two bars "||" means OR, while "&&" means AND

    Another useful function is:

    isset( $subject)
    Code (markup):
    Again, you need to test each individual variable separately. For instance:

    if ( isset($subject) || isset($email) || isset($message) )
    { do_something();}
    Code (markup):
    Do not forget to initialize variables before they are used -- do not forget to validate the user's input -- and never use user supplied data without first sanitizing it!
     
    clancey, Jun 30, 2007 IP
  5. Acecool

    Acecool Peon

    Messages:
    267
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I am surprised no one covered this yet...

    Ok, it looks like you are reading a form, if you read form data use the $_POST global to read it :)


    if (isset($_POST['submit']))
    {
         // Form was submitted, lets check variables...
         if (isset($_POST['subject']) && !empty($_POST['subject']))
         {
              // Subject is good, you can add more to that line up there by copying and pasting :-)
              // Do something in here...
         }
    
         // OR DO this..
         $subject = (isset($_POST['subject']) && !empty($_POST['subject'])) ? $_POST['subject']: 'Err';
         $name = (isset($_POST['name']) && !empty($_POST['name'])) ? $_POST['name']: 'Err';
    
         if ($subject != 'Err' && $name != 'Err')
              echo 'Ok, send the message or whatever :-)';
    }
    Code (markup):
     
    Acecool, Jun 30, 2007 IP
  6. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #6
    either way it has already been established that empty() only accepts one argument. you cannot evaluate multiple variables within it. check them individually.

    
    if ( empty($var1) && empty($var2) )
    {
        // do
    }
    
    PHP:
     
    ansi, Jun 30, 2007 IP
  7. -bank-

    -bank- Well-Known Member

    Messages:
    674
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    120
    #7
    After a little more reading of my book I found out how to do it, although thanks for the suggestions, and this one was spot on :) thanks alot again.

     
    -bank-, Jul 3, 2007 IP