Tuesday, July 29, 2008

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.. :)

No comments: