Pablo Medina
Pablo Medina

Reputation: 48

PHP - same variable showing different values

I am reading some rows from mysql to create a group of radio buttons. I need to write the name of the player two times, first as a value attribute and second the second time as a text. I don't understand why the same code ($players[$x]) produces two different values in the output html. This is the php code:

$players = readSqlFromMySql("select name from players");        
for($x = 0; $x < count($players); $x++) {                     
    echo('<input type="radio" name="radio-choice-v-2" value="' . 
    $players[$x] . '" id="name-choice-' . chr(66 + $x) . '" ><label 
    for="name-choice-' . chr(66 + $x) . '">' . $players[$x] . '</label>');
} 

This is the rendered output html:

<input type="radio" name="radio-choice-v-2" value="<td style='width:150px;border:1px solid black;'>Amie</td>" id="name-choice-B">

So the first time, the variable's output is "td style=...", wheareas the second time it shows the expected value ("Amie"). What's going on? Is it php? jquery? jquerymobile? I am using XAMPP.

Thanks

Upvotes: 0

Views: 88

Answers (2)

Theraot
Theraot

Reputation: 40150


Original Answer

Warning: this answer is wrong on account of a recent comments. Regardless, this answer explains a way in which the described phenomenon could happen.


What you describe is possible if $players is an object instead of an array.

If $players is an object of a class that implements Countable then count($players) is equivalent to calling the method $players->Count`.

Futhermore, if the class also implements ArrayAccess you can access it like an array. That is $players[$x] will be equivalent to $players->offsetGet($x). And the implementation of that method could do whatever, including returning different values on each call.


Try the following ot see if it is in fact some custom class:

echo get_class($players);

Also try the following:

var_dump($players);

You can debate the merits of print_r vs var_dump another day.


New Answer

In comment you say:

$players is an Array, $players[$x] is a String, both expected. However, var_dump returns "string(57) Amie". String(57) is the length of the unexpected code I got.

If you go back to your code, you placed $players[$x] inside of the attribute value. And what we found inside of value in the rendered out put is: <td style='width:150px;border:1px solid black;'>Amie</td>. That seems to be what you get from $players[$x].

If I put in my code:

<?php var_dump("<td style='width:150px;border:1px solid black;'>Amie</td>");

This is what I see in the browser:

string(57) "Amie"

But if I look at the source code of the page in the browser, it reads:

string(57) "<td style='width:150px;border:1px solid black;'>Amie</td>"

That is what is happening there. The value of Amie is actually "<td style='width:150px;border:1px solid black;'>Amie</td>" but you do not see in the browser.

Upvotes: 0

Kamrul Khan
Kamrul Khan

Reputation: 3350

The value of $players[$x] is <td style='width:150px;border:1px solid black;'>Amie</td> and thats what is getting printed. The second $players[$x] is within the label tag that probably you did not notice.

You probably tried printing $player[$x] on the browser thats why you did not see the td tag around it. Please check the source of your browser output and see whats in there.

Also <td style='width:150px;border:1px solid black;'>Amie</td> is exactly 57 char long; which matches your print_r output.

Upvotes: 2

Related Questions