Learning PHP - Basic 1

7/14/10

Welcome back! In last posts we have learned how to create database in MySql, now we will learn about PHP, the code on server-side. PHP (PHP Hypertext Preprocessor) is code/script that running on server-side, the script will create an application that can be integrated with HTML, so the web will work dynamic, not static.

PHP Rule
There are 2 types of how to type PHP script,
  • Embedded Script
With putting the PHP tag between the HTML page, example :

<html>
<head></head>
<body>
<?php echo "Learn PHP"; ?>
</body>
  • Non Embedded Script
<?php
echo "Eat";
echo "Code";
echo "Sleep";
?>

Besides that, there are 4 type of how to type the php tag,
  • Standard style : <?php //code here ?>
  • Short Style : <? //code here ?>
  • Javascrip Style : <script language  = 'PHP'>....</script>
  • ASP Style : <% .. %>
Output Syntax
Well there are 3 output syntax in PHP. The output syntax will display the value of variable, text, or anything that we define
  • echo
echo "$data";
echo "$member";
  • print
print "$data";
print "$member";
  • printf
printf "$data";
printf "$member";

In those example, the point is we will display the value of variable data and variable member to the browser.

Variable and Datatype
Variable is used to store the data which are temporary. The data will be gone after the program executed. There are some rule in variable :
  • Begins with character $
  • Case Sensitive, so $a is different with $A
  • First character must be letter or underline character  ( _ )
  • The next character can be letter,number, or underline character
Variable declaration must be done when the variable been called at the first time. Variable Initialization is fill value to the variable a the first time, below are some example :
  • $firstname = "Tom";
  • $middlename = "&";
  • $lastname = "Jerry";
Variable has some datatypes,
  • Integer
Contains all of the integer. Example :
$length = 10;
$radius = 10;
  • Float
Contains all of decimal number. It can be represented in rank numbers. Example :
$pi = 3.14;
$rank = 13.0E-3;
  • String
To represnt string or char type. It always flanked by single quotes (' ') or double quotes (" "). Example
$name = "Lori"
$string2 = 'Mason'
  • Array
Contain some value in a variable. The index and value of array can be String or Integer. If index is not initialization, it start with 0. Example :
$array = array(1,5,2,4); //first index is 0 with the value 1, second index is 1 with the value 5

In the next lesson, we will learn another basic from PHP. Stay Tune!


No comments:

 

Tags