Why Does This Not Work?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Why Does This Not Work?

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Why Does This Not Work?

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Why Does This Not Work?

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Why Does This Not Work?

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Why Does This Not Work?

Post by jackpf »

Ahhhh right.

Thanks a lot :)
Post Reply