|Variables, I just don't get them|
when do I use which type of global variable
I posted about variables on another thread and got excellent replies that got me through one problem. I am now stuck on another, and I think if I new a little (lot) more about the logic behind how and why they are used I would be better off.
I'm running win32/apache 2.0.4/php4.2.2 global variables=off. If it matters I am running the scripts on a locally, but will eventually be uploading everything to a server.
The tutorials that I am using are assumming global variables=on, so I am trying to translate. I've been going through several websites (including php.net) to try to figure this out, but everytime I think I have it down I get new errors. My latest two are "undefined Index" and "Undefined Variable" inside a text box.
The first one...
The tutorial that I am using is showing us how to view environment variables. The original script reads:
echo "<BR><B>Browser</B> $HTTP_USER_AGENT";, etc. through various variables. Through hit and miss and different tactics, I came up with this:
echo "<BR><B> Browser:<B>" . $_ENV['HTTP_USER_AGENT'];
echo "<BR><B> Host:<B>" . $_ENV['HTTP_HOST'];
echo "<BR><B> Referer:<B>" . $_ENV['HTTP_REFERER'];
echo "<BR><B> Remote Host:<B>" . $_ENV['REMOTE_HOST'];
echo "<BR><B> Remote Address:<B>" . $_ENV['REMOTE_ADDR'];
echo "<BR><B> Remote Port:<B>" . $_ENV['REMOTE_PORT'];
I get everything right except for Remote Host which is the Undefined index error. Another question on this. If I do phpinfo() The names of the variable names are _Server["HTTP_REFERER"], etc. how do we translate that into what we want to see? What is the difference, for example, between $_GET and $HTTP_GET_VARS?
My second script, I don't even now how to begin to fix. The code is:
<TD><INPUT TYPE="TEXT" NAME="OP1" VALUE="<?echo $OP1; ?>"</TD>
<TR><TD><INPUT TYPE="TEXT" NAME="OP2"
VALUE="<? echo $OP2; ?>"</TD>
<TD ALIGN="CENTER"><INPUT TYPE="SUBMIT" VALUE="="></TD>
<TD><INPUT TYPE="TEXT" NAME="RESULT" VALUE="<? echo $OP1 + $OP2; ?>"
The error messages (Undefined variables) appear in the text boxes.
I am more interested in the "inner workings" of variables, than quick code to fix the problems. Any help supplied will be greatly appreciated.
My best guess at for you to get a better understanding would be to read PHP: Variables - Manual [php.net]
The problem your having in your script is your not defining $OP1 $OP2, with out a definition they dont know what the value of it is. You have to define it before you call it unless its a global varible which is predefined. So basically somewhere you have to say $OP1 = "OP1"; and $OP2 = "OP2"; as you know there are different ways for doing that.
Hope this helped,
I'll start with the link where most of this info came from
There a lot of different vars of similar format
$_GET and $HTTP_GET_VARS
$_POST and $HTTP_POST_VARS
$_ENV and $HTTP_ENV_VARS
$_SERVER and $HTTP_SERVER_VARS
$_SESSION and $HTTP_SESSION_VARS
the $_ types were all introduced in 4.1.0, all earlier version must use their $HTTP counterparts.
From the above link in re ENV
$HTTP_ENV_VARS contains the same initial information (as $_ENV), but is not an autoglobal. (Note that HTTP_ENV_VARS and $_ENV are different variables and that PHP handles them as such)
I actually couldn't find anything about $REMOTE_HOST I know that you can do it a different way.
$host = gethostbyaddr($_ENV['REMOTE_ADDR']);
Where do $OP1 and $OP2 come from? Are they set higher up in the script? or posted? or something else?
I will give you a little trick. When you just want to echo a var value you can do
<?= $OP1; ?>
<?echo $OP1; ?>
On first looking at it I don't see any reason why it wouldn't work as is.
I didn't understand this
>>how do we translate that into what we want to see?
OK. So, I think I'm on the verge of figuring this out.
In my original post (PHP Form Handler page 2). I tried $_GET and it didn't work; however, $HTTP_GET_VARS did. In this case, $_ENV did work and $HTTP_ENV_VARS didn't. So what I think I am seeing is that when calling global variables from another source (ie in the first example from an html page), I use $HTTP_*_VARS. When calling global variables from within the same source I need to use $_*. Is this at all close to the truth?
For the second code, that's the whole script. The script is supposed to bring up 3 text boxs, a "+" sign and an "=" button. The user enters numbers into the first 2 boxes, presses = and the answer is supposed to appear in the last box. So I'm guessing that $OP1 and $OP2 are supposed to be globals. Is this where $GLOBALS or globals come in?
I am not sure what you mean on the first part of the post, I am sure someone can answer that.
On the second question, no those would not be global variables. Global variables are predefined. You have never defined $OP1 & 2, so this is the reason for your error of undefined variable. Where you are echoing them in the first two text boxes you shouldnt be because thats where they get defined, the value portion of that box means what ever is there will display in the box value=44 would mean the text box would show 44. If you have a preset number you expect to use for $OP1 & 2 then place that as a value and if the user wants to change it they can since its a text box. Then when they hit the submit (or equal) button it will assign name=OP1 to $OP1 and the same for OP2. This will cause the action you are wanting to happen to happen in the 3rd text box.
I've just started to made my move to Register_Globals=off and it's been quite a nightmare!
I use PHP 4.2.2 and the $_* format to get my variables.
Mapgeek: I use $_SERVER to get the variables you ask for with $_ENV. Honestly, I don't know what is the difference, but it works for me.
As for your code, I believe that you are missing a <form> tag.
If you do, then (depending on the method) you should get your variables from $_GET or $_POST.
As for why sometimes $HTTP_*_VARS works and sometimes $_*, I don't know.
If you are using PHP 4.1.0+ (and you say you are), using $_* should be enough.
But then again, I'm starting on this stuff myself :)
I lied. That wasn't the whole code. There is a <FORM> tag.
<FORM METHOD="POST" ACTION="p-5-1.php">
The ACTION is posting back into itself, meaning the file name of this script is the same as the ACTION.
I tried using both $_POST and $HTTP_POST_VARS. Neither worked.
I understand that the variables need to be defined before they are called. It's just that this guy did it this way in 3 exerceses, with the objective of showing how php statments can process data entered onto the form. So, I'm thinking there probably is a way to do it. I'm probably beating a dead horse because I don't know if I'll actually need to do this, but I am trying to get my mind wrapped around this.
Ok I am getting confused I think. What does this form do? It sounds like a caculator to me which is where my last post was thinking. Also maybe what is the tutorial your reading maybe that would help me get an understanding of what you are doing.
This is getting a little confusing, I tested that little page. I added html, head, title, body, the form tag before the table tag and the closing form tag agter the close table.
It works perfectly. I seem to be running php 4.0.6 with register_globals on.
with out register_globals on you could throw the values into vars.
$OP1 = $HTTP_POST_VARS["OP1"];
$OP2 = $HTTP_POST_VARS["OP2"];
and then use all the same form code.
I really don't understand why the $_ and $HTTP_ are sometimes working or not. My only thought is conifguration. Not really sure though.
Well, it is a calculator. But using forms to show how to process data. I had a feeling it had something to do with globals on. I added the new variables, but to no avail. I used both $_ and $HTTP. The only difference is that have have a zero in the last text box.
Back to the $_* and $HTTP_*_VARS question, is there any rule of thumb in deciding which variable to use besides trying one and then seeing if it works?
before version 4.1.0 the $HTTP_ style. Later versions support both so it would depend on whether you want an autoglobal style ($_) or not($HTTP_).
oh, I forgot to tell you. I am working out of PHP4 A Beginner's Guide by Bill McCarty. Project 5-1.
|<?= $OP1; ?> |
<?echo $OP1; ?>
Shouldn't it be:
<? = $OP1; ?>
<? echo $OP1; ?>
with a space after and before the "<? " & " ?>"
from php.net on echo [php.net]
|echo() also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. |