0

I'm still new in PHP and I need to know how I can create two or more conditional statements for a single elements. For instance, maybe if I had a form that I wanted to perform a certain action if some conditions were true.

I understand that one of the way is to group the statements like:

if(arg1 && arg2 && arg3) {
echo "All of these statements are true";
}

How can I separate this condition but making sure that it executes at the same time?

1
  • Can you be more specific about what you are trying to do? You can simply nest multiple if statements if you want to... Commented May 28, 2013 at 0:23

3 Answers 3

3

You can use nested if statements to accomplish the same check.

if(arg1)
{
    if(arg2)
    {
        if(arg3) 
        {
            echo "All of these statements are true";
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Alright. Sorry for not being specific enough. Suppose I had a form that I wanted it: Check if the that the all fields have been filled in before it's submitted, and it must echo something out if that condition is not met. I also wanted, for the same form, to check if the username field doesn't contain digits. how would I go about writing that? You don't have to give the full coding. A basic syntax would do.
@Mosire If you want to check the fields before it's submitted, your have to use client-side (javascript) not server-side (php). You would have to submit the form then do your verification if you want to use php. stackoverflow.com/…
@Mosire You should start in checking for isset and !empty values from form and then analyze each $_POST with some more if statements. Tha's basic logic. You should start fro here and post a new question once you will find problems with your code
@Mosire about my answer I think it was correct, so please accept it and upvote if you feel the same.
haha! No doubt Fabio, you've been interacting with me a lot the past couple days and I'm thankful. @Sean, there's ways to go about it in PHP as well... One more question Fabio, is it okay if the 'if' statements don't have the else statement sometimes? Maybe, when I don't want and opposing action?
|
3

Try this.

   if(($something == 1 ) && ($someotherthing !=7) && ($something == 2)) {
    echo "All of these statements are true";
    }

Comments

1

Changed answer after what I understood of you clarification:

If you want to know if the posted form has all the required fields in PHP then you could write :

if(isset($_POST['field_name']) && $_POST['field_name'] != "")
{
    //The field is set and not empty.
}
else
{
    echo "Field not set...";
}

w3schools tutorial on forms : http://www.w3schools.com/php/php_forms.asp

As for the numeric part of your question take a look at regex : http://www.codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial

EDIT 2 : As someone mentioned if you want to know if the field is set BEFORE being sent to the server, you will need to use Javascript.

1 Comment

Thanks for the answer. I think I wasn't specific enough. Please check the comment that I have made on Fabio's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.