LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   PHP, {}, and ";" after them (http://www.linuxquestions.org/questions/programming-9/php-%7B%7D-and-%3B-after-them-911622/)

Mr. Alex 11-03-2011 07:17 AM

PHP, {}, and ";" after them
 
Hello. In C++ (AFAIK) after "if {...}" you also should add ";". But after this statement in PHP you don't have to (might look like negligence, which is so like PHP). But what is a better practice in PHP?

Code:

if (true)
{
  echo 'True';
};

echo 'END';

Is this coding style better than

Code:

if (true)
{
  echo 'True';
}

echo 'END';

?

Proud 11-03-2011 09:12 AM

I'd go with whatever's valid syntax, being as it looks clear enough without the ;. In fact, I tend to comment something like } // end if describe condition in many languages.

If you don't like this PHP syntax quirk, you might hate Python ;)

ntubski 11-03-2011 10:59 AM

Quote:

Originally Posted by Mr. Alex (Post 4514743)
Hello. In C++ (AFAIK) after "if {...}" you also should add ";".

No, you don't have to do this and I've never seen anyone do this. You do have to add ";" after the closing brace of a declaration (eg struct or enum): "struct { ... };".

Mr. Alex 11-03-2011 01:11 PM

Quote:

Originally Posted by ntubski (Post 4514929)
You do have to add ";" after the closing brace of a declaration (eg struct or enum): "struct { ... };".

You mean OOP?

Here's C++ example:
Code:

#ifndef __ADDRBAR_H
#define __ADDRBAR_H

#ifdef CONFIG_ADDRESSBAR

#include "yinputline.h"

class AddressBar: public YInputLine {
public:
    AddressBar(YWindow *parent = 0);
    virtual ~AddressBar();

    virtual bool handleKey(const XKeyEvent &key);

    void showNow();
    void hideNow();
};

#endif

#endif


ntubski 11-03-2011 01:41 PM

Quote:

Originally Posted by Mr. Alex (Post 4515014)
You mean OOP?

Not specifically, but a semicolon is required after a class declaration as well.


All times are GMT -5. The time now is 04:54 AM.