Click to See Complete Forum and Search --> : Functions, arrays, Oh my
SuperHornet
12-14-2003, 12:35 PM
I must admit that PHP is new to me.
But when I execute this code:
$input= array
(
"fname" => $fname,
"lname" => $lname,
"uname" => $uname,
"password" => $password,
"title" => $title,
);
function clean_input ($input)
{
while (list($key,$value) = each($input))
{
$value = strip_tags($value);
$value = htmlspecialchars($value);
$value= trim($value);
$cinput= array($key => $vlaue);
}
print_r($cinput);
return $cinput;
}
clean_input($input);
print_r ($cinput);
?>
I get this data in the array
Array ( [title] => creator )
It seems to completely skip all of the other $value and only push the last $value ($title) into $cinput. Because if I do
echo "$value <br>";
inside of the while loop will print it all correctly.
Then when I return the new array ($cinput) with the modified $value I get this error.
Notice: Undefined variable: cinput in check_input.php on line 25
I'm sure its something dumb I'm over looking, I hope someone can point it out.
bwkaz
12-14-2003, 02:39 PM
I don't know PHP either, but I'd guess that the problem is that when you assign to $cinput, you're assigning a new array to it (you're not pushing a new value onto it).
I don't know the PHP function for adding an element to an array, but see if you can find that one.
As for the error message, that's because the $cinput variable only exists inside the clean_input function. You're returning the cleaned array, so in the main program you have to assign the result of clean_input to another variable in order to have access to it. Something like:
$output=clean_input($input);
print_r($output); This way, the return value of clean_input isn't lost.
blingbling!!
12-14-2003, 03:23 PM
PHP uses pass by value as default. When you say:
clean_input($input);
A copy is made of $input and passed to the funtion, so anything you do to $input inside the function has no effect outside it. You can 'simulate' pass by reference by altering your function definition with the & operator, like this:
clean_input(&$input);
hth.
Robin
bwkaz
12-14-2003, 07:29 PM
And then you'd get rid of anything referencing $cinput inside clean_input, and change the outer block to:
print_r($input); since $input was passed by reference. Right?
SuperHornet
12-14-2003, 08:29 PM
Thanks for both of your help.
I was able to figure it out, and here is the code.
<?php
$input= array
(
"fname" => $fname,
"lname" => $lname,
"uname" => $uname,
"password" => $password,
"title" => $title,
);
function clean_input ($value)
{
$value = strip_tags($value);
$value = htmlspecialchars($value);
$value= trim($value);
return $value;
}
while (list($key,$value) = each($input))
{
$cinput[$key] = clean_input($value);
}
print_r ($cinput);
?>
And returns
Array ( [fname] => John [lname] => smith [uname] => jsmith [password] => 1234 [title] => The Creator )
Coming from a perl backround and using sub routines, this function() thing is a bit foreign to me.
I guess you can't return a variable or what ever that was created inside the function. Only the varaible that you passed to the function can be returned. As I understand it :)
If this is the wrong train of thought, PLZ correct me.
blingbling!!
12-15-2003, 05:17 AM
I guess you can't return a variable or what ever that was created inside the function. Only the varaible that you passed to the function can be returned. As I understand it
OK then, consider yourself corrected!!! You can return anything you like, either a locally created variable, or even a 'constant' string value, for example;
return "blahblahblah";
is perfectly acceptable. Note that you don't HAVE to return a value from a function.
Robin
SuperHornet
12-15-2003, 12:05 PM
I am wondering why in my first post, when I did the
return $cinput;
I would get an error stating that it could not find the variable?
Notice: Undefined variable: cinput in check_input.php on line 25
blingbling!!
12-15-2003, 03:29 PM
I think it it to do with variable scope. By 'declaring' the $cinput variable inside a nested set of braces, the scope of that variable is within those braces, and when the code exits you loose the variable. try changing the code to this:
$input= array
(
"fname" => $fname,
"lname" => $lname,
"uname" => $uname,
"password" => $password,
"title" => $title,
);
function clean_input ($input)
{
var $cinput = "";
while (list($key,$value) = each($input))
{
$value = strip_tags($value);
$value = htmlspecialchars($value);
$value= trim($value);
$cinput= array($key => $value); //Typo in origional!!!!!!
}
print_r($cinput);
return $cinput;
}
clean_input($input);
print_r ($cinput);
Notice that i've 'declared' the $cinput variable outside the while loop braces, thus giving it the right 'scope' to be available to the return statement. Give this code a try and let me know if i'm right. (Note that i use wrote declared in inverted commas because you don't really declare variables in PHP. You should check out php.net and read about it. Just search for 'scope' on the online documentation).
hth.
Robin
(p.s. - i've just noticed there's a typo in your origional code, you refer to $vlaue - not $value. Maybe it's this that was causing the origional problem??!? I'm even more interested to know if my first guess in this post was right!)
SuperHornet
12-15-2003, 05:34 PM
Using your updated code
I was getting
Parse error: parse error, unexpected T_VAR in check_input2.php on line 24
BUT!
After reading your post, I think I understand.
and here is the code to reflect it:
$input= array
(
"fname" => $fname,
"lname" => $lname,
"uname" => $uname,
"password" => $password,
"title" => $title,
);
function clean_input ($input)
{
global $cinput;
while (list($key,$value) = each($input))
{
$value = strip_tags($value);
$value = htmlspecialchars($value);
$value= trim($value);
$cinput[$key]=$value;
}
return $cinput;
}
clean_input($input);
print_r($cinput);
And got
Array ( [fname] => John [lname] => Smith [uname] => jsmith [password] => [title] => the Creator )
Which meens it worked! Huray!
I'm not sure how that typo got in there.
I must of been correcting some formatting issues when I pasted the code into the textarea.
Thank you so much for your help.
blingbling!!
12-15-2003, 06:43 PM
Another bug squished - we like that! I'd thought of suggesting $cinput[$key]=$value, but that would have means too many p.s.'s :)
One more thing i neglected to mention, this construct you used in your code:
while (list($key,$value) = each($input))
{
//.........
}
(which i think is used in pearl) is equivalent to:
foreach($input as $key => $value)
{
//......
}
... and personally i prefer the readablility of the second (although i've got a suspicion it's not quite as efficent).
Regards
rch
bwkaz
12-15-2003, 07:48 PM
And as I said up above (:p), I suspect you can get rid of the global variable entirely by printing the direct result of the clean_input function.
Global variables are almost always bad, especially when there's an easy workaround like this:
print_r(clean_input($input)); or like what I had originally:
$output = clean_input($input);
print_r($output);