oldboy
oldboy

Reputation: 5954

Why does the value of this array element resolve to zero?

I'm working on pagination for my website, yet I'm stuck on the following piece of code. I've been messing around with it for at least an hour, and can't seem to wrap my head around what's going on with the output.

Of course the if statement executes once.

And as expected the first echo ... returns 1.

However, for some reason the second echo ... returns 0 as a float instead of <div>1</div> as a string...

$rowCount = 5;

$pgCount = ceil($rowCount / 10);

$pgParamArray["page"] = $pgCount;

$pgArray = array("", "", "", "", "");

for ($i = 0; $i < 5; $i++) {

  if ($pgParamArray["page"] - $i > 0) {

    echo $pgParamArray["page"] - $i;

    $pgArray[$i] = "<div>" . $pgParamArray["page"] - $i . "</div>";

    echo $pgArray[$i];

  }

}

I have absolutely zero idea why $pgArray[0] returns 0, yet var_dump($pgArray) returns array(5) { [0]=> string(7) .... That makes zero sense to me. Anybody know why $pgArray[0] is resolving to 0?

Upvotes: 2

Views: 57

Answers (2)

Tim Morton
Tim Morton

Reputation: 2644

You need to group the arithmetic part as mentioned in the accepted answer:
$pgArray[$i] = "<div>" . ($pgParamArray["page"] - $i) . "</div>";

To understand what's going on here, you need to run it on the command line:

php > $rowCount = 5;
php > 
php > $pgCount = ceil($rowCount / 10);
php > 
php > $pgParamArray["page"] = $pgCount;
php > 
php > $pgArray = array("", "", "", "", "");
php > 
php > for ($i = 0; $i < 5; $i++) {
php { 
php {   if ($pgParamArray["page"] - $i > 0) {
php { 
php {     echo $pgParamArray["page"] - $i;
php { 
php {     $pgArray[$i] = "<div>" . $pgParamArray["page"] - $i . "</div>";
php { 
php {     echo $pgArray[$i];
php { 
php {   }
php { 
php { }
10</div>
php > 

As you can see, the output of echo $pgParamArray["page"] - $i; is 1, immediately followed by 0</div> as the contents of $pgArray[$i].

So the real issue is, what is happening to the <div> in

$pgArray[$i] = "<div>" . $pgParamArray["page"] - $i . "</div>";

After seeing the real output, the answer is a little more obvious now: it's a grouping issue. PHP is simply taking it left to right:

((("<div>". $pgParamArray["page"]) - $i) . "</div>") 

((("<div>" . 1 ) - $i) . "</div>")

((("<div>1") - $i ) . "</div>")

((( 0 ) - $i ) . "</div>") 

((( 0 ) - 0 ) . "</div>")

((( 0 )) . "</div>>") 

((( "0</div>" )))

Upvotes: 2

Rees Morris
Rees Morris

Reputation: 158

Wrapping your statement in parenthesis seems to work for me:

$pgArray[$i] = "<div>" . ($pgParamArray["page"] - $i) . "</div>";

Without the parenthesis, it seems that the value breaks completely; the page doesn't print the <div> tags at all, but rather just adds a trailing 0 to the already printed 1.

I would assume that it's due to how PHP processes string concatenation, though I wouldn't be able to give you an exact answer as to why this happens. Just to be safe, I'd always either store any equations in variables before you pass them in, or perform all operations inside of parenthesis.

That way, you won't have weird encounters like this (for example):

echo "<div>" . 1 + 1 . "</div>";     // returns 1
echo "<div>" . (1 + 1) . "</div>";   // returns 2

Upvotes: 2

Related Questions