lec 1what is php

Upload: melek-melegim-melegim

Post on 07-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 Lec 1What is PHP

    1/52

    What is PHP?

  • 8/4/2019 Lec 1What is PHP

    2/52

    What is PHP?

    PHP stands for PHP: Hypertext Preprocessor

    PHP is a server-side scripting language, likeASP

    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

  • 8/4/2019 Lec 1What is PHP

    3/52

    What is a PHP File?

    PHP files can contain text, HTML tags andscripts

    PHP files are returned to the browser asplain HTML

    PHP files have a file extension of ".php",".php3", or ".phtml"

  • 8/4/2019 Lec 1What is PHP

    4/52

    What is MySQL?

    MySQL is a database server

    MySQL is ideal for both small and largeapplications

    MySQL supports standard SQL

    MySQL compiles on a number of platforms

    MySQL is free to download and use

  • 8/4/2019 Lec 1What is PHP

    5/52

    PHP + MySQL

    PHP combined with MySQL are cross-platform (you can develop in Windows andserve on a Unix platform)

  • 8/4/2019 Lec 1What is PHP

    6/52

    Why PHP?

    PHP runs on different platforms (Windows,Linux, Unix, etc.)

    PHP is compatible with almost all serversused today (Apache, IIS, etc.)

    PHP is FREE to download from the officialPHP resource: www.php.net

    PHP is easy to learn and runs efficientlyon the server side

    http://www.php.net/http://www.php.net/
  • 8/4/2019 Lec 1What is PHP

    7/52

    Where to Start?

    To get access to a web server with PHPsupport, you can:

    Install Apache (or IIS) on your own server,install PHP, and MySQL

    Or find a web hosting plan with PHP andMySQL support

  • 8/4/2019 Lec 1What is PHP

    8/52

    What do you Need?

    If your server supports PHP you don't need to doanything.

    Just create some .php files in your webdirectory, and the server will parse them for you.

    Because it is free, most web hosts offer PHPsupport.

    However, if your server does not support PHP,you must install PHP.

    Here is a link to a good tutorial from PHP.net onhow to install PHP5:http://www.php.net/manual/en/install.php

    http://www.php.net/manual/en/install.phphttp://www.php.net/manual/en/install.php
  • 8/4/2019 Lec 1What is PHP

    9/52

    Basic PHP Syntax

    Basic PHP Syntax A PHP scripting block always starts with . A PHP scripting block can be placedanywhere in the document.

    On servers with shorthand support enabled you can starta scripting block with . For maximum compatibility, we recommend that you use

    the standard form (

  • 8/4/2019 Lec 1What is PHP

    10/52

    Code Example

    Each code line in PHP must end with a semicolon. The semicolon isa 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 tooutput the text "Hello World".

  • 8/4/2019 Lec 1What is PHP

    11/52

    Variables in PHP

    Variables are used for storing a values, like textstrings, numbers or arrays.

    When a variable is declared, it can be used over

    and over again in your script. All variables in PHP start with a $ sign symbol.

    The correct way of declaring a variable in PHP:

    $var_name = value;

    New PHP programmers often forget the $ sign atthe beginning of the variable. In that case it willnot work.

  • 8/4/2019 Lec 1What is PHP

    12/52

    Let's try creating a variable containing astring, and a variable containing a number:

    In PHP, the variable is declared

    automatically when you use it.

  • 8/4/2019 Lec 1What is PHP

    13/52

    Naming Rules for Variables

    A variable name must start with a letter oran underscore "_"

    A variable name can only contain alpha-numeric

    characters and underscores (a-z, A-Z, 0-9, and _)

    A variable name should not containspaces. If a variable name is more than

    one word, it should be separated with anunderscore ($my_string), or withcapitalization ($myString)

  • 8/4/2019 Lec 1What is PHP

    14/52

    String Variables in PHP

    String variables are used for values thatcontains characters.

    After we create a string we can manipulateit. A string can be used directly in afunction or it can be stored in a variable.

    Below, the PHP script assigns the text"Hello World" to a string variable called$txt:

  • 8/4/2019 Lec 1What is PHP

    15/52

  • 8/4/2019 Lec 1What is PHP

    16/52

    The Concatenation Operator

    There is only one string operator in PHP.

    The concatenation operator (.) is used to puttwo string values together.

    To concatenate two string variables together,use the concatenation operator:

  • 8/4/2019 Lec 1What is PHP

    17/52

    The strlen() function

    The strlen() function is used to returnthe length of a string.

    Let's find the length of a string:

  • 8/4/2019 Lec 1What is PHP

    18/52

    The strpos() function

    The strpos() function is used to search for characterwithin a string.

    If a match is found, this function will return theposition of the first match. If no match is found, it

    will return FALSE. Let's see if we can find the string "world" in our

    string:

    See Complete String Reference

    http://www.w3schools.com/php/php_ref_string.asp

  • 8/4/2019 Lec 1What is PHP

    19/52

    PHP Operators

    PHP Operators

  • 8/4/2019 Lec 1What is PHP

    20/52

  • 8/4/2019 Lec 1What is PHP

    21/52

  • 8/4/2019 Lec 1What is PHP

    22/52

  • 8/4/2019 Lec 1What is PHP

    23/52

  • 8/4/2019 Lec 1What is PHP

    24/52

    PHP If...Else Statements

    Conditional Statements

    if statement - use this statement to execute somecode only if a specified condition is true

    if...else statement - use this statement to executesome code if a condition is true and another code ifthe condition is false

    if...elseif....else statement - use this statement toselect one of several blocks of code to be executed

    switch statement - use this statement to select oneof many blocks of code to be executed

  • 8/4/2019 Lec 1What is PHP

    25/52

    The if Statement

  • 8/4/2019 Lec 1What is PHP

    26/52

    The if...else Statement

  • 8/4/2019 Lec 1What is PHP

    27/52

  • 8/4/2019 Lec 1What is PHP

    28/52

    The if...elseif....else Statement

  • 8/4/2019 Lec 1What is PHP

    29/52

    PHP Switch Statement

  • 8/4/2019 Lec 1What is PHP

    30/52

    PHP Arrays

    A variable is a storage area holding a number ortext. The problem is, a variable will hold only onevalue.

    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 variablescould look like this:

    $cars1="Saab";$cars2="Volvo";$cars3="BMW";

  • 8/4/2019 Lec 1What is PHP

    31/52

    In PHP, there are three kind of arrays

    Numeric array - An array with a numericindex

    Associative array - An array where each IDkey is associated with a value

    Multidimensional array - An array containing

    one or more arrays

  • 8/4/2019 Lec 1What is PHP

    32/52

    Numeric Arrays

    A numeric array stores each array elementwith a numeric index.

    There are two methods to create anumeric array.

    In the following example the index areautomatically assigned (the index starts at 0):

    $cars=array("Saab","Volvo","BMW","Toyota");

  • 8/4/2019 Lec 1What is PHP

    33/52

    In the following example we assign theindex manually:

    $cars[0]="Saab";

    $cars[1]="Volvo";$cars[2]="BMW";$cars[3]="Toyota";

  • 8/4/2019 Lec 1What is PHP

    34/52

  • 8/4/2019 Lec 1What is PHP

    35/52

    Associative Arrays

    An associative array, each ID key isassociated with a value.

    When storing data about specific namedvalues, a numerical array is not always thebest way to do it.

    With associative arrays we can use the

    values as keys and assign values to them.

  • 8/4/2019 Lec 1What is PHP

    36/52

    $ages = array("Peter"=>32,"Quagmire"=>30, "Joe"=>34);

  • 8/4/2019 Lec 1What is PHP

    37/52

    $ages['Peter'] = "32";$ages['Quagmire'] = "30";$ages['Joe'] = "34";

  • 8/4/2019 Lec 1What is PHP

    38/52

  • 8/4/2019 Lec 1What is PHP

    39/52

    Multidimensional Arrays

    In a multidimensional array, each elementin the main array can also be an array.And each element in the sub-array can be

    an array, and so on.

  • 8/4/2019 Lec 1What is PHP

    40/52

    $families = array("Griffin"=>array("Peter","Lois","Megan" ),

    "Quagmire"=>array( "Glenn" ),"Brown"=>array("Cleve","Loret","Junior"));

  • 8/4/2019 Lec 1What is PHP

    41/52

    Array([Griffin] => Array([0] => Peter[1] => Lois[2] => Megan

    )[Quagmire] => Array([0] => Glenn)

    [Brown] => Array(

    [0] => Cleveland[1] => Loretta[2] => Junior)

    )

  • 8/4/2019 Lec 1What is PHP

    42/52

    Lets try displaying a single value from thearray above:

    echo "Is " . $families['Griffin'][2] ." a part of the Griffin family?";

    Is Megan a part of the Griffin family?

  • 8/4/2019 Lec 1What is PHP

    43/52

    PHP Looping - While Loops

    PHP Loops while - loops through a block of code while a

    specified condition is true

    do...while - loops through a block of codeonce, and then repeats the loop as long as aspecified condition is true

    for - loops through a block of code a specified

    number of times foreach - loops through a block of code for

    each element in an array

  • 8/4/2019 Lec 1What is PHP

    44/52

    While

  • 8/4/2019 Lec 1What is PHP

    45/52

    The do...while Statement

  • 8/4/2019 Lec 1What is PHP

    46/52

    The for Loop

  • 8/4/2019 Lec 1What is PHP

    47/52

    The foreach Loop

  • 8/4/2019 Lec 1What is PHP

    48/52

    PHP Functions

    The real power of PHP comes from its functions.

    In PHP, there are more than 700 built-infunctions.

    To keep the script from being executed when thepage loads, you can put it into a function.

    A function will be executed by a call to thefunction.

    You may call a function from anywhere within apage.

  • 8/4/2019 Lec 1What is PHP

    49/52

    Create a PHP Function

  • 8/4/2019 Lec 1What is PHP

    50/52

  • 8/4/2019 Lec 1What is PHP

    51/52

    Thi d Ch1 d 2 D f

  • 8/4/2019 Lec 1What is PHP

    52/52

    This ends Ch1 and 2 Demos ofCh1 and 2 Covered.

    Printing Example

    Comments

    Variables

    Strings

    Concatenation Operator

    Numbers Tax Rate Example

    Single And Double Quotation Marks

    Array and if and Loops