Why is this function returning false?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nicolanicola
Forum Newbie
Posts: 5
Joined: Sun Oct 25, 2009 3:52 pm

Why is this function returning false?

Post by nicolanicola »

This is in an include file. I want it to check a value in an html form and see if it's just white space, is numbers, is empty etc.

Code: Select all

function checkString($stringFieldName,$_GET){
    $output="\n<p> ";
    $validate=true;
    if(array_key_exists($stringFieldName,$_GET))
  {
        //trim any whitespace from values
        $string=trim($_GET[$stringFieldName]);
        //check if string value is empty
        if(empty($string)){
            $output.="\nYou haven't entered any data in string<br />";
            $validate=false;
        }
        if(is_numeric($string)){
            $validate=false;
            $output.="You have entered a number instead of text";
        }
    //if they have deleted values from url
  }else{
    $output.="\nYou have deleted the characters from the url\n";
    $validate=false;
  }
  echo $output."\n</p>";
}
I am using it in a php file, the code is here:

Code: Select all

 
<?php
  echo "\n<p>";
  include 'validateText&Number.inc';
  checkString("surname",$_GET);
  if ($validate==false){
    echo "\nYou did not enter the name correctly.";  
  }
  else.etc etc etc
  {
When I type in actual text it tells me I have not entered name correctly which therefore means $validate must be false. I don't know how though it can be set to false because if the field just contains text, it isn't satisfying any of the conditions that lead to $validate=false.

Please help!! Oh I'm a php newbie btw.
xdpirate
Forum Newbie
Posts: 6
Joined: Sat Oct 24, 2009 11:06 pm

Re: Why is this function returning false?

Post by xdpirate »

If I'm not mistaken, this is an issue of scope. You define $validate within your function, and thus is not available to an outer function. Try having your function to return false; or return true;, and use that return value from the function to get your answer. $validate will always be false here, since it's a null value.

Code: Select all

<?php
function dostuff() {
    if (success) {
        return true;
    } else {
        return false;
    }
}
?>
 
Then, in your include-code:

Code: Select all

<?php
$returnvalue = dostuff();
if (!$returnvalue) {
    #false, act accordingly
} else {
    #true, act accordingly
}
?>
 
nicolanicola
Forum Newbie
Posts: 5
Joined: Sun Oct 25, 2009 3:52 pm

Re: Why is this function returning false?

Post by nicolanicola »

Thank you, it works! Such a great feeling when you finally get it working.

I'd have thought though if you were including a file, you'd be able to use it's variables as well?
nicolanicola
Forum Newbie
Posts: 5
Joined: Sun Oct 25, 2009 3:52 pm

Re: Why is this function returning false?

Post by nicolanicola »

nicolanicola wrote:Thank you, it works! Such a great feeling when you finally get it working.

I'd have thought though if you were including a file, you'd be able to use it's variables as well?
Actually thinking about it, you can't use a variable from within a function that's in the current page so it makes sense now. Thank you.
xdpirate
Forum Newbie
Posts: 6
Joined: Sat Oct 24, 2009 11:06 pm

Re: Why is this function returning false?

Post by xdpirate »

nicolanicola wrote:Thank you, it works! Such a great feeling when you finally get it working.

I'd have thought though if you were including a file, you'd be able to use it's variables as well?
Nope, unfortunately it counts as an 'outer loop,' such as:

Code: Select all

if ($value==$condition) {
    if ($othervalue==$othercondition) {
        $string = 'lolhax';
    }
    print($string);
}
...wouldn't work, because the second if loop is nested in the first one, and thus the values are destroyed when it reaches the {. :)
Have a read of the variables scope rules of PHP for further info.

And, you're welcome :)
nicolanicola
Forum Newbie
Posts: 5
Joined: Sun Oct 25, 2009 3:52 pm

Re: Why is this function returning false?

Post by nicolanicola »

It isn't outputting the errors now as stored within $output. Here is my new inc file code:

Code: Select all

 
<?php
function checkString($stringFieldName,$_GET){
    $output="\n<p> ";
    $validate=true;
    if(array_key_exists($stringFieldName,$_GET))
  {
        //trim any whitespace from values
        $string=trim($_GET[$stringFieldName]);
        //check if string value is empty
        if(empty($string)){
            $output.="\nYou haven't entered any data in string<br />";
            $validate=false;
        }
        if(is_numeric($string)){
            $validate=false;
            $output.="You have entered a number instead of text";
        }
    //if they have deleted values from url
  }else{
    $output.="\nYou have deleted the characters from the url\n";
    $validate=false;
  }
  if($validate==true){
    return true;
  }else{
    return false;
  }
  echo $output."\n</p>";
}
And my php file code

Code: Select all

<?php
  echo "\n<p>";
  include 'validateText&Number.inc';
  $returnvalue=checkString("surname",$_GET);
  if ($returnvalue==false){
    echo "\nYou did not enter the name correctly.";  
  }
  else
  {..........
 
I don't understand why it isn't running that part of the function now. Unless it stops when to gets to the return part?
xdpirate
Forum Newbie
Posts: 6
Joined: Sat Oct 24, 2009 11:06 pm

Re: Why is this function returning false?

Post by xdpirate »

nicolanicola wrote:I don't understand why it isn't running that part of the function now. Unless it stops when to gets to the return part?
That is exactly the problem.

From the return page at PHP.net:
If called from within a function, the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call.
Line 24 and onwards should be:

Code: Select all

echo $output."\n</p>";
if($validate==true){
    return true;
}else{
    return false;
}
return should always be the last statement that gets executed in a function.
Last edited by xdpirate on Sun Oct 25, 2009 5:49 pm, edited 2 times in total.
nicolanicola
Forum Newbie
Posts: 5
Joined: Sun Oct 25, 2009 3:52 pm

Re: Why is this function returning false?

Post by nicolanicola »

xdpirate wrote:
nicolanicola wrote:I don't understand why it isn't running that part of the function now. Unless it stops when to gets to the return part?
That is exactly the problem.

From the return page at PHP.net:
If called from within a function, the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call.
Ahh thank you. I love forums :)
Post Reply