Forum Moderators: coopster
I think that this could be easy, but my brain cramps up part way and I can't seem to get it right (I'm in the middle of a marathon coding/programming session).
I have a result set:
$query = "SELECT * FROM mytable WHERE code = 'topsecret!'";
$result = mysql_query($query);
Now I want to loop thru the resulting $row's and apply the htmlspecialchars() function. My brain cramps right about at the point where I start to build the foreach. Can someone walk me through this?
while ($row = mysql_fetch_array($result)) {
foreach ($row as $key => $val) {
CRAMP!
}
missing a closing brace
Details..
Basically, I end up with 7 values '$row[1] = string -> $row[7] = string' for each result.
Right now I had been plugging the $row[x] results into a while loop containing the markup to present search results.
Unfortunately there are a few special characters in the db (out of my control) that I need to be careful of...
So, what I would like to do is clean up the 'bad' entities before passing the values to the html-creating while loop...
I'm melting here. I'm at the stage where I can see that array_walk should work, but damn if I can build the right function to walk my array thru...
Ha!
I knew that by typing up this message I'd get it to work. I Think I faked out the 'powers that be', so to speak.
Anyway, why the '&' in
function test_alter(&$item1, $key, $prefix)
{
$item1 = "$prefix: $item1";
}
from the example in the link I posted?
does this question expose me as the rookie I am?
Think variable scope. In PHP variables within a function are not accessble outsie of that function. So,
function($data)
{
do something with $data
}
contains the variable $data. You can call it via
function($id);
and internally it will treat $id as $data.
To get that $data back you will either need to return it (i.e. "return $data;"), pass a variable that was declared global, i.e.
$id = "asdf";
global $id;
function($id);
or, pass the variable by reference. To do that you put the ampersand in front of the variable in the fucntion declaration.
function(&$data);
Now the function will operate on the variable stored in memory rather than the variable created when the function is called.
Clear as mud?
WBF
Clear as mud?
Uhhh-huh. Yep. No, really, I was reading those Zend 'rock solid php' (or something like that) and they go into variable variables, I believe. So that may have helped me to understand what you're spelling out. Thanks!
And thanks to both of you - in the end a very nice solution. There are so many functions in PHP that I just don't know...
Why the &?Close, but not exactly. While issues of variable scope are relavent, the amperstand has no effect on scope. With it, two different variable can access the same memory location--not just the same value. Variable scope deals with variable names and where they are valid. Two variables can have the same name, but because of scope, are two compleatly different variables, each with it's own value and memory location. With the amperstand you have two names for the same variable.
Think variable scope. In PHP variables within a function are not accessble outsie of that function.
internally it will treat $id as $data.Not at all. It mearly copies the value of $id and stores it in $data. Any changes to $data are lost when $data is destroyed upon function termination.
To get that $data back you will either need to return it (i.e. "return $data;"), pass a variable that was declared global,All a
return does is copy an evaluated expression (thereby "returning" it) and terminate the function pass control back to the caller. A global variable is one with global scope--it can be referrenced anywhere. By passing a global as a paramater to a function you are making a copy of it's value and assigning it to the local variable in the same possion in the function's declaired argument list. (I'm ignoring the feature of variable length argument lists to keep things simple.) Unless you are calling by reference (with the amperstand), there is no way to get $data back; it will be destroyed upon function termination. What you can do is return a copy of it's value. The difference is very significant. function(&$data);Close enough. It's not exactly correct, but the difference is splitting hairs.Now the function will operate on the variable stored in memory rather than the variable created when the function is called.
Clear as mud?Sorry WBF, but here you are 100% correct.
Well put.Clear as mud?
Uhhh-huh. Yep. No,
variable variables..may have helped me to understand what you're spelling out.I was thinking along the same lines. In case there is confusion about variable variables:
$color = "red";
$apple = "color";
echo $$apple; // Will display: [red]red[/red]
With it, two different variable can access the same memory location--not just the same value.
Yes, it effectively sets a pointer to that memory location and operations are then conducted on the value stored in that location.
Variable scope deals with variable names and where they are valid. Two variables can have the same name, but because of scope, are two compleatly different variables, each with it's own value and memory location. With the amperstand you have two names for the same variable.
Or, two variable names pointing to the same memory location, where the value under consideration is stored.
Any changes to $data are lost when $data is destroyed upon function termination.
Which is why one must have a way to "retrieve" $data from the function if one wants to continue to use its value. Perhaps a better way of saying this is one must "preserve" the value of $data for later use.
All a return does is copy an evaluated expression (thereby "returning" it) and terminate the function pass control back to the caller.
True enough. My intent was to suggest that one could use the syntax "$id = function($id);" as a way to change/preserve the value of $id.
A global variable is one with global scope--it can be referrenced anywhere. By passing a global as a paramater to a function you are making a copy of it's value and assigning it to the local variable in the same possion in the function's declaired argument list.
But, if one declares a variable global within a function. it can then be referenced outside of that function. Another way to preserve the value generated within the function.
Unless you are calling by reference (with the amperstand), there is no way to get $data back; it will be destroyed upon function termination. What you can do is return a copy of it's value. The difference is very significant.
But, if what one wants is to preserve the new value of $data, there are three ways to do so. 1) Pass by reference, 2) assign via a return statement, or 3) declare the variable as global.
but the difference is splitting hairs.
Well said indeed!
Variable scope has cost countless hours of headscratching and fist pounding. My goal was simply to explain that one needs to understand that what takes place in a function is not going to show up outside of that function unless one takes steps to make it so.
WBF