Senin, 12 Mei 2014

macam-macam PHP lengkap penjelasan

  • PHP Intro
  • PHP Installation
  • Web Server Setup
  • PHP Syntax
  • PHP Variables
  • PHP String
  • PHP Operators
  • PHP If..Else
  • PHP Switch
  • PHP Arrays
  • PHP Looping
  • While Loops
  • For Loops
  • PHP Functions
  • PHP Forms
  • PHP $_GET
  • PHP $_POST

    • PHP INTRO
    What is PHP?
    •PHP stands for PHP: Hypertext Preprocessor
    •PHP is a server-side scripting language, like ASP
    •PHP scripts are executed on the server
    •PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
    •PHP is an open source software
    •PHP is free to download and use
    What is a PHP File?
    •PHP files can contain text, HTML tags and scripts
    •PHP files are returned to the browser as plain HTML
    •PHP files have a file extension of “.php”, “.php3″, or “.phtml”
    What is MySQL?
    •MySQL is a database server
    •MySQL is ideal for both small and large applications
    •MySQL supports standard SQL
    •MySQL compiles on a number of platforms
    •MySQL is free to download and use
    PHP + MySQL
    •PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)
    Why PHP?
    •PHP runs on different platforms (Windows, Linux, Unix, etc.)
    •PHP is compatible with almost all servers used today (Apache, IIS, etc.)
    •PHP is FREE to download from the official PHP resource: www.php.net
    •PHP is easy to learn and runs efficiently on the server side
    Where to Start?
    •To get access to a web server with PHP support, you can:
    •Install Apache (or IIS) on your own server, install PHP, and MySQL
    •Or find a web hosting plan with PHP and MySQL support
    • INSTALLATION
    What do We Need?
    •Download PHP
    •Download PHP for free here: http://www.php.net/downloads.php
    •Download MySQL Database
    •Download MySQL for free here: http://www.mysql.com/downloads/
    •Download Apache Server
    •Download Apache for free here: http://httpd.apache.org/download.cgi
    • SETUP THE WEB SERVER
    Xampp Control Panel

    Change Document Root

    Understanding Document Root

    LET’S CODE !
    • PHP Syntax
    •The PHP script is executed on the server, and the plain HTML result is sent back to the browser.
    Basic PHP Syntax
    •A PHP script always starts with <!–?php and ends with ?>. A PHP script can be placed anywhere in the document.
    •On servers with shorthand-support, you can start a PHP script with <? and end with ?>.
    •For maximum compatibility, we recommend that you use the standard form (<!–?php) rather than the shorthand form.
    •A PHP file must have a .php extension.
    •A PHP file normally contains HTML tags, and some PHP scripting code.
    •Below, we have an example of a simple PHP script that sends the text “Hello World” back to the browser:
    •Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another.
    •There are two basic statements to output text with PHP: echo and print.
    •In the example above we have used the echo statement to output the text “Hello World”
    • PHP Variables
    •PHP variables are used to hold values or expressions.  A variable can have a short name, like x, or a more descriptive name, like carName.
    Rules for PHP variable names:
    •Variables in PHP starts with a $ sign, followed by the name of the variable
    •The variable name must begin with a letter or the underscore character
    •A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
    •A variable name should not contain spaces
    •Variable names are case sensitive (y and Y are two different variables)
    •PHP has no command for declaring a variable. A variable is created the moment you first assign a value to it: $myCar=”Volvo”;
    •After the execution of the statement above, the variable myCar will hold the value Volvo.
    •Tip: If you want to create a variable without assigning it a value, then you assign it the value of null.
    •Let’s create a variable containing a string, and a variable containing a number:
    <?php
    $txt=”Hello World!”;
    $x=16;
    ?>
    PHP is a Loosely Typed Language
    •In PHP, a variable does not need to be declared before adding a value to it.
    •In the example above, notice that we did not have to tell PHP which data type the variable is.
    •PHP automatically converts the variable to the correct data type, depending on its value.
    •In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.
    •A string variable is used to store and manipulate text.
    •String Variables in PHP
    •String variables are used for values that contain characters.
    •In this chapter we are going to look at the most common functions and operators used to manipulate strings in PHP.
    •After we create a string we can manipulate it. A string can be used directly in a function or it can be stored in a variable.
    <?php
    $txt=”Hello World”;
    echo $txt;
    ?>
    •The Concatenation Operator
    •There is only one string operator in PHP.
    •The concatenation operator (.)  is used to put two string values together. To concatenate two string variables together, use the concatenation operator:
    <?php
    $txt1=”Hello World!”;
    $txt2=”What a nice day!”;
    echo $txt1 . ” ” . $txt2;
    ?>
    •The output of the code above will be:
    Hello World! What a nice day!
    • PHP Operators





    • PHP If..Else Statements
    Normal
    0 false
    false
    false
    EN-US
    X-NONE
    X-NONE
    MicrosoftInternetExplorer4
    /* Style Definitions */
    table.MsoNormalTable
    {mso-style-name:”Table Normal”;
    mso-tstyle-rowband-size:0;
    mso-tstyle-colband-size:0;
    mso-style-noshow:yes;
    mso-style-priority:99;
    mso-style-qformat:yes;
    mso-style-parent:””;
    mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
    mso-para-margin-top:0cm;
    mso-para-margin-right:0cm;
    mso-para-margin-bottom:10.0pt;
    mso-para-margin-left:0cm;
    line-height:115%;
    mso-pagination:widow-orphan;
    font-size:11.0pt;
    font-family:”Calibri”,”sans-serif”;
    mso-ascii-font-family:Calibri;
    mso-ascii-theme-font:minor-latin;
    mso-hansi-font-family:Calibri;
    mso-hansi-theme-font:minor-latin;}
           Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In PHP we have the following conditional statements:
           if statement – use this statement to execute some code only if a specified condition is true
           if…else statement – use this statement to execute some code if a condition is true and another code if the condition is false
           if…elseif….else statement – use this statement to select one of several blocks of code to be executed
           switch statement – use this statement to select one of many blocks of code to be executed
    • PHP Switch Statements
     
    PHP Array
    What is an Array
    A variable is a storage area holding a number or text. The problem is, a variable will hold only one value.
    An array is a special variable, which can store multiple values in one single variable.
    If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
    Array in PHP
    In PHP, there are three kind of arrays:
    1. Numeric array – An array with a numeric index
    2. Associative array – An array where each ID key is associated with a value
    3. Multidimensional array – An array containing one or more arrays
    1. Numeric Array
    A numeric array stores each array element with a numeric index.
    - How to create a numeric array:
    • Method 1:
    $cars=array(“Saab”,”Volvo”,”BMW”,”Toyota”);
    • Method 2:
    $cars[0]=”Saab”;
    $cars[1]=”Volvo”;
    $cars[2]=”BMW”;
    $cars[3]=”Toyota”;
    - How to Accessing Numeric Array :
    In the following example you access the variable values by referring to the array name and index:
    <?php
    $cars[0]=”Saab”;
    $cars[1]=”Volvo”;
    $cars[2]=”BMW”;
    $cars[3]=”Toyota”;
    echo $cars[0] . ” and ” . $cars[1] . “ are Swedish Cars.”;
    ?>
    Output:
    Saab and Volvo are Swedish cars.
    2. Associative Array
    An associative array, each ID key is associated with a value.
    When storing data about specific named values, a numerical array is not always the best way to do it.
    With associative arrays we can use the values as keys and assign values to them.
    - How to Create Associative Array:
    • Method 1:
    $ages = array(“Peter”=>32, “Quagmire”=>30, “Joe”=>34);
    • Method 2:
    $ages['Peter'] = “32″;
    $ages['Quagmire'] = “30″;
    $ages['Joe'] = “34″;
    - How to Accessing Associative Array :
    <?php
    $ages['Peter'] = “32″;
    $ages['Quagmire'] = “30″;
    $ages['Joe'] = “34″;
    echo “Peter is ” . $ages['Peter'] . ” years old.”;
    ?>
    Output:
    Peter is 32 years old.
    3. Multidimensional Array
    The array above would look like this if written to the output:
    Array
    (
    [mi] => Array
    (
    [0] => mi
    [1] => tekom
    [2] => ap
    )
    [akun] => Array
    (
    [0] => akun
    [1] => pajak
    )
    [adnis] => Array
    (
    [0] => adnis
    [1] => adnis2
    [2] => pemasaran
    )
    )
    How to accessing Multidimensional Array:
    echo “Apakah ” . $prodi[‘akun’][1] .  ” bagian dari Prodi MI?”;
    Output:
    Apakah pajak bagian dari Prodi MI?
    • PHP Looping
    Loops execute a block of code a specified number of times, or while a specified condition is true.
    PHP Loops
    Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task like this.
    In PHP, we have the following looping statements:
    1. while – loops through a block of code while a specified condition is true
    2. do…while – loops through a block of code once, and then repeats the loop as long as a specified condition is true
    3. for – loops through a block of code a specified number of times
    4. foreach – loops through a block of code for each element in an array
    1. While Loop
    The while loop executes a block of code while a condition is true.
    Syntax:
    while (condition)
    {
    code to be executed;
    }
    Example
    The example below defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:
    }Example
    }The example below defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:
    }
    <html>
    <body>
    <?php
    $i=1;
    while($i<=5)
    {
    echo “The number is ” . $i . “<br />”;
    $i++;
    }
    ?>
    </body>
    </html>
    }
    }
    2. Do…While
    }The do…while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true.
    }
    <?php
    $i=1;
    do
    {
    $i++;
    echo “The number is ” . $i . “<br />”;
    }
    while ($i<=5);
    ?>
    3. For Loop
    The for loop is used when you know in advance how many times the script should run.
    Syntax:
    for (init; condition; increment)
    {
    code to be executed;
    }
    Parameters:
    init:  Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop)
    condition:  Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
    increment:  Mostly used to increment a counter (but can be any code to be executed at the end of the iteration)
    Note: The init and increment parameters above can be empty or have multiple expressions (separated by commas).
    4. Foreach Loop
    • PHP Function
    The real power of PHP comes from its functions.
    In PHP, there are more than 700 built-in functions.
    PHP Built-in Functions
    For a complete reference and examples of the built-in functions, please visit our PHP Reference.
    To keep the script from being executed when the page loads, you can put it into a function. A function will be executed by a call to the function. You may call a function from anywhere within a page.
    • PHP Form & User Input
    The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.
    PHP Form Handling
    The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.
    • PHP $_GET & $_POST
    When to use method=”get”?
    When using method=”get” in HTML forms, all variable names and values are displayed in the URL.
    Note:
    This method should not be used when sending passwords or other sensitive information!
    However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.
    The get method is not suitable for very large variable values. It should not be used with values exceeding 2000 characters.

    When to use method=”post”?
    Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
    However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
    • PHP $_REQUEST Variable
    The predefined $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.
    The $_REQUEST variable can be used to collect form data sent with both the GET and POST methods.
    About these ads
     
         ditulis oleh: taufik asikin  

    Tidak ada komentar:

    Posting Komentar