I was wondering if there is any nice way of writing functions in PHP so that they don't require ( ) around the parameters.
Example:
function sayThis($str) {
echo $str;
}
sayThis "hi!!";
Thanks,
Matt Mueller
There simply isn't. "echo" is more of an operator than a function, so you'd actually need to rewrite the PHP interpreter source in order to introduce new "functions" like those.
Edit: Actually, the more accurate term for "echo" is, as eyze has correctly pointed out, language construct rather than operator. http://php.net/manual/de/function.echo.php provides some more information.
Simple answer, no.
echo is a language construct not a function, hence it doesn't need the parentheses.
Related
There are plenty of examples of both on the web. The php manual says "The include() statement [...]", which seems contradictory - if it's a statement shouldn't it not have parenthesis?
Both of these work:
include('somefile.php');
include 'somefile.php;
So should I or anyone else care?
Quoting from the manual (my emphasis)
Because include() is a special language construct, parentheses are not needed around its argument.
These are also called "special forms", and include such things as echo and return statements. Note that while none of these are functions, you can still speak of expressions and statements, the difference being the former have a value while the latter don't. Since include, include_once, require and require_once all return a value (TRUE if the include was successful), they can be used in expressions. By this reasoning, "include statement" would be incorrect, though includes are almost always used as statements.
include is a statement :
Explain by following eg
// won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')
if (include('vars.php') == 'OK') {
echo 'OK';
}
// works
if ((include 'vars.php') == 'OK') {
echo 'OK';
}
The parentheses are parameters for a function.
With include you can use it either as a function or a statement in php.
Because include() is a special language construct, parentheses are not
needed around its argument.
Documentation here: http://php.net/manual/en/function.include.php
With echo same concept, quoting from the PHP manual here
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.
Single values within parens evaluate to the value itself, so the parens themselves are of no consequence.
Both. In many areas of the PHP documentation, everything is referred to as a statement. Example (from control structures) - "A statement can be an assignment, a function call, a loop, a conditional statement or even a statement that does nothing (an empty statement)."
The difference between a statement and a functions is a matter of the semantics of the individual language. Thus, it's up the PHP maintainers to either explicitly define this, or let it remain ambiguous.
include, require, echo, print, and a handful of other keywords are language constructs on their own, rather than function calls, and do not need parentheses. However, some people like to pretend they're function calls and use parentheses anyway. They are identical.
for echo always use echo "test"
<?php
// this will give you error
echo ("test","test");
//that will work fine
echo "test","test";
?>
Statements having only one parameter, can have also parenthesis, e.g:
echo ('hello world');
I believe the only difference is what you want to do.
For example, these two
echo 'Hello World';
echo ('Hello World');
...both print Hello World.
It is just what you want to do, what you feel most comfortable with, and if you're like me, you want to make your script look pretty :D
Can I use it as a method name in my classes?
It appears to be a function: http://www.php.net/manual/en/ref.misc.php
But I see there listed some language constructs too, like die and exit.
When you can... you can, when you can't... you'll know :)
See a list of reserved words here http://php.net/manual/en/reserved.php and avoid them. Any good PHP IDE will warn you when you attempt to illegal-name your methods.
There's another issue. If you forget $this->Method() and you just use Method() in your class, it will work as it's defined as a function. This will lead to freak and hard to find bugs.
<?php
class Test {
function defined() {
return "Yes you can";
}
}
$x = new Test();
echo $x->defined();
Yes, you can. No, you shouldn't. Using the same name as built-in functions is never a good idea. The word defined has an established meaning in PHP, and nobody should have to think harder to figure out how your class is using (or abusing) the word in some specific context.
There are plenty of examples of both on the web. The php manual says "The include() statement [...]", which seems contradictory - if it's a statement shouldn't it not have parenthesis?
Both of these work:
include('somefile.php');
include 'somefile.php;
So should I or anyone else care?
Quoting from the manual (my emphasis)
Because include() is a special language construct, parentheses are not needed around its argument.
These are also called "special forms", and include such things as echo and return statements. Note that while none of these are functions, you can still speak of expressions and statements, the difference being the former have a value while the latter don't. Since include, include_once, require and require_once all return a value (TRUE if the include was successful), they can be used in expressions. By this reasoning, "include statement" would be incorrect, though includes are almost always used as statements.
include is a statement :
Explain by following eg
// won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')
if (include('vars.php') == 'OK') {
echo 'OK';
}
// works
if ((include 'vars.php') == 'OK') {
echo 'OK';
}
The parentheses are parameters for a function.
With include you can use it either as a function or a statement in php.
Because include() is a special language construct, parentheses are not
needed around its argument.
Documentation here: http://php.net/manual/en/function.include.php
With echo same concept, quoting from the PHP manual here
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.
Single values within parens evaluate to the value itself, so the parens themselves are of no consequence.
Both. In many areas of the PHP documentation, everything is referred to as a statement. Example (from control structures) - "A statement can be an assignment, a function call, a loop, a conditional statement or even a statement that does nothing (an empty statement)."
The difference between a statement and a functions is a matter of the semantics of the individual language. Thus, it's up the PHP maintainers to either explicitly define this, or let it remain ambiguous.
include, require, echo, print, and a handful of other keywords are language constructs on their own, rather than function calls, and do not need parentheses. However, some people like to pretend they're function calls and use parentheses anyway. They are identical.
for echo always use echo "test"
<?php
// this will give you error
echo ("test","test");
//that will work fine
echo "test","test";
?>
Statements having only one parameter, can have also parenthesis, e.g:
echo ('hello world');
I believe the only difference is what you want to do.
For example, these two
echo 'Hello World';
echo ('Hello World');
...both print Hello World.
It is just what you want to do, what you feel most comfortable with, and if you're like me, you want to make your script look pretty :D
Do you know any PHP statement that works like Python's pass statement?
Just leave the bracket's empty...
Python has the pass word because they don't use brackets to define the body part of classes, function, and other statement. PHP doesn't have this dilemma , and therefore doesn't need something to say that a body statement is empty.
It isn't needed in PHP. The Python code:
if x == y:
pass
Can be written in PHP by just leaving the brackets empty
if ( x == y ){
}
The same applies to other PHP constructs requiring brackets such as classes or functions.
Can't you just use a semicolon?
if ( $x == $y )
;
When needed for readability you could use
null;
The syntax checkers and linters are becoming more and more good at signaling not useful block of codes, also when this is a voluntary action of the developer.
A native do_nothing() function would be very nice and readable sometimes.
To avoid stressing alerts from syntax checkers, I use:
echo(null);
NULL is the alternative for pass in python
$a == $b? $b = 10 : NULL;
I wanted to know how PHP would execute this. Order of operations
addslashes(strip_tags($record['value']));
Is addslashes called first or strip_tags?
In other words, does it execute from the inside out or from the outside in?
From the inside out.
The things passed into a function in PHP are called "expressions". When you pass an expression as a parameter, what you're really passing is the value of that expression. In order to do that, the expression is evaluated before it is passed in.
More about expressions from the php manual.
strip_tags is called first.
and this is not just the case with PHP, it is the case with every other programming language (excluding some obscure esoteric language that may have some unique order of evaluation).
PS: Here is some documentation: PEDMAS. This is what inspired this kind of evaluation order in programming languages too.
If you think about it in a logical way, what does PHP need in order to execute the function? The variable. So, strip_tags needs the $record['value'] to be inputted before it can execute the function and strip the tags from it. That function will return a value.
Now, addslahes needs a variable too. It cannot execute on a function, it needs that function inside it to return something for it to process. So it uses that returned value from strip_tags as its variable and executes upon that.
addslashes takes one argument, in your case it is strip_tags($record['value']).
addslashes can't be called when it's argument isn't resolved.
Therefore strip_tags must be called first. This is the case in nearly all popular programming languages out there. I wonder how you managed to get by before knowing this!