a simple question

This is where you share YOUR scripts with others

Moderators: macek, egami, gesf

hliostasio
New php-forum User
New php-forum User
Posts: 2
Joined: Thu Aug 23, 2012 2:10 am

a simple question

Postby hliostasio » Thu Aug 23, 2012 2:17 am

Hi, I am new to php and I have a question on assossiative arrays maded with loop.
I have written this code:

for ($j=0; $j<4; $j++)
{
$given=array( //S O S
"$j" => $_POST{"answer$j"});
echo "answer to".$j ."=> ". $given[$j] . "<br />";
} //until hera it prints the given answers

echo "<h1>SOSapanthsh sthn 0=> ". $given[0] . "<br /></h1>"; //but here ..prints a notice

Where I have made mistake?
Thank you !

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: a simple question

Postby egami » Thu Aug 23, 2012 1:15 pm

Your Code...

Code: Select all


for 
($j=0; $j<4; $j++)
 {
 $given=array( //S O S 
"$j" => $_POST{"answer$j"}); 
echo 
"answer to".$j ."=> ". $given[$j] . "<br />";
 } //until hera it prints the given answers

 echo "<h1>SOSapanthsh sthn 0=> ". $given[0] . "<br /></h1>"; //but here ..prints a notice
 



Try this..

Code: Select all



$i 
= 4;
for ($j = 0; $j < $i; $j++) { 
$given
[$j] = $_POST['answer'.$j];
echo "Answer to $j => $given[$j]<br>";
}

echo '<h1>SOSapanthsh sthn 0=> '.$given['0'].'</h1><br>';

echo '<pre>'; print_r($given); echo '</pre>';
 

hliostasio
New php-forum User
New php-forum User
Posts: 2
Joined: Thu Aug 23, 2012 2:10 am

Re: a simple question

Postby hliostasio » Sun Aug 26, 2012 3:06 am

Thank you for answering me!


Return to “Your Scripts”

Who is online

Users browsing this forum: No registered users and 2 guests