0

I wonder why in the world the first line of code:

echo('"'.$row['$Id'].'"');

results in output: "" while the second one:

echo('"');
echo($row['Id']);
echo('"');

Results in "71" which is exactly what I wanted to see...? I am sure that there is something simple but I don't know what is that.

hakre
  • 193,403
  • 52
  • 435
  • 836
Janusz Chudzynski
  • 2,700
  • 3
  • 33
  • 46
  • If you want to see what a variable contains, use `var_dump()`, not echo. Works for arrays, too. – hakre Oct 13 '11 at 15:05
  • Just as a side note, `echo`s are constructs not functions. You can perfectly have `echo '"' . $row['id'] . '"';` [PHP Manual - Echo](http://php.net/manual/en/function.echo.php) – acm Oct 13 '11 at 15:07

3 Answers3

7

In the first line you have a dollar symbol before Id, whereas in the second line it's just Id.

As both array indexes contain different values, the output is different.

Additionally I suggest that you enable error reporting to the highest level when you develop, as it will give you warning on common mistakes that can happen while typing code.

You can do this by adding the following two lines to the top of your script:

error_reporting(~0);
ini_set("display_errors", "1");

or by changing your PHP configuration.

JoLoCo
  • 1,355
  • 2
  • 13
  • 23
  • Beaten to it by Topener by 18 seconds! – JoLoCo Oct 13 '11 at 15:03
  • how come @hakre edited this, and this gets more upvotes while my answer actually has the needed code, instead of a description, making my answer actually better – Rene Pot Oct 13 '11 at 15:16
  • 2
    @Topener a code without description is not necessarily better than an explanation of what's wrong. As the old saying goes, "give a man a fish and you'll feed him for one day, teach him to fish and you'll feed him for a lifetime". PS I have no upvotes given in all these questions – Damien Pirsy Oct 13 '11 at 15:18
  • @Topener: Edited your answer as well, following your theory this should help ;) – hakre Oct 13 '11 at 15:27
  • @hakre: Interesting... both answers are running neck-and-neck. Can you come edit a few answers for me?! ;-) – Herbert Oct 13 '11 at 15:47
  • @Topener - it's a game of luck. Don't sweat it :) You win some, you lose some, you get rep anyway. :) – Vilx- Oct 13 '11 at 17:11
5

You need to remove the $ from your code:

echo '"'.$row['Id'].'"';

Or you need to $ add it:

echo('"');
echo($row['$Id']);
echo('"');

Depending of what you want to achieve.

hakre
  • 193,403
  • 52
  • 435
  • 836
Rene Pot
  • 24,681
  • 7
  • 68
  • 92
3
$row['$Id']
$row['Id']

Look carefully...

Vilx-
  • 104,512
  • 87
  • 279
  • 422