Php w3 School

download Php w3 School

of 129

  • date post

    07-Jul-2018
  • Category

    Documents

  • view

    215
  • download

    0

Embed Size (px)

Transcript of Php w3 School

  • 8/19/2019 Php w3 School

    1/129

    PHP 5 Tutorial (HOME) http://www.w3schools.com/php/php_includes.asp

     !3"chools Home  #e$t %hapter &

    PHP is a ser'er scriptin lanuae and is a power*ul tool *or ma+in d,namic and interacti'e

    !e- paes uic+l,.

    PHP is a widel,used *ree and e**icient alternati'e to competitors such as Microso*t0s 1"P.

    Eas, 2earnin with "how PHP

    Our "how PHP tool ma+es it eas, to learn PHP it shows -oth the PHP source code and the HTM2 output o* the code.

    E$ample

    46O%T7PE html8

    4html8 4-od,8

    49php echo M, *irst PHP script 98

    4/-od,8 4/html8 O/P M, *irst PHP script

    INTRO

    PHP scripts are e$ecuted on the ser'er.

    What You Should Already Know

    ;e*ore ,ou continue ,ou should ha'e a -asic understandin o* the *ollowin:

    • HTM2

    • %""

  • 8/19/2019 Php w3 School

    2/129

    • PHP is an acron,m *or PHP H,perte$t Preprocessor

    • PHP is a widel,used open source scriptin lanuae

    • PHP scripts are e$ecuted on the ser'er 

    • PHP costs nothin it is *ree to download and use

    PHP is an amazing and popular language!

    =t is power*ul enouh to -e at the core o* the -iest -loin s,stem on the we- (!ordPress)

    =t is deep enouh to run the larest social networ+ (?ace-oo+)

    =t is also eas, enouh to -e a -einner0s *irst ser'er side lanuae

    What is a PHP File?

    • PHP *iles can contain te$t HTM2 %""

  • 8/19/2019 Php w3 School

    3/129

    • PHP can encr,pt data

    !ith PHP ,ou are not limited to output HTM2. 7ou can output imaes P6? *iles and e'en

    ?lash mo'ies. 7ou can also output an, te$t such as @HTM2 and @M2.

    Why PHP?

    • PHP runs on 'arious plat*orms (!indows 2inu$ Ani$ Mac O" @ etc.)

    • PHP is compati-le with almost all ser'ers used toda, (1pache ==" etc.)

    • PHP supports a wide rane o* data-ases

    PHP is *ree. 6ownload it *rom the o**icial PHP resource: www.php.net

    • PHP is eas, to learn and runs e**icientl, on the ser'er side

    SYNTAX

    The PHP script is e$ecuted on the ser'er and the plain HTM2 result is sent -ac+ to the -rowser.

    ;asic PHP ",nta$

    1 PHP script can -e placed an,where in the document.

    1 PHP script starts with :

    49php // PHP code oes here 98

    The de*ault *ile e$tension *or PHP *iles is .php.

    1 PHP *ile normall, contains HTM2 tas and some PHP scriptin code.

    ;elow we ha'e an e$ample o* a simple PHP *ile with a PHP script that uses a -uiltin PHP *unction echo to output the te$t Hello !orld on a we- pae:

    E$ample

    46O%T7PE html8 4html8 4-od,8

    http://www.php.net/ http://www.php.net/

  • 8/19/2019 Php w3 School

    4/129

    4hB8M, *irst PHP pae4/hB8

    49php echo Hello !orld

    98

    4/-od,8 4/html8

    Run eample 

    No"e# PHP statements are terminated -, semicolon (). The closin ta o* a -loc+ o* PHP code also automaticall, implies a semicolon (so ,ou do not ha'e to ha'e a semicolon terminatin the last line o* a PHP -loc+).

    %omments in PHP

    1 comment in PHP code is a line that is not read/e$ecuted as part o* the proram. =ts onl,  purpose is to -e read -, someone who is editin the code

    %omments are use*ul *or:

    • To let others understand what ,ou are doin  %omments let other prorammers

    understand what ,ou were doin in each step (i* ,ou wor+ in a roup)

    • To remind ,oursel* what ,ou did  Most prorammers ha'e e$perienced comin -ac+ to

    their own wor+ a ,ear or two later and ha'in to re*iure out what the, did. %omments can remind ,ou o* what ,ou were thin+in when ,ou wrote the code

    PHP supports three wa,s o* commentin:

    E$ample

    46O%T7PE html8 4html8 4-od,8

    49php // This is a sinle line comment

    C This is also a sinle line comment

    /D This is a multiple lines comment -loc+ 

    http://www.w3schools.com/php/showphp.asp?filename=demo_syntax http://www.w3schools.com/php/showphp.asp?filename=demo_syntax

  • 8/19/2019 Php w3 School

    5/129

    that spans o'er more than one line D/ 98

    4/-od,84/html8

    Run eample 

    PHP %ase "ensiti'it,

    =n PHP all userde*ined *unctions classes and +e,words (e.. i* else while echo etc.) are #OT casesensiti'e.

    =n the e$ample -elow all three echo statements -elow are leal (and eual):

    E$ample

    46O%T7PE html8 4html8 4-od,8

    49php E%HO Hello !orld4-r8 echo Hello !orld4-r8

    EcHo Hello !orld4-r8 98

    4/-od,8 4/html8

    Howe'er in PHP all 'aria-les are casesensiti'e.

    =n the e$ample -elow onl, the *irst statement will displa, the 'alue o* the color 'aria-le (this is  -ecause color %O2OF and co2OF are treated as three di**erent 'aria-les):

    E$ample

    46O%T7PE html8 4html8 4-od,8

    49php colorGred

    http://www.w3schools.com/php/showphp.asp?filename=demo_syntax_comments http://www.w3schools.com/php/showphp.asp?filename=demo_syntax_comments

  • 8/19/2019 Php w3 School

    6/129

    echo M, car is  . color . 4-r8 echo M, house is  . %O2OF . 4-r8 echo M, -oat is  . co2OF . 4-r8 98

    4/-od,84/html8 $aria%les

    Variables are "containers" for storing information:

    Example

  • 8/19/2019 Php w3 School

    7/129

    0ules for ,-, 'ariables:

    • A 'ariable starts #ith the 1 sign* follo#e& by the name of the 'ariable

    • A 'ariable name must start #ith a letter or the un&erscore character

    • A 'ariable name cannot start #ith a number

    • A 'ariable name can only contain alpha2numeric characters an& un&erscores $A2 * 324* an& / %

    • Variable names are case sensiti'e $1y an& 1 are t#o &ifferent 'ariables%

    0emember that ,-, 'ariable names are case2sensiti'e

    7reating $8eclaring% ,-, Variables

    ,-, has no comman& for &eclaring a 'ariable(

    A 'ariable is create& the moment you first assign a 'alue to it:

    Example

  • 8/19/2019 Php w3 School

    8/129

    ,-, automatically con'erts the 'ariable to the correct &ata type* &epen&ing on its 'alue(

    n other languages such as 7* 7!!* an& ;a'a* the programmer must &eclare the name an& type of the 'ariable before using it(

    ,-, Variables lobal

  • 8/19/2019 Php w3 School

    9/129

    e%ho '

  • 8/19/2019 Php w3 School

    10/129

    ,-, also stores all global 'ariables in an array calle& 1>L?@AL

  • 8/19/2019 Php w3 School

    11/129

    .hen* each time the function is calle&* that 'ariable #ill still ha'e the information it containe& from the last time the function #as calle&(

    Note: .he 'ariable is still local to the function(

    &'ho and prin" s"a"emen"s n ,-, there are t#o basic #ays to get output: echo an& print(

    n this tutorial #e use echo $an& print% in almost e'ery example(

  • 8/19/2019 Php w3 School

    12/129

    .he follo#ing example sho#s ho# to &isplay strings an& 'ariables #ith the echo comman&:

    Example

  • 8/19/2019 Php w3 School

    13/129

    $%ars=array2'7ol5o'9'W'9'1oyota'3!

    print $txt)! print '*@8'!

    ?&

    Run example »

    (a"a ")pes

  • 8/19/2019 Php w3 School

    14/129

    • An integer can be either positi'e or negati'e

    • ntegers can be specifie& in three formats: &ecimal $+32base&%* hexa&ecimal $+62base& 2 prefixe& #ith 3x% or octal $G2base& 2 prefixe& #ith 3%

    n the follo#ing example #e #ill test &ifferent numbers( .he ,-, 'ar/&ump$% functionreturns the &ata type an& 'alue of 'ariables:

    Example

  • 8/19/2019 Php w3 School

    15/129

    ,-, @ooleans

    @ooleans can be either .0FE or )AL

  • 8/19/2019 Php w3 School

    16/129

  • 8/19/2019 Php w3 School

    17/129

    .he ,-, strlen$% function

    .he strlen$% function returns the length of a string* in characters(

    .he example belo# returns the length of the string "-ello #orl&":

    Example

  • 8/19/2019 Php w3 School

    18/129

    ip: .he position of the string "#orl&" in the example abo'e is 6( .he reason that it is 6 $an& not I%* is that the first character position in the string is 3* an& not +(

    7omplete ,-,

  • 8/19/2019 Php w3 School

    19/129

  • 8/19/2019 Php w3 School

    20/129

    .he example belo# sho#s the &ifferent results of using the &ifferent arithmetic operators:

    Example

  • 8/19/2019 Php w3 School

    21/129

    Example

  • 8/19/2019 Php w3 School

    22/129

  • 8/19/2019 Php w3 School

    23/129

    e%ho $i! ,, outputs  ?&

    Run example »

    ,-, 7omparison ?perators