Concatenation Question?

$lo = 2;
$high = 7;
echo $lo < $high;

The above code outputs 1.

However,

echo $lo < $high . " string";

doesn’t concatenate " string" to that 1. Why is that?

I know there is a better way of doing all of the above, but I’m just trying to understand the concatenation issue.

Thanks!

Why are you trying to complicate simple thing?

I’m not sure about this codes below, but try,


    echo {$lo < $high}. " string";

<?php
echo (int)( $lo < $high ) . " string";

i think this happen because, php does the operation first and cast the the ‘answer’ to a number ‘1’ is true, ‘0’ is false.

http://www.php.net/manual/en/language.operators.precedence.php

A word of caution - the dot operator has the same precedence as + and -, which can yield unexpected results.

Example:

<php
$var = 3;

echo "Result: " . $var + 3;
?>

The above will print out “3” instead of “Result: 6”, since first the string “Result3” is created and this is then added to 3 yielding 3, non-empty non-numeric strings being converted to 0.

To print “Result: 6”, use parantheses to alter precedence:

<php
$var = 3;

echo "Result: " . ($var + 3);

http://www.php.net/manual/en/language.operators.string.php

Thanks everyone for your replies and help. Your feedback has helped me to clarify what is happening and why it is not working.