13 Replies - 596 Views - Last Post: 09 April 2012 - 10:33 AM Rate Topic: -----

#1 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Weird thing I found with echo

Posted 08 April 2012 - 04:54 PM

I was wondering why this is happening.

code:
<?php
$page = (isset($_GET['page'])) ? $_GET['page'] : 0;
echo '<a href="?page=' . $page+1 . '" class="pull-right" class="button pull-right">Next</a><br>';
echo '<a href="?page=' . ($page+1) . '" class="pull-right" class="button pull-right">Next</a>';
?>



Output:
1" class="pull-right" class="button pull-right">Next</a><br>
<a href="?page=1" class="pull-right" class="button pull-right">Next</a>



I'm using WAMP and PHP 5.3

This post has been edited by creativecoding: 08 April 2012 - 04:55 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Weird thing I found with echo

#2 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

Re: Weird thing I found with echo

Posted 08 April 2012 - 05:03 PM

Well the "how" would be because in the first line you don't have $page+1 in a set of ()

Not sure on the why though, other than it clearly doesn't like you trying to add like that within a concatenation.
Was This Post Helpful? 0
  • +
  • -

#3 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Weird thing I found with echo

Posted 08 April 2012 - 05:10 PM

Concatenation has precedence over the addition operator.

So it's trying to concatenate a variable that is probably equal to 0 that also has a plus sign next to it.

It's probably the plus sign because it's not evaluating after the concatenation so the code fouls up because a number is not able to sum to a string.

This post has been edited by hiddenghost: 08 April 2012 - 05:12 PM

Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3718
  • View blog
  • Posts: 5,989
  • Joined: 08-June 10

Re: Weird thing I found with echo

Posted 08 April 2012 - 05:12 PM

The + and . operators have equal precedence, so when you order them like that they are evaluated left to right. Which means that the first . concats the first string and the $page value. Then the + adds the string -- which when used in an arithmetic operation will be converted to 0 -- to the 1, after which it finally concats the output of that addition to the string on the end.
Was This Post Helpful? 2
  • +
  • -

#5 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: Weird thing I found with echo

Posted 08 April 2012 - 05:41 PM

Very good explanation, Atli.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2959
  • View blog
  • Posts: 10,183
  • Joined: 08-August 08

Re: Weird thing I found with echo

Posted 08 April 2012 - 07:00 PM

I think there may be a little more to this than the order of operations. Check this out:
<?php
$x = 10;
echo 'testing: '. $x + 2 .'<br>';
echo 'testing: '.($x+2).'<br>';
?>

Output is missing the first testing:
2<br>testing: 12<br>


And this way just gets an error:
<?php
$x = 10;
echo 'testing: '.$x + 2.'<br>';
echo 'testing: '.($x+2).'<br>';
?>

[08-Apr-2012 21:59:01] PHP Parse error:  syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /Users/me/Documents/htdocs/test.php on line 3


Was This Post Helpful? 0
  • +
  • -

#7 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3718
  • View blog
  • Posts: 5,989
  • Joined: 08-June 10

Re: Weird thing I found with echo

Posted 08 April 2012 - 07:13 PM

View PostCTphpnwb, on 09 April 2012 - 02:00 AM, said:

Output is missing the first testing:

That's because the first 'testing', along with the $x variable, are converted into a 0 for the arithmetic operation done when the + operator is applied.

Try starting the first 'testing' with a number. Then, instead of being converted to a 0 and appearing to disappear completely, it will be converted into that number and you'll get that number + 2 instead of just 2.

View PostCTphpnwb, on 09 April 2012 - 02:00 AM, said:

And this way just gets an error:

This would be because the . that you mean to be a concat operator is now read as if it were separating the 2 from a fraction. Try:
echo 'testing: '. $x + 2.0.'<br>';


Was This Post Helpful? 1
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2959
  • View blog
  • Posts: 10,183
  • Joined: 08-August 08

Re: Weird thing I found with echo

Posted 08 April 2012 - 07:29 PM

I knew there was a reason I always prefer to use parentheses: being explicit saves hassle.
Was This Post Helpful? 0
  • +
  • -

#9 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Weird thing I found with echo

Posted 08 April 2012 - 07:37 PM

View PostAtli, on 08 April 2012 - 07:12 PM, said:

The + and . operators have equal precedence, so when you order them like that they are evaluated left to right. Which means that the first . concats the first string and the $page value. Then the + adds the string -- which when used in an arithmetic operation will be converted to 0 -- to the 1, after which it finally concats the output of that addition to the string on the end.


Oh your right. I looked it up again.
Whoops! Thanks for clarifying that.
Was This Post Helpful? 0
  • +
  • -

#10 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3718
  • View blog
  • Posts: 5,989
  • Joined: 08-June 10

Re: Weird thing I found with echo

Posted 08 April 2012 - 07:42 PM

CTphpnwb said:

I knew there was a reason I always prefer to use parentheses: being explicit saves hassle.

Yea, very true. I also find it a lot more readable with the parenthesis.

Although when I'm dealing with an echo statement like that, I'd be much more likely to pass things as multiple parameters rather than a single, concatenated string:
echo 'testing: ', $x + 2, '<br>';


Either that, or simply:
$x = 10;
$x2 = $x + 2;
echo "testing: {$x2}<br>";


Separation of presentation and logic, right! :)
Was This Post Helpful? 0
  • +
  • -

#11 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Weird thing I found with echo

Posted 08 April 2012 - 08:08 PM

You know it's strange I have never really payed too much attention to type differences when it comes to strings and numbers.
Yet I don't think I have ever had this issue with echo.

Actually the way I code is kind of haphazard so I get into quite a bit of trouble. Just not that one problem. :)

Come to think of it I almost never use php type casting. Is that good or bad?
Was This Post Helpful? 0
  • +
  • -

#12 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Weird thing I found with echo

Posted 08 April 2012 - 08:20 PM

View PostAtli, on 08 April 2012 - 09:42 PM, said:

Either that, or simply:
$x = 10;
$x2 = $x + 2;
echo "testing: {$x2}<br>";


Separation of presentation and logic, right! :)


That's the way I do it. I don't ever use the comma way in echoes. Just personal consistency I guess.

What about?
$x = 10;
$x += 2;
echo "testing: $x<br>";
// or echo "testing: {$x}<br>";
// or
echo preg_replace("/\[num\]/", $x, "testing: [num]<br>");



Sorry, got a little silly there.
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2959
  • View blog
  • Posts: 10,183
  • Joined: 08-August 08

Re: Weird thing I found with echo

Posted 09 April 2012 - 06:39 AM

View PostAtli, on 08 April 2012 - 10:42 PM, said:

Separation of presentation and logic, right! :)

Ah! Another reason I'd be unlikely to see this is that I'm more likely to be doing something like:
$x = 10;
echo str_replace("{sum}", $x+2, file_get_contents("somefile.html"));


Was This Post Helpful? 0
  • +
  • -

#14 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,179
  • Joined: 08-June 10

Re: Weird thing I found with echo

Posted 09 April 2012 - 10:33 AM

View PostCTphpnwb, on 09 April 2012 - 03:39 PM, said:

Ah! Another reason I'd be unlikely to see this is that I'm more likely to be doing something like:
$x = 10;
echo str_replace("{sum}", $x+2, file_get_contents("somefile.html"));


not to forget the printf() family of functions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1