Strange Echo, Print Behaviour in PHP

Strange echo, print behaviour in PHP?

Your statement parses to humans as follows.

Echo a concatenated string composed of:

  1. The result of the function print('3'), which will return true, which gets stringified to 1
  2. The string '2'
  3. The result of the function print('4'), which will return true, which gets stringified to 1

Now, the order of operations is really funny here, that can't end up with 43211 at all! Let's try a variant to figure out what's going wrong.

echo '1' . print('2') . '3' . print('4') . '5';

This yields 4523111

PHP is parsing that, then, as:

echo '1' . (print('2' . '3')) . (print('4' . '5'));

Bingo! The print on the left get evaluated first, printing '45', which leaves us

echo '1' . (print('2' . '3')) . '1';

Then the left print gets evaluated, so we've now printed '4523', leaving us with

echo '1' . '1' . '1';

Success. 4523111.

Let's break down your statement of weirdness.

echo print('3') . '2' . print('4');

This will print the '4' first, leaving us with

echo print('3' . '2' . '1');

Then the next print statement is evaluated, which means we've now printed '4321', leaving us with

echo '1';

Thus, 43211.

I would highly suggest not echoing the result of a print, nor printing the results of an echo. Doing so is highly nonsensical to begin with.


Upon further review, I'm actually not entirely sure how PHP is parsing either of these bits of nonsense. I'm not going to think about it any further, it hurts my brain.

Strange print behaviour in PHP?

Because it's a bitwise operator. I think it means 4 multiplied to 2^5 because that operator means

Shift the bits of $a $b steps to the left (each step means "multiply
by two")

so it's five steps. It's 4 * 2 * 2 * 2 * 2 * 2 (But I'm guessing here; everything happens at bit level).

Weird PHP echo behaviour when setting echo'ing into a html value field

try echo in this way

echo "<td><input id='firmenname1'  type='text' value=".$row['firmenname1']." /></td>";

print behavior php

It's because PHP is a ridiculous language. print is not a normal function, it's a language construct. This line is actually parsed as:

if (print (("foo") || print("bar")))

And ("foo") || print("bar") is an expression which evaluates to 1. The string "foo" in a boolean context is true, so the || operator yields 1.

If you explicitly parenthesize the expression the way one would expect it to be parsed:

if ((print("foo")) || (print("bar"))) 

Then the output is what you would expect:

foo

PHP echo printing additional characters

SOLUTION 1:

short_open_tag=On

in php.ini and restart your Apache server.

SOLUTION 2:

Use the <?php tags completely because <? is not recommended.

If you ever have to move your code to a server where it's not supported (and you can't enable it), then you're doomed. It's better to use the full syntax.

strange ob_start() behaviour - double output

Remove the echo on the last line.

ob_get_flush() implicitly prints the stored output and also returns it so you're printing it out twice.

You may have confused ob_get_flush() with ob_get_clean()

PHP floor() weird behaviour

This is a result of floating point precision in PHP, in your example:

gettype($val1); returns integer

and

gettype($val2); returns double

Combine that with this warning on php.net:

Additionally, rational numbers that are exactly representable as
floating point numbers in base 10, like 0.1 or 0.7, do not have an
exact representation as floating point numbers in base 2, which is
used internally, no matter the size of the mantissa. Hence, they
cannot be converted into their internal binary counterparts without a
small loss of precision. This can lead to confusing results: for
example, floor((0.1+0.7)*10) will usually return 7 instead of the
expected 8, since the internal representation will be something like
7.9999999999999991118....

taken from http://php.net/manual/en/language.types.float.php

And we can see why your floor takes it from 28.9999999999 etc down to 28, instead of the integer 29 to 29.

php print inside print - what's the result

This is because of the brackets (operation precedence) - the

(print 3)

in the end of the line displays the first digit of the final output (3), but all PHP print statements return 1. Always (check the manual). So after this, we've got:

print (2).(3 * 1);

which is the same as:

print (2).(3);

Now it's just a simple concatenation which will output "23". So we've got "323" displayed.

Note that

print (2).(1 - (print 3));

would display "320".



Related Topics



Leave a reply



Submit