Do you know any PHP statement that works like Python's pass
statement?
Asked
Active
Viewed 2.7k times
34

Jonathan Leffler
- 730,956
- 141
- 904
- 1,278

Jan Tojnar
- 5,306
- 3
- 29
- 49
6 Answers
67
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.

Tyler Carter
- 60,743
- 20
- 130
- 150
-
7i need it to use in ternary operator. `(condition?something:pass)`; `condition?something:` ends with error – Jan Tojnar May 17 '10 at 21:08
-
4@Jan Tojnar: Use an empty string `''`. If you use the ternary operator, you mostly assign a value to a variable. This will assign `false` then (empty string is false). If you do not use it for variable assignment, it what use a plain `if` statement. Much easier to understand then. – Felix Kling May 17 '10 at 21:36
-
Can't you just use `result = condition ? something : result`? – ktdrv May 17 '10 at 21:36
26
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.

Yacoby
- 54,544
- 15
- 116
- 120
-
1And better yet, add a comment in those empty brackets so that you can remember what you were trying to do there.. – Chris Nielsen Sep 25 '20 at 20:58
0
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);

TortelliEngineer
- 183
- 1
- 2
- 13