You are here: Re: newbie syntax question « PHP Programming Language « IT news, forums, messages
Re: newbie syntax question

Posted by Ivαn Sαnchez Ortega on 02/13/06 01:59

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

leegold wrote:

> Whoa, that's saying that the initial argument to the print function is not
> completely evaluated, but only half-way evaluated and passed and
> then...gosh I'd expect the function to let the args or params - whatever -

So, let's have a look at the operator precedence table* to explain what's
happening behind the scenes while evaluating this:

print '12==12' . print (12==12);

* http://es2.php.net/manual/en/language.operators.php


First of all, the outer "print" wants to be evaluated. In order to do so,
the expression it refers to ("'12==12' . print (12==12)") has to be
evaluated.

The expression "'12==12' . print (12==12)" wants to be evaluated. That
expression can be split into two expressions, "'12==12'" and "print
(12==12)", separated by the left-associative binary operator "." (string
concatenate). So, in order to evaluate this expression, first I'll have to
evaluate "'12==12'", then evaluate "print (12==12)", then apply the binary
operator.

So, the expression "'12==12'" wants to be evaluated. As it is a constant, it
gets evaluated as such.

Next, the expression "print (12==12)" wants to be evaluated. And here comes
the trick. You are not evaluating a numerical expression, but a function
that does I/O (outputs something). But, in order to evaluate this
print(12==12), first we have to evaluate (12==12).

So, (12==12) gets evaluated, as (bool) true.

Then, "print ((bool) true)" is ready to run. This outputs "1" by stdout
(implicit type casting from bool to string), and the expression "print
(12==12)" gets evaluated as (bool) true, as print has been sucessful.

Then, "'12==12' . print(12==12)" is ready to be evaluated. At this time,
that expression equals "'12==12' . (bool) true". So, (bool) true gets
implicitly casted from bool to string, and both strings are concatented.
Now that expression equals "12==121".

Then, the outer print is ready to be run and evaluated. While running, it
outputs "12==121" by stdout. Then, it evaluates to (bool) true.



If that was too much for you, just think about the following code:

<?php

$a = '12==12: ';
$b = print (12==12);
print $a.$b;

?>

$a is evaluated first. Then $b, then "print $a.$b". That's how it happens.

> I'd expect it to print the result of the line and the print fuction itself
> not to be a factor like your saying.

They are not "factors". As in C (and several other imperative languages),
PHP works with expressions.

- --
- ----------------------------------
IvΓ‘n SΓ‘nchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

http://acm.asoc.fi.upm.es/~mr/ ; http://acm.asoc.fi.upm.es/~ivan/
MSN:i_eat_s_p_a_m_for_breakfast@hotmail.com
Jabber:ivansanchez@jabber.org ; ivansanchez@kdetalk.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFD78v53jcQ2mg3Pc8RAgi2AKCIEMCwlGyDTFm+Pq9yQ3XsfCBfCgCdFbw6
fU+7d+LJ50F2SNpodKsxu1M=
=TzYY
-----END PGP SIGNATURE-----

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация