4

since echo is not a function, we do not use parenthesis when calling it.

example:

echo "hello";

as opposed to

echo ("hello");  

If it were to be a function, it would have been forced to be called as echo ("hello");

In ASP/Vbscript, I can call a function one of the following ways;

call dosomething("x","y","z")

dosomething "x","y","z"   notice the missing parathesis

The closest thing I have seen to this parenthesis-free syntax in PHP is the echo. I like the ability to skip the parenthesis.

My question to you is if there is a way to write a function in PHP, that would not require the use of the parenthesis?

Average Joe
  • 4,521
  • 9
  • 53
  • 81
  • 5
    `echo` is not a function but rather a "language construct" (http://php.net/manual/en/function.echo.php) that's why you can use with without parenthesis. Check out the answer to this: http://stackoverflow.com/questions/1180184/what-is-the-difference-between-a-language-construct-and-a-built-in-function-in – Yaniro Mar 17 '12 at 09:03

3 Answers3

12

From within PHP? No.

You'd need to modify the C code in the Zend Engine. This is of course undesirable as you would need to use a custom build to run your code. It would also be confusing needlessly to other programmers. You should not try to change core language features simply because you like the ability to skip tokens.

The parenthesis are important to denote function invocation.

Things such as echo, include, etc are called language constructs. The way they are implemented in the language are similar to normal unary operators such as new, ++ etc.

Note too that language constructs do not strictly omit the parenthesis. Take unset() for example.

alex
  • 479,566
  • 201
  • 878
  • 984
  • "No" would suffice as the question asked specifically about writing "a function in PHP". – salathe Mar 17 '12 at 09:41
  • 3
    @salathe Is fleshing out an answer frowned upon? – alex Mar 17 '12 at 09:42
  • Only when it takes the answer off-topic. – salathe Mar 17 '12 at 09:59
  • 5
    @salathe I don't believe it has here. I told the OP *how* it could be done and the disadvantages of doing it. – alex Mar 17 '12 at 10:00
  • which does not answer the question asked. – salathe Mar 17 '12 at 10:02
  • 2
    @salathe *...is [there] a way to write a function in PHP, that would not require the use of the parenthesis?* to which I answered *Not without modifying the C code in the Zend Engine*. Seems like an answer to the question to me. I'll explicitly add the *not in PHP* part if it helps you. – alex Mar 17 '12 at 10:03
  • @adam anything bar the first paragraph (or first sentence before your edit) is superfluous for this question. – salathe Mar 17 '12 at 10:06
  • @salathe Sure, but we'd never have great answers (not implying this one is) if people only posted *yes* or *no*. – alex Mar 17 '12 at 10:07
  • If the question is answered by "yes" or "no" then that is a *great answer*. The ubiquitous apparent need to "flesh out" answers here on SO is disappointing. Adding fluff for the sake of *appearing* to be a more thought-out, satisfactory or otherwise better answer—to game the OP into accepting yours over others—is something I heartily dislike about this site. – salathe Mar 17 '12 at 10:11
  • 2
    @salathe I'd much rather learn the answer to my questions and then some more relevant information. I'll just disagree and leave it at that. Also, believing I added the extra information to be accepted over the other answers is a presumption and an incorrect thought. – alex Mar 17 '12 at 10:14
  • Fair enough. I stand corrected in this individual case, clearly. – salathe Mar 17 '12 at 10:16
  • 12
    I see your point salathe, but I prefer a more in depth answer like the one Alex gave. I would have hated if the answer was just a yes or no. Take the yes case. Is that going to cause me to ask another one asking "how then?". Also, many times, a simple yes|no is very misleading. – Average Joe Mar 17 '12 at 14:16
2

no. "Echo" is not a function in php, but a language construct, that's why you can use it without parens. The same goes for "include" or "require", you can use them with or without parens.

Vlad Balmos
  • 3,372
  • 19
  • 34
0

echo() is not actually a function (it is a language construct), so you are not required to use parentheses with it.

More about echo can be found here: http://php.net/manual/en/function.echo.php

Mike
  • 72
  • 1
  • 2
  • 9
divya
  • 19
  • 5