Page 1 of 1

Coding style

Posted: Sun Mar 06, 2011 5:40 am
by meee
I would like to make my code a better look. I read some articles, but didn't find all information. Please could recommend some sites where I can find how to style php and html and mysql inside php script in a proper way.

Re: Coding style

Posted: Sun Mar 06, 2011 5:50 am
by Darhazer
Read Clean Code by Robert Martin. Yes, the entire book.
And then we can talk about good looking code.

Re: Coding style

Posted: Sun Mar 06, 2011 5:51 am
by Eran
Check out the coding conventions on the PEAR website. They are considered the standards for PHP
http://pear.php.net/manual/en/standards.php

Re: Coding style

Posted: Sun Mar 06, 2011 8:03 am
by meee
thank you, I found a lot of answers in the mentioned site.

I have just two more questions:

How do you implement php inside html block.

like this:

Code: Select all

      ...
      <div>
        <div>
          <p>
            <?php 
            if ($example==TRUE){
                echo "statement";
            } 
            ?>
          </p>
        </div>
      </div>
      ...
or like this:

Code: Select all

      ...
      <div>
        <div>
          <p>
<?php 
if ($example==TRUE){
    echo "statement";
} 
?>
          </p>
        </div>
      </div>
      ...
or other way?

Re: Coding style

Posted: Sun Mar 06, 2011 9:01 am
by Eran
I prefer to keep the indentation of the markup (HTML), it looks more organized that way (to me).

Re: Coding style

Posted: Mon Mar 07, 2011 1:47 pm
by meee
I need just one more answer to style my code in a proper way :). Is the following a common way or should I do different? I am confusing if it is ok to have so many opening and closing php tags inside the code, and this is just a tiny sniped of the code.

Code: Select all

<div>
  <!--some html code here !-->
       <li>title 1</li>
       <li>title 2</li>
       <li>
       <?php if($userId){
          ?>
          <a href="/exmployments.php"><a href="/exmployments.php"><?php=$LANG['employments']?></a></a>
          <?php          
        }else{
          ?>
          <a href="/login.php"><?php=$LANG['employments']?></a>
          <?php
        }
        ?>
        </li>
    </ul>
</div>    

Re: Coding style

Posted: Mon Mar 07, 2011 1:58 pm
by AbraCadaver
It is OK, but it might look a little better if you consider this: http://php.net/manual/en/control-struct ... syntax.php Some people like it, some don't.