Login | Register | FAQ
Anonymous

i just have a simpel php question

+ Post a reply

4 posts Page 1 of 1


i just have a simpel php question

by clever » Fri Oct 14, 2011 9:33 pm

i wrote an simple php array



$months = "jan , feb , march , april , may"
$year = explode(",", $months);
echo "<p> months of the </p>";
echo "<ul>";
for ( $x=0; $x<=4; $x++) {
echo "<li>" . $year[$x] . "</li>";
}
echo "</ul>";
as you see looped the array
and the just for the sake of testing i did this


$months = "jan , feb , march , april , may";

$year = explode(",", $months);

echo "<p> months of the </p>";
echo "<ul>";
for ( $x=0; $x<=4; $x++) {
echo "<li> $year[$x] </li>";
}
echo "</ul>";

the code is the same but i changed echo "<li>" . $year[$x] . </li>"; to echo "<li> $year[$x] </li>"; and the result stayed the same
so my question is which is the right and preferred or any way is fine
any help will be appreciated
User avatar

clever

  • Posts: 20
  • Joined: Sat Oct 01, 2011 7:04 pm

Re: i just have a simpel php question

by HTML.net » Sat Oct 15, 2011 8:58 am

The first one (echo "<li>" . $year[$x] . "</li>";) is correct. The second one isn't.

- Andreas, HTML.net
User avatar

HTML.net
Site Admin

  • Posts: 1194
  • Joined: Sat Sep 11, 2010 11:24 am
--
Show some love for HTML.net on Twitter, Facebook and Google: Use the buttons on top of all pages.

Re: i just have a simpel php question

by clever » Mon Oct 17, 2011 10:07 pm

ok thanks but as i said in my php editor 2.22 the results do not change
but anyway thanks a lot for your answer
User avatar

clever

  • Posts: 20
  • Joined: Sat Oct 01, 2011 7:04 pm

Re: i just have a simpel php question

by atik » Tue Oct 18, 2011 5:09 am

As you wrote, both give the same answer.
But as php programmer echo "<li>" . $year[$x] . "</li>"; is more accurate than echo "<li> $year[$x] </li>";
It's because you can separate the string and the variable parts of the statement.
When using echo ""; be careful about using echo ''; both gives completely different answer for variables.
User avatar

atik

  • Posts: 471
  • Joined: Mon Oct 17, 2011 4:55 pm


+ Post a reply

Page 1 of 1