![]() |
|
#1
|
||||
|
||||
Variable usage - how loose is it?Hello All,
I have recently started dabbling in PHP and it seems that variables can be used/declared in quite a hodge podge manner. Perhaps I am missing something but why does this piece of code even work? testing.php PHP Code:
Before after actually indexing into $my_array I make no mention of it. Yet it works as anticipated otherwise. I origonally thought that I would have to use the $number_sent variable to declare $my_array but this does not seem to be the case. Am I to assume that I can just keep indexing the array at will? Scoping seems to be correct (there is no output outside the do ... while construct) but there does not seem to be an adverse effect to attempting to read into a non-existant array (outside the do ... while). Coming from C++ to this seems, well, a little confusing. Mark Oh, the form that I come from has two named <input>'s of type text and a submit button (that is unnamed) giving me a value of 2 for number sent. I had played with both a for and do while getting to this point. That's why the array indexing outside the loop looks for [0] and [1]. Output: Code:
Code from index.html HTML Code:
One last thing. The reason for the sparseness of the tags is I have been playing with the form class from the code section and have been testing default behavior against the documentation I have. Most likely not safe in a global sense but I like to see how things work by pushing on them (you know, with a hammer __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#2
|
||||
|
||||
|
Quote:
![]() Quote:
PHP Code:
__________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#3
|
||||
|
||||
|
Quote:
THANK YOU! Now I get something more like I anticipated, Code:
That's more like it. Seems to be more like -Wall in g++ would give me. Just because I can get away with something surely doesn't mean I should. Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#4
|
||||
|
||||
|
Quote:
EXACTLY... don't lose your good habits now. I always "define" my variables before using them, at least most of the time ![]() __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#5
|
||||
|
||||
|
I was just looking this code over and I realized when I pulled the for...
PHP Code:
out, I never dealt with incrementing $i. Lord have Mercy, I will definately be using the error reporting from now on. Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#6
|
||||
|
||||
|
Well, after another day of fighting with types (or actually lack thereof) it seems that there is just no good way to make a variable act like I would like them to. I had high hopes with settype() but that still will promote an int to a float for something like,
Code:
The variables all get passed as arguments and I tried to con them into acting like integers to no avail. The division promotes to a float and messes up my integer math. The only solution I can come up with is, PHP Code:
What a drag. Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#7
|
||||
|
||||
|
You might also find floor() and similar functions useful in situations like this.
__________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#8
|
||||
|
||||
|
Quote:
My problem is just the extreme flexiblity of it all. I like integer math for things like this. floor() will still promote to a float. Quote:
It would be nice if the language allowed for proper declaration of variables. Doing it with a cast like I did effectively does do what I want. Just something I am going to have to keep my eye on. Dratted C++ getting me used to being specific. I was just getting to the point that I used constants and unsigned as a mechanism to keep numbers in check and not using globals (or very, very rarely). PHP's constants are all global in scope. I guess it has as much to do with the way the language is used though. Thanks for the tip J. Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
Recent GIDBlog
NARMY by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Tutorial] Pointers in C (Part II) | Stack Overflow | C Programming Language | 0 | 27-Apr-2005 17:36 |
| [Tutorial] Pointers in C (Part I) | Stack Overflow | C Programming Language | 1 | 08-Apr-2005 18:35 |
| making string data into a variable | dopee | MySQL / PHP Forum | 0 | 25-Oct-2004 04:30 |
| PHP/Apache memory usage issue | bacchus | Apache Web Server Forum | 0 | 18-Aug-2003 12:57 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The