34

Do you know any PHP statement that works like Python's pass statement?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jan Tojnar
  • 5,306
  • 3
  • 29
  • 49

6 Answers6

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
  • 7
    i 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
9

Can't you just use a semicolon?

if ( $x == $y )
  ;
rossipedia
  • 56,800
  • 10
  • 90
  • 93
3

When needed for readability you could use

null;
ajthinking
  • 3,386
  • 8
  • 45
  • 75
1

NULL is the alternative for pass in python

$a == $b? $b = 10 : NULL;
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