Showing posts with label code ethic. Show all posts
Showing posts with label code ethic. Show all posts

Tuesday, July 29, 2008

PHP Coding Ethic

Everyone can be programmer...
But we need to ensure that our coding is easy to be follow up by others...
Okay, maybe you don't care about others, but what if YOU are the one following up other peoples code?
Any nice feeling? i bet ya ;)

Okay, this might be not the only standard way of making things looks nice,
but it is our way of doing things in the company.

---------------------
Bad Coding:
$var1 = 123;
$var2 = "xxxx";
$var3 = true;
Why?
  • Bad Naming:
    - Who would have known the purpose of the variable "var1"?
    - Variable type is difficult to know... especially long piece of codes...
Better Coding:
$iCounter = 123;
$sMsg = "xxxx";
$bCheck = true;
Why?
  • Good naming:
    - $i... indicate variable type of number
    - $iCounter indicate what it is used for..

Its best to have proper naming. We include the starting 1st charactor in our variable naming as the variable type. List of naming:
  • $i... - number
  • $f... - float
  • $s... - string
  • $b... - boolean
  • $m... - mixed type
  • $o... - object
-----------------

Bad Coding:
if($bCheck == true) { $xxx = 1;
echo $xxx; }

foreach($xx as $yy ){
echo $yy; }

echo (($bXx == true) ? (($xxx==1)? "1": (($bYy==$xxx-1)? "true": "false")) : "ok" );
Why?:
  • Bad spacing if($bCheck <-- look too compact
  • Bad curly bracket positining and indent...
  • Too compact formula, look cool!, but it is definitely not easy to debug if problem risest! Even for the developer him/herself!
Better Coding:
if ($bCheck == true)
{
$xxx = 1;
echo $xxx;
}

foreach ($xx as $yy)
{
echo $yy;
}


if ($bXx == true)
{
if ($xxx == 1)
{
echo "1";
}
else
{
if ($bYy==$xx-1)
{
echo "true";
}
else
{
echo "false";
}
}
}
else
{
echo "ok";
}


Why?
  • Easier for reading and debugging
  • More spacing and proper curly bracket positioning


Disclaimer:
Logos and photos used in this tutorial are copyright of their respective owner. The content published above is solely for education purpose. Feel free to close this website if you are not happy here.. :)


PHP Tutorial #3 - The Basic

Previous*

Okay, time to get your hand dirty :D

For Ubuntu / Debian distribution:
1st, the starting page for apache is default pointed /var/www/
it is configured in /etc/apache2/sites-available/default assigned as parameter of DocumentRoot
The default file to open for directory will be in /etc/apache2/mods-enabled/dir.conf as parameter of DirectoryIndex

PHP files need to be declared as .php extension by default.
The starting page will be index.php

Create a file in /var/www/index.php
Enter the content:
<?php
$sNo = 123;
echo $sNo;
?>
You will get output:
123
In this tutorial, we will only show you the very simple basic.
For more information, please refer to php documentation or tutorial found here:
http://www.php.net/manual/en/language.basic-syntax.php

In PHP, a variable type is not needed to define. You may reassign the variable at any time to any type. "$" indicate it is a variable. Each statement must end with ";"
Example:
$mVar = 123;
echo $mVar;
echo "\r\n";
$mVar = "xyz";
echo $mVar;
Output:
123
xyz
"echo" is the simple function like c print.
you may also use the function print to do it.

To call a function:
$mResult = myFunction([parameter...]);

To define your function:
Example:
function myFunction($param1, $param2)
{
$iResult = $param1 + $param2;
return $iResult;
}
To call it:
Example:
$iResult = myFunction(123, 555);
echo $iResult;
Output:
678

For comment, use c like concept:
// Line based comment
/*
Multiple line
comments...
*/

Operators

Operators in PHP is alike C.
Example:
$iResult = 123 + 555;
//result to 678

$iResult = 111 - 10;
//result to 101

$iResult = 500 / 2;
//result to 250

$iResult = 50 * 3;
//result to 150

$bResult = (1==2)
//result to false (boolean)

$bResult = (1==1)
//result to true (boolean)

$bResult = (1!=2)
//result to true

$bResult = (1!=1)
//result to false

// AND operator
$bResult = (true && false)
//result to false

$bResult = (true && true)
//result to true

// OR operator
$bResult = (true || false )
// result to true

$bResult = (false || false )
// result to false
Next*

Disclaimer:
Logos and photos used in this tutorial are copyright of their respective owner. The content published above is solely for education purpose. Feel free to close this website if you are not happy here.. :)