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
try
this will evaluate that $text_message is set to “hello user”
merely evaluates that $text_message is set and is set to something other than null or false.
Try this:
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.
So you only check empty 2x instead of 4x.
Original and Issues
You have to (or should) consider 4 possibility (2^2) each variable can have 2 states, well call them on and off
In your original, you are only considering 2 of them
Because:
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.
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:
hope that makes sense.