skip to Main Content

I am working on a chat room and the condition i want to implement is:
when user send both file and text message it will return an error.

but this isn’t working correct.

this is my code

if($_FILES['chat_upload_file']){

                    echo "You are in file checking<br/>";
                    echo $text_message;
                    if($text_messege){
                        echo "It's in IF<br/>";
                        $error = 1;
                        $error_msg = "Either Sender can send file or text";
                        exit(0);
                    }
                    else{

                        echo "it's in else<br/>";
                        exit(0);
}

here $text_messege = "hello user";

the output that i want is from internal IF : It’s in IF

but the output is : It’s in else

2

Answers


  1. try

    if($text_messege == "hello user"){
    

    this will evaluate that $text_message is set to “hello user”

    if($text_messege){
    

    merely evaluates that $text_message is set and is set to something other than null or false.

    Login or Signup to reply.
  2. Try this:

    if(!empty($_FILES['chat_upload_file']) && !empty($text_messege)){
        //both
    }else if(!empty($_FILES['chat_upload_file'])){
        //file only
    }else if(!empty($text_messege)){
        //message only
    }else{
        //nither
    }
    

    I would probably use empty instead of just testing if it’s true or false. Empty will cover undefined index issues you may have. Without more to go on, I have no way to know if that is a possibility. For example I have no Idea where $message comes from and if it’s always considered “set” (or not undefined).

    You can optimize this by doing something like this.

    $file = !empty($_FILES['chat_upload_file']) ? $_FILES['chat_upload_file'] : false;
    
    $text_messege = !empty($text_messege) ? $text_messege : false;
    
    if($file && $text_messege){
        //both
    }else if($file){
        //file only
    }else if($text_messege){
        //message only
    }else{
        //nither
    }
    

    So you only check empty 2x instead of 4x.

    Original and Issues

    the condition i want to implement is: when user send both file and text message it will return an error.

    You have to (or should) consider 4 possibility (2^2) each variable can have 2 states, well call them on and off

      File | Message
       on    on
       on    off
       off   on
       off   off
    

    In your original, you are only considering 2 of them

      File | Message
       on    on
       on    off
    

    Because:

    if($_FILES['chat_upload_file']){
        if($text_messege){
           //file: on  message: on
        }else{
          //file: on  message: off
        }
    }
    

    Does not consider what happens when no file is submitted. And if it’s an error to send a File and a Message, then it must be ok to send just a message and no file. Otherwise, you would never be able to send a message (you cant send on with a file, and you cant send one without). Maybe there is stuff below this? I have no way to know that, though.

    It makes little sense to add an else to the outer IF, because you will probably wind up duplicating stuff.

    if($_FILES['chat_upload_file']){
        if($text_messege){
           //file: on  message: on
        }else{
          //file: on  message: off
        }
    }else{
        if($text_messege){
           //file: off  message: on
        }else{
          //file: off  message: off
        }
    }
    

    And it’s a lot cleaner to have 1 control block with 4 conditions, then 3 control blocks with 6 conditions.

    It’s implied by what you want to do that:

    • it’s fine to have just a file
    • it’s fine to have just a message (you don’t consider it)
    • it’s not fine to have both a file and a message
    • who knows with neither (you don’t consider it)

    hope that makes sense.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search