-1

As a beginner I know how to work with basic for and foreach loops in PHP. Most of the time we prefer foreach for arrays. I am asking this just for the sake of insight into this "for loop" behavior and how it varies compared with javascript. The same loop in Javascript gives us results. (Experts feel the question is insane pls don't downvote it)

Javascript

var arrayList = ["php", "javascript", "c++", "C lang", "Python"];
var text = "";
for(var i = 0; i < arrayList.length; i++) 
{
   text += arrayList[i] + "<br>";   
}
 document.write(text);

PHP-I know it will work if I put the variable in the loop, but why not in PHP

$_array = array("PHP", "Javascript", "C++", "Jquery", "Python");
$total = count($_array);
$result = "";
for($i = 0; $ i <$total; $i++)
{
 $result += $_array[$i]."<br>";
 }
echo $result;
Mithun Jack
  • 171
  • 8
  • 2
    this extra space `$ i` is just a typo here, right? – Jeff Sep 01 '18 at 09:36
  • 3
    In PHP, strings are added (as you show) using `.` rather than `+`. This also applies to `+=` should be `.=` for PHP. – Nigel Ren Sep 01 '18 at 09:37
  • They are completely different languages, so why would they have to work the same way? – Andreas Sep 01 '18 at 09:38
  • That not at all problem. I just type here like that. When u correct and test that, it gives 0 – Mithun Jack Sep 01 '18 at 09:38
  • adding to Nigels answer: when using the + operator php will try to get the intval of the string. If you put "5php" and "4Javascript" in the array the result will be 9 – Jeff Sep 01 '18 at 09:39
  • Javascript and PHP have different concatenation operator. JS uses "+" but PHP uses "." – hanish singla Sep 01 '18 at 09:40
  • @Andreas But this is a basic loop. Is it wrong for the beginner expect to go everything right when he starts? That's my doubt.That's it – Mithun Jack Sep 01 '18 at 09:40
  • 1
    It's like asking "Why do I have to move my feeds differently when riding a bike or a car. They both bring me from A to B.". The question doesn't make much sense as they are two completely different "things" as are JavaScript and PHP. They will both have a way to iterate over a collection but they don't have to work the exact same way. – Andreas Sep 01 '18 at 09:44
  • @Andreas , Qirel and everyone What confused me in javascript is I thought it was a increment operator (+). That's why I tried it in PHP. And now I know "+" used in the meaning of concatenation, not as increment. Thank you. Thanks a lot everyone – Mithun Jack Sep 01 '18 at 10:03

2 Answers2

1

There are few corrections here:

<?php /* missing */
$_array = array("PHP", "Javascript", "C++", "Jquery", "Python");
$total = count($_array);
$result = "";
for($i = 0; $i <$total; $i++) /* remove extra space between $<space>i */
{
 $result .= $_array[$i]."<br>"; /* replace + with . */
}
echo $result;

BTW, php has implode() which does the same:

<?php
$result = implode("<br/>",array("PHP", "Javascript", "C++", "Jquery", "Python"));
echo $result;

Equivalent short code for JS:

var text = ["php", "javascript", "c++", "C lang", "Python"].join("<br>");
document.write(text);
0

In PHP concatenation operator is DOT ("."), not PLUS ("+") character:

WRONG

$result += $_array[$i]."<br>";

RIGHT

$result .= $_array[$i]."<br>";