Coding style

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
meee
Forum Newbie
Posts: 18
Joined: Wed Feb 04, 2009 1:38 pm

Coding style

Post 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.
Last edited by meee on Sun Mar 06, 2011 8:05 am, edited 1 time in total.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Coding style

Post by Darhazer »

Read Clean Code by Robert Martin. Yes, the entire book.
And then we can talk about good looking code.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Coding style

Post 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
meee
Forum Newbie
Posts: 18
Joined: Wed Feb 04, 2009 1:38 pm

Re: Coding style

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Coding style

Post by Eran »

I prefer to keep the indentation of the markup (HTML), it looks more organized that way (to me).
meee
Forum Newbie
Posts: 18
Joined: Wed Feb 04, 2009 1:38 pm

Re: Coding style

Post 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>    
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Coding style

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply