-2

I need a clear explanation.

Take a look at the code

<?php
$user_id = $_GET['user_id'];                  
include "../database.php"; 
$query="SELECT name FROM user WHERE user_id='$user_id'";
$result=mysqli_query ($connect, $query);

while($data = mysqli_fetch_array ($result))
{
    $name=$data['name'];
    echo"<tr><td>$name</td></tr>";
}                   

?>

When i change into this one, the code still working. .

echo"<tr><td>".$data['name']."</td></tr>";

But, when i change into this one, it is not working. .

echo"<tr><td>$data['name']</td></tr>";

Do the way I use " and . matter?

Bluetree
  • 1,324
  • 2
  • 8
  • 25
Arcfield
  • 19
  • 6

1 Answers1

5

You can also write it as

 echo "<tr><td>{$data['name']}</td></tr>";

or

echo "<tr><td>$data[name]</td></tr>";

or (fun fact not too many people know about):

echo '<tr><td>', $data['name'], '</td></tr>';

That's just PHP syntax for you. Read about strings on the official site.

. is for concatenation. Double quotes support "interpolation" but you have to use it correctly. If you want to access an array index, either put {} around the variable, or drop the ' (only works one-level deep I believe).

That last syntax is special for echo. echo isn't a function, so they're not really "arguments" but it will let you output multiple things by separating them with commas nonetheless.

mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • 1
    I'm sure you can do better than this. – Funk Forty Niner Dec 15 '17 at 03:23
  • 1
    @Fred-ii- Gotta post it quick for that sweet-sweet karma and then edit it with further details. That's the SO game; otherwise you get buried! Don't hate the playa. – mpen Dec 15 '17 at 03:25
  • 1
    I'm sure your wouldn't have gone unnoticed. I've seen posts with many answers where one was given minutes later only to be the proof that that person took the time to write a robust and clear answer, as opposed to dropped in code with a minimal amount of what it actually does. – Funk Forty Niner Dec 15 '17 at 03:27
  • 1
    i don't know what you guys talking about, but hey..Thanks for answering :D hehehe – Arcfield Dec 15 '17 at 03:30
  • *"i don't know what you guys talking about"* - @Arcfield it's about giving a clear explanation in order for people to actually "understand" the mechanics of (php) coding. If you don't know how a powersaw works, then you shouldn't work with it until you do ;-) – Funk Forty Niner Dec 15 '17 at 03:32