array problem

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
MeHigh
Forum Newbie
Posts: 13
Joined: Mon Jun 30, 2003 9:31 am

array problem

Post by MeHigh »

I have this code:

Code: Select all

<?php
<?php
$limbaje = array(0=>"Perl", 1=>"PHP", 2=>"Python");
$limita = count($limbaje);
for ($i = 0; $i < $limita; $i++)
{
echo "<br>$i => $limbaje[i]";
}
?>
?>
It was suppose to print something like

Code: Select all

0=&gt;Perl
1=&gt;PHP
2=&gt;Python
but all it does is:

Code: Select all

0=&gt;
1=&gt;
2=&gt;
Is it becouse the Register_globals option in php.ini is off and if so, how can make it print the variables without turning the register_globals option ON?

Many thanks in advance
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

Code: Select all

<?php 
$limbaje = array(0=>"Perl", 1=>"PHP", 2=>"Python"); 
$limita = count($limbaje); 
for ($i = 0; $i < $limita; $i++) 
&#123; 
echo "<br>$i => $limbaje&#1111;$i]"; 
&#125; 
?>
you missed the $ off the I variable name in the echo.
User avatar
MeHigh
Forum Newbie
Posts: 13
Joined: Mon Jun 30, 2003 9:31 am

Post by MeHigh »

thanx a lot
ik
Forum Commoner
Posts: 34
Joined: Thu Jul 10, 2003 5:33 am
Location: Lancs Uni, UK
Contact:

Post by ik »

There are more simple ways:

foreach($smth as $key=>$val)
echo '<br>['.$key.']=>'.$val;

Or just use print_r(), as it described in PHP manual:
<pre>
<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x','y','z'));
print_r ($a);
?>
</pre>
Post Reply