GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Jul-2005, 17:26
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,108
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

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:

<?php
  if( $_POST ){
  $number_sent = count( $_POST );
  $my_message = $number_sent;
#  $my_array[$number_sent];
  do{
    $my_array[$i] = current($_POST);
    next($_POST);
    echo $my_array[$i];
    echo "<br>";
  }while(current($_POST));
  
  echo $my_array[0];
  echo "<br>";
  echo $my_array[1];
  echo "<br>";
  }
  else{
    $my_message = 'error';
  }
?>



<html>
<body>
</body>
  <form name="newform" method="post" action="index.html">
    <p>Message
      <input name="message" type="text" value="<?php echo $my_message; ?>" size=40 maxlength="400" />
    </p>
      <input type="submit" name="goback_but" value="Go Back" />
    </p>
  </form>
</html>

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. Any thoughts on the matter would be most welcome.

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:
Hello World // from the loop Goodbye World // from the loop // outside the loop from last <br> in the loop // outside the loop last <br> moves Message down Message (text box shows '2' for the named <input>'s)

Code from index.html
HTML Code:
<form action="testing.php" method="post"> <p> Test 1 : <input name="test1" /> Test 2 : <input name="test2" /> <input type="submit" value="Submit" > </p> </form>

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 ) to see what happens.
__________________
"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  
Old 28-Jul-2005, 17:40
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Quote:
Originally Posted by cable_guy_67
..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?...
Yes

Quote:
Originally Posted by cable_guy_67
...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).
Try adding this line to the top of your script:

PHP Code:

<?php
error_reporting( E_ALL );
// your supplied code...
?>

  #3  
Old 28-Jul-2005, 17:50
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,108
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by JdS
Yes
Try adding this line to the top of your script:

PHP Code:

<?php
error_reporting( E_ALL );
// your supplied code...
?>


THANK YOU!
Now I get something more like I anticipated,
Code:
Notice: Undefined variable: i in C:\mytest\testing.php on line 10 Notice: Undefined variable: i in C:\mytest\testing.php on line 12 Hello Notice: Undefined variable: i in C:\mytest\testing.php on line 10 Notice: Undefined variable: i in C:\mytest\testing.php on line 12 World Notice: Undefined offset: 0 in C:\mytest\testing.php on line 16 Notice: Undefined offset: 1 in C:\mytest\testing.php on line 18

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  
Old 28-Jul-2005, 17:53
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Quote:
Originally Posted by cable_guy_67
...Just because I can get away with something surely doesn't mean I should...

EXACTLY... don't lose your good habits now. I always "define" my variables before using them, at least most of the time
  #5  
Old 28-Jul-2005, 19:18
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,108
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
I was just looking this code over and I realized when I pulled the for...

PHP Code:

#  for($i = 0; $i < $number_sent; $i++){ 



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  
Old 29-Jul-2005, 16:23
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,108
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
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:
$DefPercent + (((100 - $DefPercent) / $OtherIDs) * $OtherIDs )

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:

$DefPercent + ((int)((100 - $DefPercent) / $OtherIDs) * $OtherIDs ) 



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  
Old 29-Jul-2005, 18:25
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
You might also find floor() and similar functions useful in situations like this.
  #8  
Old 29-Jul-2005, 19:22
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,108
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by JdS
You might also find floor() and similar functions useful in situations like this.

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:
Originally Posted by PHP manual
Returns the next lowest integer value by rounding down value if necessary. The return value of floor() is still of type float because the value range of float is usually bigger than that of integer.

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 GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 18:09.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.