true && print()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stefan Weiss

    true && print()

    Hi.

    function printx($str) {
    echo $str;
    }

    true && printx("foo");
    true && print("bar");


    Why do I see "foo", but not "bar"?

    According to the manual, print and echo are language constructs, not
    functions. That seems to be the reason why they don't behave as
    expected, but what I'm looking for is an explanation. Is there a
    rationale for not executing print() in this context?

    tia,
    stefan
  • Tim Roberts

    #2
    Re: true && print()

    Stefan Weiss <spaceman@foo.a t> wrote:
    [color=blue]
    >Hi.
    >
    > function printx($str) {
    > echo $str;
    > }
    >
    > true && printx("foo");
    > true && print("bar");
    >
    >
    >Why do I see "foo", but not "bar"?
    >
    >According to the manual, print and echo are language constructs, not
    >functions. That seems to be the reason why they don't behave as
    >expected, but what I'm looking for is an explanation. Is there a
    >rationale for not executing print() in this context?[/color]

    You GAVE the explanation. What you have written there are expressions.
    printx is a function which returns a value (implicitly), and can thus be
    used in an expression. "print" is a statement; it does not return a value,
    and thus cannot participate in expressions.

    C has made us all lazy; people aren't aware of the difference between an
    expression and a statement. Consider that you are asking to do the same
    thing as this in C:

    1 && while( x == 3 ) {
    dowhat(x);
    }
    --
    - Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.

    Comment

    • kicken

      #3
      Re: true &amp;&amp; print()

      Tim Roberts wrote:[color=blue]
      >
      > You GAVE the explanation. What you have written there are expressions.
      > printx is a function which returns a value (implicitly), and can thus be
      > used in an expression. "print" is a statement; it does not return a value,
      > and thus cannot participate in expressions.
      >
      > C has made us all lazy; people aren't aware of the difference between an
      > expression and a statement. Consider that you are asking to do the same
      > thing as this in C:
      >
      > 1 && while( x == 3 ) {
      > dowhat(x);
      > }[/color]

      Actually, that's the main difference between print and echo. Print CAN
      be used in expressions while echo can't. Print does have a return value
      , and behaves like a normal function, despite it not actually being one.
      See the last paragraph in the manual, which links to this faq:


      In response to the OP, the code works just fine for me. It prints out
      "foobar".

      Comment

      • Stefan Weiss

        #4
        Re: true &amp;&amp; print()

        On 2005-06-17 08:08, kicken wrote:[color=blue]
        > Actually, that's the main difference between print and echo. Print CAN
        > be used in expressions while echo can't. Print does have a return value
        > , and behaves like a normal function, despite it not actually being one.
        > See the last paragraph in the manual, which links to this faq:
        > http://www.faqts.com/knowledge_base/...l/aid/1/fid/40[/color]

        Yes, I am aware of the difference between print and echo, that is why I
        used print in my example.
        [color=blue]
        > In response to the OP, the code works just fine for me. It prints out
        > "foobar".[/color]

        Very interesting. I have tried the same script on several web servers
        now, with the following results:

        [SuSE 9.2, Apache/2.0.50]
        This is localhost, running PHP 4.3.8
        foobar

        [SuSE 9.2, Apache/2.0.50]
        This is localhost, running PHP 5.0.4
        foobar

        [Debian Sarge, Apache/2.0.53]
        This is www.foo.at, running PHP 5.0.4
        foobar

        [Debian Sarge, Apache/1.3.33]
        This is akira.foomatic. at, running PHP 5.0.4
        foo


        Huh.

        The only interesting difference that I can see is that the last server
        is running Apache 1 instead of Apache 2.


        cheers,
        stefan

        Comment

        Working...