tags:

views:

167

answers:

5

I fell in love with Python syntax of not using { } but indents instead. Is it possible to config PHP to not use { }?

+14  A: 

You can't use indents, but there is the alternative syntax. It uses:

if (cond):

endif;

while (cond):

endwhile;

etc.

This isn't significant whitespace.

Matthew Flaschen
A: 

There is some of the endif type of syntax, but you probably shouldn't use that just so you can create blocks without curly braces.

And even if you found some way that allows you to mark blocks of code by indentation and not by curly braces, that wouldn't be a very good idea. If you really care about using Python's syntax, then use Python.

aldld
A: 

You could also skip using {} for a single operation after a test statement or a loop.

Babiker
+3  A: 

PHP does not use white space to handle blocks the way Python does. PHP and Python are two distinctly different languages. If you were to "configure" PHP to have syntax like Python's, it would no longer be PHP, but some new language.

There is no direct way to do this. If you want to use PHP, I'd recommend trying to adapt to PHP's syntax and idioms. Luckily, as PHP is not white-space sensitive, you are free to format your code and indent it using the same indentation scheme you used with Python.

Using the if/edif syntax and such (as suggested) will just make your PHP look even further from the Python you are trying to emulate. If you really want to program in Python, consider using it as your server-side language. There are even nice frameworks like Pylons that can make this easier, if that is what you need.

Reed Copsey
+2  A: 

To be honest, I'd switch to django for your server side stuff if I were you, as it really is a nicer language than php in many respects. If you'd like to avoid using curly braces, php provides alternative syntaxes for many of it's methods, they usually look something like this:

if(somthing):
do_somthing();
endif;

I usually use these when I'm output buffering text or have small php interjections in template code. You can also use ternary operators and other conventions that avoid curly braces, like this one:

if(something)
    do_something();

or a ternanry:

(something)?do_something():do_something_else();

the php manual is pretty good about letting you know when you can use alternative syntax, but like I said, if you're not already using alternative php syntax, it sounds like a fantastic time to switch to django.

Jesse
I think there is no **if** in front of that ternary operation.
Babiker
whooops. fix'd.
Jesse