0

i have an array of usernames and if a specific name is entered then code should say i know you concatenated with input name...this part is working fine...the problem is in if(!$knowyou) this line of code is giving the error of undefined variable i'm not getting it i tried to resolve it by declaring variable to to top of code i.e giving variable a global scope but this doesn't solve my problem i also tried defining variable at different places but it doesn't work for me...please anyone of you can explain the solution of error?? if you have time then please tell me why this error occure so i can avoid it in future thanks in advance.

<body>
<?php 
//print_r ($_GET);
//$knowyou;

$names=array("mirza","ali","usman");
if(isset($_POST['submit'])){

    if($_POST["uname"]){
        foreach($names as $name){
            if($_POST["uname"]==$name){

                echo "i know you your name is ".$name;
                $knowyou=1;
                }

        }
        if(!$knowyou){
            echo "i don't know you".$_POST["uname"];
            }
    }
    else {
    echo"please enter your name";
    }
}

?>
<form method="post">
  <label for="uname">Name</label>
  <input type="text" name="uname" />
  <input type="submit" name="submit" value="Submit" />
</form>
</body>
2
Contributors
2
Replies
15
Views
1 Year
Discussion Span
Last Post by vampz
1

You wrote that you declared the variable $knowyou in the top of the code , you didn't . You just have a comment there with the variable name that doesn't make any sense @see http://php.net/manual/en/language.variables.php AND @see http://php.net/manual/en/language.basic-syntax.comments.php .

Now back to your code, when you want a variable to be boolean use true or false as values. So in line 14 $knowyou = true; (Not 1) When you first declare the variable assign it the value false , then only if your critirea matches will be true. Line 4 $knowyou = false; (not comments).

0

wow thanks a lot for the explanation links...yes my proble is solved now.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.