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>

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).

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.