Jump to content

Recommended Posts

People talk about standards compliant - mainly CSS / HTML.

 

But how is the best way to lay out php code? I.e the standards compliant way. I have been doing PHP for a year or so, but have only self-taught myself. I feel I know more advanced stuff and not the basics.

 

This is how I would do it now:

 



<?php

//document code as much as possible
$variableName=2;

//perform if statement
if($variableName==1)
{echo "Yes";}
else
{

if(1==2)
{
echo "ok";
}
else
{
echo "no";
}

}

?>

 

Does the layout of my code look like it was produced by a novice?

Yes it does.

Indentation helps a lot, so use it.

There are many differnt conventions regarding coding style. It's usually what you prefer, or - if you work in a team - what the team assumed as a standard

Here's example of what Zend Framework guidelines look like

http://framework.zend.com/wiki/display/ZFDEV/PHP+Coding+Standard+(draft)

You're going to get a lot of mixed answers - here's my translation:

<?php
//document code as much as possible
$variableName=2;
//perform if statement
if ($variableName==1) {
  echo 'Yes';
} else {
  if (1==2) {
    echo 'ok';
  } else {
    echo 'no';
  }
}
?>

 

Truth is, there's no right and wrong way - it ALL depends on YOU. The layout you feel comfortable with. How you label your variables. How you name your files. How you structure each script as well as in how you break your HTML and PHP as I've seen some people have one massive PHP script that uses echo to throw HTML to the browser - I think that's messy so I start and close PHP tags throughout scripts. When using a text editor that color codes everything it's soooo much easier to read the file.

Does the layout of my code look like it was produced by a novice?

 

Yes, you have no indentation.  It's hard to debug that way especially with a lot of code.

 

I tend to use Java's standard of formatting because that's what I learned first and use at work.

 

A lot of different frameworks, applications, and third party systems have their own standards.  i.e. Drupal - Coding standards.

If you don't indent properly, you are going to run into problems.

 

 

Using PHP Formatter, gives:

<?php
  //document code as much as possible
  $variableName = 2;
  
  //perform if statement
  if ($variableName == 1) {
      echo "Yes";
  } else {
      if (1 == 2) {
          echo "ok";
      } else {
          echo "no";
      }
  }
?>

 

See how much easier it is to see the flow?

 

Ken

Classes must be named by following the naming conventions.

 

The brace is always written on the line underneath the class name ("one true brace" form).

 

Every class must have a documentation block that conforms to the phpDocumentor standard.

 

Any code within a class must be indented the standard indent of four spaces.

 

Only one class is permitted per PHP file.

What crap! There's absolutely NO way I'm going to go for their course now. Thanks Mchl - that just made my mind up for me. ;)

Not to completely go with the flow or anything, but first off I agree that identation definitely makes the difference.

 

Additionally I'd recommend and there are some examples in this post, for things like conditionals, I believe it is MUCH easier to read and understand if you put spaces with the operators.

<?php
if ($variableName==1) {
?>

Versus

<?php
if ($variableName == 1) {
?>

 

When you have multiple lines of code, that bit of separation makes it easier on the eyes to identify problems, particularly if your editor doesn't syntax highlight.

Woooah...

 

So many answers.

 

The easier the question.. the more answers you tend to get.

 

I once wrote a really lengthy, difficult question - would take 10 minutes just to read - and nobody replied!

 

Thanks for all the responses.

 

kenrbnsn's example is how I tend to remember most professionally created code looking like.

 

 

Just another thing - I have seen this used, what do people think of this:

 

instead of

 

<?php
//various code etc
$v=1;
if(1==1)
{
echo "<a href='link.html'>Link</a>";
}
//more code
$a=1;
?>

 

they have used...

 

<?php
//various code etc
$v=1;
if(1==1)
{
?>
<a href='link.html'>Link</a>
<?php
}
//more code
$a=1;
?>

 

Is that ok. Although it tends to be when displaying a lot of html, not just one line.

 

 

The easier the question.. the more answers you tend to get.

 

Makes sense you get a larger range of skill.

 

Is that ok. Although it tends to be when displaying a lot of html, not just one line.

 

No, like every suggested, you need indentation!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.