Beginner Programming Sever Side PHP5

History of PHP
• Rasmus Lerdorf(1995)
• PHP ( Personal Homepage)
• PHP3 ( 1998)
• Zeev suraski and Andi Gutmans(Zend)
• PHP4 ( 2000) Becoming very popular in web
• Expand to OOP ( Java, C#, VB.NET)
• Hence PHP5 emerge which is OOP support.
What newly at PHP5 ?
• New and improved MySQLextension
• PHP 5 bundles SQLite
• SimpleXMLextension (XML)
• Iteratorsand SPL
• Error Handling and Debugging
• Streams, Filters, and Wrappers


What needing diinstall ?
• Web Server (Apache, IIS)
• PHP5 Module
• Database Server (MySQL, PostgreSQL)
• Apache2Triad
• Sokkitv4.0
• Editor (Edit Plus / ZendStudio)


Declaration Variable
    <?
     $testing = 5; //integer
     echo gettype($testing);
     $testing = "five"; //string
     echo gettype($testing);
     $testing = 5.0; //double
     echo gettype($testing);
     $testing = true; //boolean
     echo gettype($testing);
    ?>

Declaration of Constanta
    <?
     define(“USERNAME”,”Algho”);
      echo “User Name :”.USERNAME;
    ?>
   

Casting
    <?
     $varpublic= 3.14;
      $vardouble= (double)$varpublic;
     echo gettype($vardouble); //double
     $varstring= (string)$varpublic;
     echo gettype($varstring); //string
    ?>


Operator

• Arithmetic (+, -, *, /)
• Concatenation (.)
• Assigment(+=, -=, /=, *=, %=, .=)
• Comparison (==, !=, >, <, >=, <=)
• Logical (||, &&, !)


If Statement

    <?
    $mood = "sad";
    if ($mood == "happy"){
    echo "your happy!";
    }elseif($mood== "sad"){
    echo "wow your suffer!";
    }else{
    echo "not happy and not suffer but $mood"; }
    ?>


Switch Statement
    <?
    $mood = "sad";
    switch($mood){
    case "happy":
    echo "Mood you good!";
    break;
    case "sad":
    echo "Mood you bad...";
    break;
    default:
    echo "Mood you $mood";
    }
    ?>


While Statement
    <?
    $counter = 1;
    while($counter<=12){
    echo "$counter * 2 is " .($counter*2)."<br>";
    $counter++;
    }
    ?>


Do Statement
    <?
    $num=201;
    do{
    echo ("Execution number: $num<br>\n");
    $num++;
    }while($num>200 && $num<400);
    ?>

For Statement
    <?
    for($counter=1;$counter<=12;$counter++){
    echo "$counter * 2 is : " .($counter*2)."<br>";
    }
    ?>


Break Statement
    <?
    $counter=1;
    while($counter<10){
    if($counter==5){
    echo "stop f counter values 5 <br>";
    break;
    }
    echo $counter."<br>";
    $counter++;
    }
    ?>

Continue Statement
    <?
    for($counter=1;$counter<=10;$counter++){
    if($counter==5) continue;
    echo "counter result : $counter<br>";}
    ?>

Usage of Function.
    <? //functionbyvalue.php
    function tax($salary){ //definisi
    $salary = $salary-(($salary/100)*20);
    return $salary;
    }

    $salary = 2000;
    echo tax($salary); //hasil1600
    echo $salary; //hasil2000
    ?>


Function Passing by Reference
    <? //functionbyreference.php
    function tax(&$salary){ //definisi
    $salary = $salary-(($salary/100)*20);
    return $salary;
    }
    $salary = 2000;
    echo tax($salary); //hasil1600
    echo $salary; //hasil1600
    ?>

Setting default parameter function
    <?
    function tax($salary=2000){ //definisi
    $salary = $salary-(($salary/100)*20);
    return $salary;
    }
    echo tax();
     ?>


Scope Variable
    <?
    $welcomemessage= "Hello World"; //global
    function translate(){
    $welcomemessage= "Halo Dunia"; //local
    return $welcomemessage;
    }
    translate();
    echo $welcomemessage;
    ?>

Nesting Function
    <? //nestingfunction.php
    function pension($total){
    function tax($salary){
    return $salary -(($salary/100)*20);
    }
    $posttax= tax($total);
    return tax(tax($total)-($posttax/100)*3);
    }
    $total = 2000;
    echo pension($total);
    ?>

Array
• Defenition of array() function
    $users = array(“tom”,”sharon”,”jhon”,”hary”);

• Identifier of array
    $user[] = “tom”; $user[] = “sharon”;
    $user[] = “jhon”; $user[] = “hary”;

• Example used array
    <? //foreach.php
    $user= array("tom","bert","sharon","jhon");
    foreach($user as $key=>$value){
    echo "Array Ke-".$key." isinya: ".$value."<br>"; }
    ?>

Statement While List
    <? //whilelist.php
    $user=     array(name>"bob",occupation=>"programmer",age=>30,hobby=>"swimming");
    while(list($index,$value) = each($user)){
    echo $index." : ".$value."<br>";
    }
    ?>

Function In Array
• implode -> combination array to string
• explode -> breaking string to array
• sort() -> sort array scr asc
• rsort() -> sort array scr desc
• ksort() -> sort index string array scr asc
• array_pop() -> delete array in the last character
• array_push() -> added array


Example Used Array
    <? //explodeimplode.php
    $city[0]="padang";
    $city[1]="dharmasraya";
    $city[3]="bukittinggi";
    $strcombination= implode("-",$city);
    echo "after combination: ".$strcombination."<br>";
    $cityarray= explode("-",$strcombination);
    foreach($cityarrayas $key=>$value){
    echo "Array to-".$key." = ".$value."<br>";
    }
    ?>

Take Data From Form (HTML)
    //formuser.php
    <html><body>
    <form action="lihat.php" method="POST">
    Name: <input type="text" name="name"><br>
    Addres: <input type="text" name="addres"><br>
    Password: <input type="password" name="password"><br>
    Gender:
    <input name="gender" type="radio" value="male">Male
    <input name="gender" type="radio" value="female">Female<br>
    Boldness:<br><textareaname="boldness"></textarea><br>
    <input type="submit"> <input type="reset">
    </form>
    </body></html>

    <? //lihat.php
    echo "<html></body>"; //file lihat.php
    if (empty($_POST["nama"])) $ket.="name still empety,";
    if (empty($_POST["password"])) $ket.="password still empety,";
    if (empty($_POST["hobi"])) $ket.="hobby still empety,";
    if (empty($_POST["alamat"])) $ket.="addres still empety,";
    if (empty($_POST["gender"])) $ket.="gender still empety,";
    if (empty($_POST["pilihan"])) $ket.="choice still empety ";
    if (empty($_POST["keterangan"])) $ket.="boldness still empety ";
    if (isset($ket)){
    echo "Error :".$ket;
    exit;
    }
    echo "your name: ".$_POST["nama"]."<br>";
    echo "your addres: ".$_POST["alamat"]."<br>";
    echo "your Password: ".$_POST["password"]."<br>";
    echo "your gender: ".$_POST["gender"]."<br>";
    echo "your Hobby:
    foreach($_POST["hobi"] as $key=>$value){
    echo "Hobby to-".$key."= ".$value."<br>";
    }
    echo "choice your President : ".$_POST["pilihan"]."<br>";
    echo "Boldnes:".$_POST["boldness"];
    echo "</body></html>";
    ?>



Register Global If Close ?

• Hence we cannot overcome variable directly
•$_GET[“varname”]
•$_POST[“varname”]
•$_SESSION[“varname”]
•$_COOKIE[“varname”]
•$_REQUEST[“varname”]
•$_FILES[“varname”]


Used Session (HTML)
    <html><body>
    <form action="prosessession.php" method="POST">
    User Name :<input type="text" name="username"><br>
    Password :<input type="password" name="password"><br>
    <input type="submit">
    </form>
    </body></html>


Used Session (PHP)
    <? //prosessession.php
    session_start();
    $username = $_POST["username"];
    $password = $_POST["password"];
    if ($username=="erick" && $password=="webdb"){
    $_SESSION["user"] = $username;
    header("Location: succseslogin.php");
    }else {
    echo "Sorry, you failed to login";
    }
?>

Set Cookies
    <? //setcookie.php
    setcookie("username[one]","erick",time()+60);
    setcookie("username[two]","jhon",time()+60);
    setcookie("username[three]","bart",time()+60);
    echo "Cookie telahdiset... <a href='lookcookie.php'>cookie</a>";
    ?>


Look Cookies
    <? //lihatcookie.php
    echo "After send Cookie : <br>";
    if (isset($_COOKIE["username"])){
    while(list($index,$value) = each($_COOKIE["username"])){
    echo "Name to-".$index." = ".$value."<br>";
    }
    }
    ?>

Used Header Authentication
    <? //headerauth.php
    if(!isset($PHP_AUTH_USER)){
    header("WWW-Authenticate: Basic realm=\"My Realm\"");
    header("HTTP/1.0 401 Unauthorized");
    echo("Textto send if user hits Cancel button\n");
    exit;
    }else {
    echo "<p>Hello $PHP_AUTH_USER</p>";
    echo "<p>You entered $PHP_AUTH_PW as pwd</p>";
    } ?>

No comments:

Post a Comment

Tinggalkan pesan anda untuk kami

Note: Only a member of this blog may post a comment.