Printing arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Printing arrays

Post by hob_goblin »

in my script i have something like:

$num = 2;
$some_nums = ($_1, $_2, $_3);
$final_num = array_rand($some_nums, $num);

how would i print every single value of "$final_num"?
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

Code: Select all

foreach($array as $var) {
  echo "$var";
}
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Or, if you just need a quick and dirty display of the array:

var_dump() or print_r()
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

<?
if($whattodo == "generate")&#123;
echo "doing your work...";
function rsl()&#123;
$randomsnum = mt_rand(97, 122);
$schar = chr($randomsnum);
return($schar);
&#125;

function rbl()&#123;
$randomlnum = mt_rand(65, 90);
$bchar = chr($randomlnum);
return($nchar);
&#125;

function rsn()&#123;
$randomnnum = mt_rand(48, 57);
$nchar = chr($randomnnum);
return($nchar);
&#125;

$_1 = rsl();
$_2 = rbl();
$_3 = rsn();
$_4 = rsl();
$_5 = rbl();
$_6 = rsn();
$_7 = rsl();
$_8 = rbl();
$_9 = rsn();
$_10 = rsl();
$_11 = rbl();
$_12 = rsn();
$_13 = rsl();
$_14 = rbl();
$_15 = rsn();
$_16 = rsl();

$final_set = array($_2, $_3, $_4, $_5, $_6, $_7, $_8, $_9, $_10, $_11, $_12, $_13, $_14, $_15, $_16);
$num_final = $num - 1;
$final_num = array_rand($final_set, $num_final);

foreach($final_num as $var) &#123; 
  echo "$var"; 
&#125;






&#125; else &#123;
echo "Type in chars:";
echo "<form method=POST action="$PHP_SELF"><input type=hidden name=whattodo value=generate><input type=text name=num><br>";
echo "<input type=submit value=submit></form>";
&#125;
?>

basically what i want to do is generate a password... that the user inputs how many characters.. and it generates it, random letters, and numbers. Mixed case.

After that I will use it with another script im writing..
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

Code: Select all

<?
if($submit) &#123;
  $pass = "";
  $az09 = "abcdefghijklmnopqrstuvwxyz";
  $az09 .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  $az09 .= "0123456789";

  while(strlen($pass) < $num) &#123;
    mt_srand((double) microtime() * 1000000);
    $pass .= substr($az09, mt_rand(0, strlen($az09) - 1), 1);
  &#125;

  echo "$pass";
&#125; else &#123;
  echo "Type in chars:";
  echo "<form method="POST" action="$PHP_SELF">";
  echo "<input type="text" name="num"><br>";
  echo "<input type="submit" name="submit" value="submit">";
  echo "</form>";
&#125;
?>
might be another solution.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

wow, thanks ALOT, that worked instantly...and did everything I wanted. except with about 1/3 of the coding i was going to write out.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

http://www.ice-on-fire.net/dpinclu.php incase you want to see the finished product. :D
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

nice ;]
diehuman
Forum Newbie
Posts: 5
Joined: Tue Jun 17, 2003 4:09 pm
Location: Kalispell, MT

Post by diehuman »

That is the coolest 404 page I have ever seen, haha.
Post Reply