Page 1 of 1

Why Does This Not Work?

Posted: Sun Jul 26, 2009 12:41 pm
by jackpf
Hello all,
I was just wondering...why does this not work?

Code: Select all

@fopen('non-existent-file.txt') or echo 'error';
Will result in a parse error.

But

Code: Select all

@fopen('non-existent-file.txt') or print 'error';
Using the print() function will not.

I don't understand... :P

Jack.

Re: Why Does This Not Work?

Posted: Sun Jul 26, 2009 2:31 pm
by Eran
echo is not a function.
echo() is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo() (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function
http://www.php.net/echo

Re: Why Does This Not Work?

Posted: Sun Jul 26, 2009 2:33 pm
by jackpf
Neither is print :P

http://php.net/print


That's why I don't understand why one works and the other doesn't.

Re: Why Does This Not Work?

Posted: Sun Jul 26, 2009 3:02 pm
by Eran
print() behaves like a function in that you can do:
$ret = print "Hello World"; And $ret will be 1. That means that print
can be used as part of a more complex expression where echo cannot.

Re: Why Does This Not Work?

Posted: Sun Jul 26, 2009 3:12 pm
by jackpf
Ahhhh right.

Thanks a lot :)