php for loop

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
a1234567
Forum Newbie
Posts: 1
Joined: Thu Feb 14, 2008 9:31 pm

php for loop

Post by a1234567 »

Hello,

How can this be written by using a for loop??

$name1 = $_POST['name1'];
$name2 = $_POST['name2'];
$name3 = $_POST['name3'];
$name4 = $_POST['name4'];
$name5 = $_POST['name5'];
$name6 = $_POST['name6'];
$name7 = $_POST['name7'];
$name8 = $_POST['name8'];
$name9 = $_POST['name9'];
$name10 = $_POST['name10'];

This might look easy just by replacing the numbers, but I cannot get it to work, please help
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: php for loop

Post by Christopher »

Code: Select all

extract($_POST);
// ot
for ($i=0; $i<=10; ++$i) {
    $name = 'name' . $i;
    $$name = $_POST[$name];
}
 
(#10850)
Post Reply