1

i was doing validation for user form after each validation i used to store "valid" in a array index and at last comparing them like this:

 if(isset($fullname)){
    if ($valid["name"]=="valid"&&$valid["username"]=="valid"&&$valid["password"]=="valid"&&$valid["email"]=="valid") {
            session_start();
            $_SESSION["reg_name"] = $fullname1;
            $_SESSION["reg_username"] = $username1;
            $_SESSION["reg_email"] = $email1;
            $_SESSION["reg_password"] = $password1;
            $_SESSION["reg_gender"] = $_REQUEST['gender'];
            header("location:validation&insertion.php");
    }

Well i will check validation and then make session .

My question is there any short way to check the whole array across a single value like "valid"? I hope you have understand my question.Comment it if it is not asked well. Do not rate as negative.Please ignore my grammar mistakes.I hate those who edit my question's grammar.

2
  • What would be the value otherwise if it is not valid? Commented Nov 2, 2016 at 5:13
  • it's value will be not valid i am storing by using if else if regex matches then set value to "valid" else set to "notvalid" Commented Nov 2, 2016 at 5:18

1 Answer 1

3

You can just count the number of unique values and check if it's equal to 1, then check one value if it is "valid".

if (count(array_unique($valid)) === 1 && $valid["name"] === "valid") {
    session_start();
    $_SESSION["reg_name"] = $fullname1;
    $_SESSION["reg_username"] = $username1;
    $_SESSION["reg_email"] = $email1;
    $_SESSION["reg_password"] = $password1;
    $_SESSION["reg_gender"] = $_REQUEST['gender'];
    header("location:validation&insertion.php");
}

Or just simply check if a "notvalid" value is found in the array:

if (!in_array("notvalid", $valid)) {
    session_start();
    $_SESSION["reg_name"] = $fullname1;
    $_SESSION["reg_username"] = $username1;
    $_SESSION["reg_email"] = $email1;
    $_SESSION["reg_password"] = $password1;
    $_SESSION["reg_gender"] = $_REQUEST['gender'];
    header("location:validation&insertion.php");
}
Sign up to request clarification or add additional context in comments.

3 Comments

@AwaisAhmad I have edited my answer; had some little mistakes.
i think you should delete first part second part is short and it has work perfectly
@AwaisAhmad The first part is an alternative though, if you will have another value assigned other than "notvalid". Anyways, no problem.

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.