3 Txoj Hauv Kev Sau PHP Code

Cov txheej txheem:

3 Txoj Hauv Kev Sau PHP Code
3 Txoj Hauv Kev Sau PHP Code

Video: 3 Txoj Hauv Kev Sau PHP Code

Video: 3 Txoj Hauv Kev Sau PHP Code
Video: TUDev Tech Talk с профессором Борой Озкан - Финтех и будущее финансов 2024, Kaum ib hlis
Anonim

PHP yog cov lus server sau siv los tsim cov nplooj ntawv sib tham sib. Cov lus no tau dhau los ua neeg nyiam vim nws yooj yim siv, cuam tshuam hauv cov vev xaib, thiab nws koom ua ke nrog HTML. Xav txog dab tsi tshwm sim thaum nplooj ntawv ntawm lub vev xaib raug kho. Qab tus txheej txheem, muaj ntau (tej zaum ntau pua) ntawm PHP cov cai uas tswj hwm nplooj ntawv nplooj ntawv hloov pauv raws li ntau yam xwm txheej/xwm txheej. Qhov wikiHow no qhia koj yuav ua li cas sau qee tus lej PHP yooj yim yog li koj tuaj yeem nkag siab qhov pib ntawm PHP ua haujlwm li cas.

Kauj ruam

Txoj Kev 1 ntawm 3: Pib nrog kab lus "Echo"

Sau PHP Scripts Kauj Ruam 1
Sau PHP Scripts Kauj Ruam 1

Kauj Ruam 1. Qhib cov ntawv kho qhov program

Koj yuav siv txoj haujlwm no los sau thiab kho cov lej PHP.

  • Notepad tuaj yeem nkag mus rau ntau yam ntawm Windows ntawm txoj kev luv Yeej + R. Tom qab ntawd, ntaus "Notepad".
  • TextEdit tuaj yeem siv ntawm Mac khoos phis tawj los ntawm kev mus rau "Applications"> "TextEdit" folder.
Sau PHP Scripts Kauj Ruam 2
Sau PHP Scripts Kauj Ruam 2

Kauj Ruam 2. Ntaus nqe lus yooj yim rau hauv Notepad qhov rai

Segments hauv PHP code pib thiab xaus nrog cov cim PHP suav nrog hauv lub kaum sab xis (""). "Echo" yog cov lus yooj yim heev (hais kom ua rau lub khoos phis tawj) hauv PHP lus uas yuav tso saib cov ntawv ntawm lub vijtsam. Cov ntawv koj xav tso yuav tsum tau muab ntim rau hauv cov lus hais thiab xaus nrog tus lej cim.

Cov cai yuav zoo li no:

Sau PHP Scripts Kauj Ruam 3
Sau PHP Scripts Kauj Ruam 3

Kauj Ruam 3. Txuag cov ntawv nrog lub npe "helloteman" thiab txuas ntxiv.php

Koj tuaj yeem txuag nws los ntawm kev qhib "File" menu> "Save As …".

  • Hauv Notepad, ntxig rau. Php txuas ntxiv ntawm qhov kawg ntawm lub npe thiab muab nws tso rau hauv cov lus hais. Qhov no yuav tiv thaiv cov ntaub ntawv los ntawm kev hloov pauv mus rau cov ntawv sau ncaj qha los ntawm Notepad. Yog tsis muaj cov lus hais, cov ntaub ntawv yuav raug cawm nrog lub npe "nyob zoo phooj ywg.php.txt". Xwb, koj tuaj yeem xaiv cov ntawv qhia zaub mov nco hauv ntu "Txuag raws li hom" thiab hloov nws mus rau "Txhua Cov Ntaub Ntawv (*.*)" Kom cov ntaub ntawv tuaj yeem raug cawm hauv lub npe koj tau ntaus, thiab koj tsis xav tau rau ntxig quotes.
  • Hauv TextEdit, koj tsis tas yuav ntxig cov lus hais, tab sis lub qhov rai pop-up yuav tshwm los nug koj kom paub tseeb tias cov ntaub ntawv yuav tsum tau khaws tseg nrog.php txuas ntxiv.
  • Nco ntsoov tias koj khaws cov ntaub ntawv mus rau "server" cov ntaub ntawv teev npe hauv paus. Feem ntau, cov npe no yog cov npe hu ua "htdocs" hauv "Apache" nplaub tshev ntawm Windows, lossis "/Library/Webserver/Documents" ntawm Mac. Txawm li cas los xij, daim nplaub tshev tseem ceeb tuaj yeem teeb tsa lossis xaiv los ntawm tus neeg siv.
Sau PHP Scripts Kauj Ruam 4
Sau PHP Scripts Kauj Ruam 4

Kauj Ruam 4. Nkag mus rau PHP cov ntaub ntawv ntawm lub web browser

Qhib lub vev xaib uas xav tau thiab ntaus qhov chaw nyob hauv qab no rau hauv qhov chaw nyob bar siv lub npe ntawm PHP cov ntaub ntawv khaws tseg: https://localhost/halotemanteman.php. Qhov browser qhov rai yuav tso tawm cov lus "ncha" tom qab nws.

  • Yog tias koj tau txais cov lus yuam kev, nco ntsoov tias koj tau ntaus tus lej tseeb uas tau hais los saum no, suav nrog lub cim nyuv.
  • Tsis tas li, xyuas kom cov ntaub ntawv raug khaws tseg rau hauv phau ntawv teev npe raug.

Txoj Kev 2 ntawm 3: Siv PHP thiab HTML

Sau PHP Scripts Kauj Ruam 5
Sau PHP Scripts Kauj Ruam 5

Kauj Ruam 1. Nkag siab tag "php"

Tus chij "" qhia PHP lub cav tias qhov nkag los lossis lub ntsiab lus ntxiv hauv yog PHP code. Kev nkag los lossis cov ntsiab lus sab nraud ntawm cov cim no yuav raug kho raws li HTML thiab tsis quav ntsej los ntawm PHP lub cav thiab xa mus rau qhov browser ib yam li lwm cov lej HTML lossis kab ntawv. Qhov tseem ceeb uas koj yuav tsum paub yog tias tsab ntawv PHP lossis tus lej tau muab tso rau sab hauv nplooj ntawv HTML dawb.

Sau PHP Scripts Kauj Ruam 6
Sau PHP Scripts Kauj Ruam 6

Kauj Ruam 2. Nkag siab cov kab lus ntxiv ntawm cov cim

Cov kab lus tau siv los qhia PHP lub cav kom ua qee yam. Hauv kab lus "ncha", piv txwv li, koj tuaj yeem qhia lub tshuab luam ntawv lossis tso saib dab tsi uas tau muab tso rau hauv kab lus.

Lub cav PHP nws tus kheej tsis tau luam tawm dab tsi rau lub vijtsam. Cov zis tsim los ntawm lub cav xa mus rau qhov browser raws li HTML code. Qhov browser tsis "paub" tias cov ntsiab lus tau txais lossis cov cai yog PHP tso tawm. Dab tsi ntawm qhov browser nkag siab yog tias cov tswv yim nws tau txais yog HTML code yooj yim

Sau PHP Scripts Kauj Ruam 7
Sau PHP Scripts Kauj Ruam 7

Kauj Ruam 3. Siv cov cim HTML los hais txog nqe lus

Ntxiv cov cim HTML tuaj yeem hloov pauv cov lus ntawm PHP nqe lus. cov cim " ” “"Muaj nuj nqi siv cov ntawv sib txawv rau cov ntawv uas tau ntxiv ntawm ob. Nco ntsoov tias tus cim no tau ntxiv rau sab nraum cov ntawv uas yuav tsum tau ua siab tawv, tab sis sab hauv cov lus hais ntawm "ncha" nqe lus.

  • Koj tus lej yuav zoo li no:

    <php?

    ncha Nyob zoo cov phooj ywg!

    ";

    ?>

Sau PHP Scripts Kauj Ruam 8
Sau PHP Scripts Kauj Ruam 8

Kauj Ruam 4. Txuag thiab qhib cov ntawv hauv qhov browser

Xaiv cov ntawv qhia zaub mov "Cov Ntaub Ntawv"> "Txuag Raws Li …" thiab khaws cov ntawv ua "helloteman2.php", tom qab ntawd qhib nws hauv qhov browser nrog qhov chaw nyob hauv qab no: https://localhost/halotemanteman2.php. Cov lej tso tawm yuav yog zoo ib yam li cov cai yav dhau los, tab sis lub sijhawm no cov ntawv tau nthuav tawm hauv qhov loj.

Nco ntsoov tias koj khaws cov ntaub ntawv mus rau lub ntsiab "server" cov ntawv teev npe. Feem ntau, cov npe no yog cov npe hu ua "htdocs" hauv "Apache" folder ntawm Windows, lossis "/Library/Webserver/Documents" ntawm Mac. Txawm li cas los xij, daim nplaub tshev tseem ceeb tuaj yeem teeb tsa lossis xaiv los ntawm tus neeg siv

Sau PHP Scripts Kauj Ruam 9
Sau PHP Scripts Kauj Ruam 9

Kauj Ruam 5. Kho cov ntawv kom ntxiv lo lus thib ob "ncha"

Nco ntsoov tias txhua nqe lus yuav tsum tau muab cais los ntawm ib tus lej.

  • Koj tus lej yuav zoo li no:

    <? php cov

    echo "Nyob zoo, cov txiv neej!"

    ;

    echo "Koj nyob li cas?";

    ?>

Sau PHP Scripts Kauj Ruam 10
Sau PHP Scripts Kauj Ruam 10

Kauj Ruam 6. Txuag thiab khiav cov ntawv ua "hellofrienddobel.php"

Nplooj ntawv yuav tso tawm ob nqe lus "ncha" tso tawm ua ntu zus hauv ob kab ntawm kab ntawv. Ua tib zoo mloog cov cai"

Ntawm thawj kab. Cov cai yog HTML cim rau ntxig kab tshiab.

  • Yog tias koj tsis ntxiv nws, cov lej tso tawm yuav zoo li no:

    Nyob zoo cov neeg! Koj nyob li cas?

Txoj Kev 3 ntawm 3: Paub Txog Cov Cuam Tshuam

Sau PHP Scripts Kauj Ruam 11
Sau PHP Scripts Kauj Ruam 11

Kauj Ruam 1. Xav txog qhov hloov pauv raws li "ntim" ntawm cov ntaub ntawv

Txhawm rau tswj cov ntaub ntawv, ob tus lej thiab npe, koj yuav tsum khaws nws hauv "thawv". Cov txheej txheem no yog hu ua cov lus sib txawv. Cov syntax rau tshaj tawm qhov sib txawv yog "$ MyVariable =" Nyob zoo, phooj ywg! ";"

  • Tus lej ($) cim thaum pib ntawm tus lej qhia PHP tias "$ MyVariable" yog qhov sib txawv. Txhua qhov kev hloov pauv yuav tsum pib nrog tus lej cim, tab sis koj tuaj yeem tso npe hloov pauv nrog txhua lub npe.
  • Hauv qhov piv txwv saum toj no, cov ntaub ntawv uas twb muaj lawm yog "Nyob zoo, hais mav!" thiab qhov sib txawv siv yog "$ Variableku". Koj tab tom qhia PHP khaws cov ntaub ntawv ntawm sab xis ntawm lub cim sib npaug rau hauv qhov sib txawv ntawm sab laug ntawm lub cim sib npaug.
  • Cov ntawv hloov pauv uas muaj cov ntaub ntawv cov ntaub ntawv raug hu ua cov hlua.
Sau PHP Scripts Kauj Ruam 12
Sau PHP Scripts Kauj Ruam 12

Kauj Ruam 2. Npe lub npe sib txawv

Kev siv lossis siv cov kev hloov pauv hauv cov cai yog hu ua "hu" lossis "hu". Tshaj tawm koj cov kev hloov pauv, tom qab ntawd siv lossis "hu" lawv tsis txhob siv tus kheej ntaus cov ntawv.

  • Koj tus lej yuav zoo li no:

    $ Kuv qhov txawv txav = "Nyob zoo, cov phooj ywg!";

    ncha $ myvariable;

    ?>

Sau PHP Scripts Kauj Ruam 13
Sau PHP Scripts Kauj Ruam 13

Kauj Ruam 3. Txuag thiab khiav cov ntawv

Mus rau "File" menu> "Save As …" thiab khaws cov ntaub ntawv nrog lub npe "myfirstvariable.php". Qhib tus browser thiab mus ntsib https://localhost/mypertamavariabel.php. Tom qab ntawv, tsab ntawv lossis tus lej yuav tso saib qhov sib txawv hauv qhov browser browser. Cov ntawv tso tawm yuav zoo ib yam li cov ntawv dawb/ntawv dawb, tab sis cov txheej txheem tso saib lossis pom qhov sib txawv.

Nco ntsoov tias koj khaws cov ntaub ntawv mus rau lub ntsiab "server" cov ntawv teev npe. Feem ntau, cov npe no yog cov npe "htdocs" hauv "Apache" nplaub tshev ntawm Windows, lossis "/Library/Webserver/Documents" ntawm MacOS X. Txawm li cas los xij, daim nplaub tshev tseem ceeb tuaj yeem teeb tsa lossis xaiv los ntawm tus neeg siv

Sau PHP Scripts Kauj Ruam 14
Sau PHP Scripts Kauj Ruam 14

Kauj Ruam 4. Siv qhov sib txawv nrog cov lej

Cov lej kuj tseem tuaj yeem muaj tus lej (paub tias yog cov lej), thiab cov lej no tuaj yeem siv ua haujlwm siv cov lej lej yooj yim. Pib los ntawm kev tsim peb qhov hloov pauv npe "$ SmallNumber", "$ LargeNumber", thiab "$ Amount".

  • Koj tus lej yuav zoo li no:

    <? php cov

    $ SmallNumbers;

    $ BigNumber;

    $ Tus Nqi;

    ?>

Sau PHP Scripts Kauj Ruam 15
Sau PHP Scripts Kauj Ruam 15

Kauj Ruam 5. Muab tus nqi lossis tus lej cov ntaub ntawv rau thawj ob qhov sib txawv

Nkag tus lej cov ntaub ntawv mus rau qhov hloov pauv "$ SmallNumbers" thiab "$ BigNumbers".

  • Nco ntsoov tias cov ntaub ntawv tus lej tsis tas yuav tsum muab ntim rau hauv cov lus hais. Yog tias nyob nrog, cov lej yuav raug txiav txim siab ua cov ntaub ntawv xws li qhov sib txawv "Nyob zoo, phooj ywg!".
  • Koj tus lej yuav zoo li no:

    <? php cov

    $ LiteNumber = 12;

    $ BigNumber = 356;

    $ Tus Nqi;

    ?>

Sau PHP Scripts Kauj Ruam 16
Sau PHP Scripts Kauj Ruam 16

Kauj Ruam 6. Siv peb qhov sib txawv los xam thiab tso saib qhov sib piv ntawm lwm qhov kev hloov pauv

Hloov chaw ntawm kev suav koj tus kheej, koj tuaj yeem tso npe thawj ob qhov sib txawv hauv "$ Tus lej" sib txawv. Nrog rau kev ua lej, lub tshuab yuav suav qhov sib txawv ntawm ob qhov kev hloov pauv los ntawm nws tus kheej. Txhawm rau tso tawm qhov tshwm sim, koj tsuas yog xav tau ntxiv "echo" nqe lus uas rov qab hloov pauv tom qab tshaj tawm.

  • Kev hloov pauv cov lej yuav siv tau thaum koj tso saib qhov sib txawv "$ Tus lej" nrog nqe lus "ncha".
  • Koj tus lej yuav zoo li no:

    <? php cov

    $ LiteNumber = 12;

    $ BigNumber = 356;

    $ Sum = $ SmallNumbers + $ BigNumbers;

    echo $ Tus Nqi;

    ?>

Sau PHP Scripts Kauj Ruam 17
Sau PHP Scripts Kauj Ruam 17

Kauj Ruam 7. Khaws cov ntawv thiab khiav tsab ntawv lossis tus lej

Qhov browser qhov rai yuav tso tawm tsuas yog ib tus lej. Tus lej yog qhov tshwm sim ntawm qhov sib txawv ntawm ob qhov kev hloov pauv tau hais hauv qhov sib txawv "$ Tus Nqi".

Sau PHP Scripts Kauj Ruam 18
Sau PHP Scripts Kauj Ruam 18

Kauj Ruam 8. Txheeb xyuas cov ntawv sib txawv (hlua)

Los ntawm kev siv qhov sib txawv los khaws cov ntawv cov ntaub ntawv, koj tuaj yeem hais qhia qhov sib txawv thaum twg koj xav siv cov ntawv xov xwm yog li koj tsis tas yuav tsum sau cov ntaub ntawv ib txwm. Cov txheej txheem no tseem ua rau nws yooj yim dua rau koj los ua cov ntaub ntawv nyuaj ntau dua yav tom ntej.

  • Thawj qhov sib txawv, "$ MyVariable" muaj cov ntaub ntawv lossis cov hlua "Nyob zoo, phooj ywg!". Qhov sib txawv yuav ib txwm muaj cov ntawv "Nyob zoo, cov phooj ywg!" tshwj tsis yog koj hloov cov ntawv.
  • Nqe lus "ncha" yuav tso saib cov ntaub ntawv khaws tseg hauv qhov sib txawv "$ MyVariable".
Sau PHP Scripts Kauj Ruam 19
Sau PHP Scripts Kauj Ruam 19

Kauj Ruam 9. Txheeb xyuas tus lej lossis cov lej sib txawv

Koj tau hais txog cov hauv paus ntawm kev siv cov lej sib txawv uas siv cov lej ua lej. Cov ntaub ntawv los ntawm kev suav ua lej tuaj yeem raug cawm mus rau lwm qhov sib txawv. Qhov no tsuas yog qhov pib ntawm ntau qhov txiaj ntsig uas tuaj yeem tau txais los ntawm qhov sib txawv uas koj tsim.

  • Ob qhov sib txawv "$ SmallNumbers" thiab "$ BigNumbers" tau ntxiv nrog cov lej cov ntaub ntawv.
  • Qhov sib txawv thib peb, "$ Tus Nqi" khaws tag nrho "$ SmallNumbers" thiab "$ LargeNumbers". Txij li qhov sib txawv "$ SmallNumber" khaws thawj cov ntaub ntawv thiab "$ BigNumber" khaws cov ntaub ntawv thib ob, qhov sib txawv "$ Sum" muaj cov ntaub ntawv ntxiv thawj tus lej rau tus lej thib ob. Cov ntaub ntawv lossis tus nqi tuaj yeem hloov pauv los ntawm kev hloov pauv ntawm ib qho ntawm cov kev hloov pauv siv.

Lub tswv yim

  • Txog kab lus no, nws xav tias koj tau nruab Apache thiab PHP hauv koj lub computer. Thaum twg los xij koj tau ceeb toom kom khaws cov ntaub ntawv, koj yuav tsum khaws nws hauv "\ ht docs" (Windows) lossis "\ Library / WebServer / Documents" (Mac) phau ntawv teev npe, hauv Apache directory.
  • Cov lus pom yog lub hauv paus tseem ceeb ntawm txhua txoj haujlwm yog li ua kom ntseeg tau tias koj paub ntxiv cov lus hauv PHP.
  • Ib qho cuab yeej muaj txiaj ntsig zoo rau sim PHP cov ntaub ntawv yog XAMPP, ib qho program dawb uas teeb tsa thiab khiav Apache thiab PHP, thiab pab koj simulate server ntawm koj lub computer.

Pom zoo: