Jump to content

PHP debate

Judahnator

I got into a friendly argument recently over the correct way to format code.

 

What do you guys (and gals) think is more correct?

 

IF () {

...

} ELSE {

...

}

 

vs.

 

IF ()

     {

     ...

     }

ELSE

     {

     ...

     }

 

~Judah

Link to comment
Share on other sites

Link to post
Share on other sites

-snip-

 

I personally format with the latter way,

 

   }

else

   {

 

That's just me though.

The projects never end in my line of work.

CPU: Dual Xeon E5-2650v2 || GPU: Dual Quadro K5000 || Motherboard: Asus Z9PE-D8 || RAM: 64GB Corsair Vengeance || Monitors: Dual LG 34UM95, NEC MultiSync EA244UHD || Storage: Dual Samsung 850 Pro 256GB in Raid 0, 6x WD Re 4TB in Raid 1 || Sound: Xonar Essense STX (Mainly for Troubleshooting and listening test) || PSU: Corsair Ax1500i

CPU: Core i7 5820k @ 4.7GHz || GPU: Dual Titan X || Motherboard: Asus X99 Deluxe || RAM: 32GB Crucial Ballistix Sport || Monitors: MX299Q, 29UB65, LG 34UM95 || Storage: Dual Samsung 850 EVO 1 TB in Raid 0, Samsung 850 EVO 250GB, 2TB Toshiba scratch disk, 3TB Seagate Barracuda || PSU: EVGA 1000w PS Platinum

Link to comment
Share on other sites

Link to post
Share on other sites


if($condition){

//code

}else{

//code

}

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

I personally format with the latter way,

 

   }

else

   {

 

That's just me though.

Agreed with the Latter type format.

 

Even looks good in Ruby

PEWDIEPIE DONT CROSS THAT BRIDGE

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

I've personally never seen production real code formatted like any of the posts so far. Of course, anything can work if it is used consistently...

if (condition) {  // foo} else if (condition) {  // bar} else {  // baz}

Or:

if (condition){  // foo}else if (condition){  // bar}else{  // baz}
Link to comment
Share on other sites

Link to post
Share on other sites

There isn't a right or wrong way but saying that most coding standards include the use of:

if (condition) {    //Code here} else {    //Code here}

Examples:

 

http://pear.php.net/manual/en/standards.control.php

http://www.php-fig.org/psr/psr-2/

https://make.wordpress.org/core/handbook/coding-standards/php/#brace-style

http://framework.zend.com/manual/1.12/en/coding-standard.coding-style.html

Link to comment
Share on other sites

Link to post
Share on other sites

I use the following:

if (condition){   //code   //code}else{   //code   //code}

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

First one.

If (Agree){   LikeMyComment();}else{   Sad = true;}

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Yea the former. It's a coding standard all my programmers must adhere to, and which most programmers with a tertiary education usually use anyway.

Link to comment
Share on other sites

Link to post
Share on other sites

I am more of a

if (condition) {  // Statement(s)} else {  // Statement(s)}
Kind of guy, better aesthetics IMHO.

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

 

I use the following:

if (condition){   //code   //code}else{   //code   //code}

This is my coding style in general. I feel like it's easier on the eyes. 

Link to comment
Share on other sites

Link to post
Share on other sites

AFAIK It's purely down to a mix in readability and personal preference.

 

Personally I'm a ..

if (condition) {    // code} elseif (condition) {    // code} else {    // code}

.. kind of guy, but there are many variants, especially when you get onto other things such as functions or other control structures.

Link to comment
Share on other sites

Link to post
Share on other sites

For PHP I use this for control structures:

 

if (condition) {

     //code;

} else {

    // code;

}

 

but, for classes and functions:

 

class ClassName
{
    public function FunctionName()
    {
        //code;
    }
}

Link to comment
Share on other sites

Link to post
Share on other sites

 

I use the following:

if (condition){   //code   //code}else{   //code   //code}

 

I usually do this with indentation . 

Link to comment
Share on other sites

Link to post
Share on other sites

if(){//code}else{//code}

In all seriousness though...

if () {    /* code */} else {    /* code */}

It's cleaner.

Link to comment
Share on other sites

Link to post
Share on other sites

I prefer this in php to be honest

if():    //indented codeelseif():    //indented codeelse:    //indented codeendif;

I also prefer using <? instead of <?php

i want to die

Link to comment
Share on other sites

Link to post
Share on other sites

I prefer this in php to be honest

if():    //indented codeelseif():    //indented codeelse:    //indented codeendif;

I also prefer using <? instead of <?php

 

Don't forget <?= ?>, that's extra usefull.

Link to comment
Share on other sites

Link to post
Share on other sites

I also prefer using <? instead of <?php

 

Get in to the habit of using:

<?php //code ?>

It might be a little longer but it also requires no additional configuration and is normally recommended in coding standards which are advisable to adhere to.

Link to comment
Share on other sites

Link to post
Share on other sites

Get in to the habit of using:

<?php //code ?>

It might be a little longer but it also requires no additional configuration and is normally recommended in coding standards which are advisable to adhere to.

um as of php5.5 you don't even need php tags. also no, as of php5.5 (at least for debian) there's no extra configuration for short tags or no tags.

get out of here

i want to die

Link to comment
Share on other sites

Link to post
Share on other sites

 

um as of php5.5 you don't even need php tags. also no, as of php5.5 (at least for debian) there's no extra configuration for short tags or no tags.

get out of here

 

Have fun finding a job where you can use the latest version of operating systems and software. Few decent sized companies use the latest software.

 

Plus as I mentioned, the basic and most framework specific coding standards dictate the use of the full tags. In a work environment your colleagues won't be too pleased when your work fails in CI testing because you are too lazy to write <?php

 

 

 

PHP code must always be delimited by the full-form, standard PHP tags:

 

Short tags are never allowed. For files containing only PHP code, the closing tag must always be omitted (See General standards).

http://framework.zend.com/manual/current/en/ref/coding.standard.html

 

 

No Shorthand PHP Tags

Important: Never use shorthand PHP start tags. Always use full PHP tags.

 

https://make.wordpress.org/core/handbook/coding-standards/php/

 

 

  • short_tag [PSR-1] PHP code must use the long

    <?php ?> tags or the short-echo <?= ?> tags; it must not use the other tag variations.

http://cs.sensiolabs.org/

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

 

Have fun finding a job where you can use the latest version of operating systems and software. Few decent sized companies use the latest software.

 

Plus as I mentioned, the basic and most framework specific coding standards dictate the use of the full tags. In a work environment your colleagues won't be too pleased when your work fails in CI testing because you are too lazy to write <?php

 

 

http://framework.zend.com/manual/current/en/ref/coding.standard.html

 

https://make.wordpress.org/core/handbook/coding-standards/php/

 

http://cs.sensiolabs.org/

 

last time i checked those were guidelines for a CMS and framework, neither of which I use. Also who said I want to get a job coding? I do it for fun.

i want to die

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×