Está en la página 1de 80

g.

RvinefaZe

WEB-daprogrameba
PHP

teqnikuri universiteti
1
saqarTvelos teqnikuri universiteti

g. RvinefaZe

WEB-daprogrameba
PHP

damtkicebulia stu-s
saredaqcio-sagamomcemlo
sabWos mier

Tbilisi
2009
2

uak 681.3.06

ganxilulia serveris mxareze daprogramebisaTvis gankuTvnili


specializebuli ena PHP-is bazisuri saSualebani.
gankuTvnilia informatikis da marTvis sistemebis fakultetis 2 2 . 0 2 ,
22.01, 0 7 1 9 . 0 8 specialobaTa Semswavleli studentebisa da am
sakiTxebiT dainteresebuli pirebisaTvis.

recenzenti: asoc. prof. e. Turqia

© gamomcemloba “teqnikuri universiteti”, 2009


ISBN
3

PHP4
Sesavali
arcTu didi xnis win Web-kvanZebidan momxmarebels adgilze
ZiriTadad statikuri HTML-failebi gadmoecemoda, romelTac igi
brouzeris meSveobiT mxolod kiTxulobda. dRes ki WEB-sivrceSi
umeteswilad dinamikuri WEB-furclebi anu DHTML-failebi
gadaigzavneba. maTi awyoba xdeba WEB-kvanZebis serverze
ganTavsebuli WEB-programebis gaSvebiT, rasac momxmarebeli
axorcielebs sakuTari kompiuteridan sistemasTan dialogSi.
internetSi programebis SeqmnisaTvis dRes yvelaze
popularuli WEB-daprogramebis ena gaxlavT PHP4. igi
gansakuTrebiT aadvilebs serverebze ganlagebul monacemTa
bazebTan urTierTobas.
dasawyisSi PHP (personal home page) Seiqmna, rogorc makrosebis
krebuli, martivi saxis piradi WEB-furclebis formirebisTvis.
Semdeg ki igi iqca srulfasovan daprogramebis enad, romelzec
dawerili konstruqciebi Caidgmeba serverze moTavsebul HTML-
teqstebSi. WEB-furclis gamoZaxebisas PHP-is preprocesoris mier
adgilze xdeba am konstruqciebis (programuli fragmentebis)
damuSaveba da HTML-is arsebul fragmentebs Soris Caemateba
Sesabamisi informacia, magaliTad, bazidan amoRebuli monacemebi,
serverze konstruirebuli grafikuli gamosaxulebebi da sxv.
PHP-is versia - PHP4(5) - misaerTebeli modulis saxiT
kompilirdeba dReisaTvis yvelaze popularul WEB-server-programa
Apache-sTvis. serverze PHP-programebi umeteswilad urTierToben
MySQL monacemTa bazasTan (romelic ufasod vrceldeba). ase rom,
Apache, MySQL da PHP triadas dRevandel dRes konkurenti ar hyavs.
Tumca, cxadia, PHP-s muSaoba SeuZlia sxva WEB-garemoSi da sxva
bazebTanac.
saerTod, PHP-programebs interpretatori amuSavebs, magram
SesaZlebelia maTi kompilirebac (Tumca es SesaZlebloba mxolod
komerciuli damatebis saxiT arsebobs), rac kidev ufro amaRlebs
sistemis mwarmoeblurobas.
CGI-sTan SedarebiT PHP-s is mniSvnelovani upiratesoba
gaaCnia, rom misi interpretatori funqcionirebs serverTan mWidro
integraciis pirobebSi _ saWiro ar gaxlavT TiToeuli programis
Sesrulebisas sistemis xelaxla gaSveba, rac mniSvnelovnad
amaRlebs muSaobis siswrafes.
PHP-s rigi sxva Rirsebebic gaaCnia:
• produqtis Riaoba;
• programebis daweris gaadvileba HTML-teqstisa da TviT
programuli nawilis gancalkevebis gamo;
• programebis erTi operaciuli sistemidan sxvaSi gadatanis
simartive;
• programebis Sesrulebis sakmarisi siswrafe.
4

P H P – is dayeneba
PHP-s ufasod CamovtvirTavT http://www.php.net kvanZidan.
http://www.php.net/manual/ misamarTze SesaZlebelia produqtis
Sesaxeb uaxlesi, amomwuravi informaciis miReba.
SeviswavloT, rogor xdeba PHP-is dayeneba Apache Web-
serverTan samuSaod, kompiuteris marTvisas Linux operaciuli
sistemis mier. (saerTod, SesaZlebelia Apache-sTan ukve mierTebuli
PHP-is variantis moZiebac, magram roca am process TviTon
vaxorcielebT, arCevani ufro farTo xdeba _ am SemTxvevaSi
parametrebs Cven vaniWebT mniSvnelobebs).
sistemaSi PHP-is dayenebis ufleba mxolod administrators
(supermomxmarebels) aqvs. PHP-is, rogorc Apache-sTvis modulis,
kompilireba ori gziT SeiZleba ganxorcieldes:
• pirveli gulisxmobs Apache-is xelaxla kompilacias masTan
PHP-is statikurad mierTebiT (linkirebiT);
• meore gza iTvaliswinebs mxolod PHP-is kompilacias,
oRond manamde unda ganxorcieldes Apache-is kompilacia
DSO reJimSi (Dynamic Shared Object - saerTo sargeblobis
dinamiuri obieqts aRniSnavs). swored, am reJimiT
visargeblebT PHP-is kompilirebisas. es gza PHP-is
dayenebis ufro martivi xerxia da Cvenc mas ganvixilavT.
imis Sesamowmeblad, uzrunvelyofs Tu ara Apache aRniSnul
reJimSi modulebis mierTebas, saWiroa Sesrulebaze gavuSvaT httpd
faili -1 almiT:
/www/bin/httpd -1
gamodis modulebis sia. Tu masSi figurirebs mod_so.c,
davaskvniT, rom DSO reJimSi muSaoba SesaZlebelia (winaaRmdeg
SemTxvevaSi mogviwevs Apache-is xelaxla kompilireba).
PHP4-is CamotvirTvis Sedegad gankargulebaSi gveZleva gzip
utilitiT SekumSuli Php-4.0.4pll.tar.gz distributiuli faili.
misi gaSla Semdegi brZanebiT xdeba:
tar -xvzf php-4.0.4pll.tar.gz
gadavdivarT PHP-distributivis katalogSi:
cd../php-4.0
Sesrulebaze vuSvebT aRniSnul katalogSi arsebul configure
programas parametrebisaTvis yvelaze ufro miRebuli
mniSvnelobebis miniWebiT:
./configure --enable-track-vars \
--with-gd \
--with-mysql \
--with-apxs=/www/bin/apxs
bolo mniSvneloba im katalogze miuTiTebs, romelSic Apache
Web-serveria dayenebuli, mysql bazisTvis ki igulisxmeba, rom igi
dumiliT gaTvaliswinebul katalogSia dayenebuli. Mmagram Tuki es
5
ase ar gaxlavT, saWiroa am gansxvavebuli katalogis Sesaxeb
informaciis gaTvaliswinebac:
--with-mysql=/path/to/dir
configure programis Sesrulebis Semdeg vuSvebT make utilitas.
kompiuterze dayenebuli unda iyos C enis kompilatori.

Apache serveris konfigurireba


Apache-serveris katalogis conf-qvekatalogSi movZebniT
httpd.conf fails da davumatebT mas Semdeg striqonebs:
AddType application / x-httpd-php .php
AddType application / x-httpd-php-source .phps
es niSnavs, rom PHP-interpretatori .php gafarToebis failebs
daamuSavebs rogorc programebs, xolo .phps gafarToebis mqoneT _
rogorc gaferadebul HTML-teqsts.
php.ini failis meSveobiT ki SesaZlebelia PHP-is parametrebis
Secvla ukve misi kompilirebis da dayenebis Semdeg. UNIX am fails
inaxavs /usr/local/lib katalogSi, xolo Windows operaciuli sistema -
Windows-Si.
SesaZlebelia am failis mimdinare katalogSi ganTavsebac.
maSin mas eqneba lokaluri moqmedebis areali. jerjerobiT ki Cven
vkmayofildebiT am failis parametrebisTvis miniWebuli standar-
tuli mniSvnelobebiT.
php.ini failSi SeiZleba mniSvnelobis gansazRvra Semdegi
direqtivisTvisac: short_open_tag=On (True,Yes), rac uzrunvelyofs
php-blokebis gaformebas <?Bbloki ?> saxiT. am SesaZleb-
lobisGgamorTva xorcieldeba direqtivisTvis Off (False,No)
mniSvnelobis miniWebiT.
Tu Web-furclebis formirebisaTvis XML-enasac viyenebT, maSin
am e.w. mokle tegebis gamoyeneba zemoT aRniSnuli xerxiTve unda
avkrZaloT da visargebloT mxolod standartuli tegebiT:
<?php
?>

Cveni pirveli programa PHP-ze


Notepad-teqstSi avkriboT Semdegi teqsti:
<?php
print “Hello Web!”;
?>
faili davimaxsovroT first.php saxeliT.
6
Tu serverze ar vmuSaobT, mogvixdeba FTP-servisiT failis
masze gadagzavna (kopireba). Semdeg ki am fails SeiZleba mivmarToT
Cveni brouzeridan. ekranze aisaxeba warwera:
Hello Web!
magram Tuki serverze PHP dayenebuli ar gaxlavT, anda failis
gafarToeba arasworad iqna gansazRvruli, maSin mosalodnelia
ekranze gamovides Semdegi teqsti:
<?php
Hello Web!
?>
print() warmoadgens funqcias Cven mier daweril umartives
programaSi. Tu mas ekranze striqoni gamohyavs, maSin es striqoni
brWyalebSi unda Caisvas (dasaSvebia erTmagi brWyalebic).
viciT, rom funqciis argumentebi frCxilebSi unda moTavsdes.
print funqcia ki am mxriv gamonaklisia - dasaSvebia mis mier
gamosayvani informacia frCxilebSi ar movaqcioT.
PHP-instruqciebi wertil-mZimiT unda dabolovdes. gamonakliss
SeiZleba warmoadgendes blokSi bolo instruqcia, Tumca es
rekomendirebuli ar gaxlavT (saeWvo SemTxvevebSi SeiZleba aseTma
gadawyvetilebam interpretatori SecdomaSi Seiyvanos).
iseve, rogorc JavaScript-is scenarebi, PHP-instruqciebis
blokebic SeiZleba uSualod HTML-teqstSi ganlagdes:

listingi 1
<html>
<head>
<title>gaumarjos PHP-isa da HTML-is Zmur kavSirs! </title>
</head>
</body>
<?php
print “win, programirebaSi axali mwvervalebis dasapyrobad!”;
?>
</body>
</html>
PHP-is interpretatori am furcelze mxolod PHP-blokebiT
“interesdeba”. maTi erToblioba ki qmnis erTian PHP-programas.
Sinaarsobrivad es imas niSnavs, rom pirvel blokSi gamocxadebuli
cvladebis, funqciebis Tu klasebis gamoyeneba momdevno blokebSic
SeiZleba.
programebSi xSirad gamoiyeneba komentarebi:
// es komentaria.
# Tqven warmoidgineT, esec komentaria.
/* es ki
ramdenime striqonze
7
gawelili komentaria.
*/
komentarebSi Cawerili informacia interpretatoris mier
ignorirdeba.

cvladebi
PHP-Si cvladis saxeli $ simboloTi iwyeba! Mmomdevno
poziciebze ki SeiZleba gamoviyenoT laTinuri simboloebi, cifrebi
da xazgasmis niSani. sxva simboloebis, maT Soris xarvezis
(carieli aris) gamoyeneba ar daiSveba. moviyvanoT cvladebis
sworad dasaxelebis magaliTebi:
$a
$ a_b_c
$ b555
cvladebs eniWebaT mniSvnelobebi. MmniSvnelobebi da
Sesabamisad cvladebic sxvadasxva tipis SeiZleba iyos. KkerZod,
cvladebSi dasaSvebia SevinaxoT: ricxvebi, simboloTa striqonebi,
obieqtebi, masivebi, logikuri mniSvnelobebi...
rogorc wesi, cvladis Seqmna da misTvis mniSvnelobis miniWeba
erTsa da imave instruqciaSi xdeba, magaliTad:
$num1 = 8;
$name = “gia”;
instruqciebSi mniSvnelobaminiWebuli cvladis gamoyeneba,
faqtobrivad, am mniSvnelobis gamoyenebaa. MmagaliTad, gamosaxuleba
print $num1;
ekranze gamoiyvans imave informacias (8), rasac - gamosaxuleba
print 8;

dinamikuri cvladebi
PHP4(5)-Si siaxlea - cvladis saxeli SeiZleba davimaxsovroT,
rogorc sxva cvladis mniSvneloba! kerZod, gamosaxuleba
$user=”bob”;
SeiZleba Secvlil iqnes Semdegi ori gamosaxulebiT:
$holder=”user”;
$$holder=”bob”;
instruqcia print $$holder; ekranze gamoiyvans bob mniSvnelobas.
gansxvavebul Sedegs mogvcems instruqcia
print “$$ holder”;
am SemTxvevaSi interpretators ekranze gamohyavs brWyalebSi
moqceuli cvladis mniSvneloba anu mocemul SemTxvevaSi $user
striqoni. Ee.i. interpretatori, ase vTqvaT, meore nabijs aRar dgams
- $user aRiqvas, rogorc cvladi, da gamogviyvanos misi mniSvneloba.
8
sainteresoa, rom dinamikuri cvladi SeiZleba gamocxaddes
striqonis (striqonuli mudmivis) meSveobiTac (gamoiyeneba
gansxvavebuli sintaqsi):
$ {“user”}=”bob”;
aseTi gadawyveta, rogorc Semdeg vaCvenebT, aadvilebs
dinamikuri cvladebis generirebis process ciklebisa da
konkatenciis operatoris gamoyenebiT.
cxadia, figurul frCxilebSi cvladic SeiZleba moTavsdes:
$ {$holder}
qvemoT programaSi moyvanilia dinamikuri cvladis Seqmnis
sxvadasxva magaliTi.
$holder cvladSi Senaxulia “user” striqoni, romelic $$holder
dinamikur cvlads aformirebs $user cvladis saxiT. am
ukanasknels eniWeba “bob” mniSvneloba.
Semdeg moyvanilia aRniSnuli cvladebis mniSvnelobebis
ekranze gamoyvanis aseve sxvadasxva xerxi:

listingi 2
<html>
<head>
<title> dinamikuri cvladis Seqmna da misdami mimarTva</title>
</head>
<body>
<?php
$holder=”user”;
$$holder=”bob”;

print “$user<br>”; //gamohyavs ”bob”


print “$$holder”; //gamohyavs ”bob”
print “<br>”;
print “$ {$holder}<br>”; //gamohyavs ”bob”
print “S {‘user’}<br>” ; //gamohyavs ”bob”
?>
</body>
</html>

PHP saSualebas iZleva, erTi cvladi meores daeyrdnos:


<?php
$ Ziritadi_cvladi=42;
$ Ziritadze_dayrdnobili=&Ziritadi_cvladi;
print $Ziritadze_ dayrdnobili; // gamodis 42
?>
9
monacemTa tipebi
monacemebis damuSavebisas PHP iTvaliswinebs maT tips. es ena
ar moiTxovs, rom Tavidanve gamocxaddes monacemTa tipi. saWiro
informacias PHP iRebs cvladisadmi miniWebuli mniSvnelobis
mixedviT. SevniSnoT, rom amgvar midgomas naklic gaaCnia – ar aris
gamoricxuli, didi moculobis programebSi programists
mxedvelobidan gamorCes, Tu rogoria ama Tu im cvladis tipi da
Sedegad programam arasworad gamoTvalos Sedegi.
CamovTvaloT PHP-Si gaTvaliswinebuli monacemTa tipebi:

tipi magaliTi aRwera


Integer 5 mTeli ricxvi
Double 3.111 mcuravwertiliani ricxvi
String “hello” simboloebis striqoni
Boolean true (false) logikuri tipi
Object obieqti
Array masivi

programists SeuZlia gettype() funqciis meSveobiT Seamowmos


monacemTa tipi:

Llistingi 3
<html>
<head>
<title>cvladis tipis Semowmeba </title>
</head>
<body>
<?php
$testing = 5;
print gettype ($testing); // integer
print “<br>”;
$testing = “five”;
print gettype ($testing ); // string
print (“<br>”);
$testing = 5.0;
print gettype ($testing ); //double
print (“<br>”);
$testing = true;
print gettype ($testing ); //Boolean
print “<br>”;
?>
</body>
</html>
10
es programa gamoitans Semdeg Setyobinebebs:
integer
string
double
boolean

monacemTa tipis Secvla


PHP-Si saWiroebis SemTxvevaSi SesaZlebelia monacemTa erTi
tipis meoreTi Secvla.

listingi 4
<html>
<head>
<title> cvladis tipis Secvla </title>
</head>
<body>
<?php
$undecided = 3.14;
print gettype( $undecided ); //double
print “ -- $undecided<br>” // 3.14
settype( $undecided, string );
print gettype ( $undecided ); // string
print “ -- $undecided<br>”; // 3.14
settype( $undecided, integer );
print gettype( $undecided ); // integer
print “ -- $undecided<br>”; //3
settype( $undecided, double );
print gettype( $undecided ); // double
print “ -- $undecided<br>”; // 3.0
settype( $undecided, boolean );
print gettype ( $undecided ); // boolean
print “ -- $undecided<br>”; // 1
?>
</body>
</html>
aRsaniSnavia, rom mocemul cvladze dayrdnobiT SesaZlebelia
SevqmnaT axali, magram gansxvavebuli tipis mqone cvladi. cxadia,
bazisuri cvladis tipi am SemTxvevaSi ucvleli rCeba (bunebrivia,
mniSvnelobac). axali, gansxvavebuli tipis cvladis mniSvneloba ki
ganisazRvreba bazisuri cvladis mniSvnelobis mixedviT.
moviyvanoT aseTi gardaqmnebis magaliTi:

listingi 5
<html>
<head>
<title> cvladis tipis gardaqmna </title>
11
</head>
<body>
<?php
$undecided = 3.14;
$holder = ( double ) $undecided;
print gettype( $holder ); // double
print “ -- $holder<br>”; // 3.14
$holder = ( string ) $undecided;
print gettype( $holder ); // string
print “ -- $holder<br> “; // 3.14
$holder = ( integer ) $undecided;
print gettype( $holder ); // integer
print “ -- $holder<br>”; // 3
$holder = ( double ) $undecided;
print gettype ( $holder ); // double
print “ -- $holder<br>”; // 3.14
$holder = ( boolean ) $undecided;
print gettype ($holder ); // boolean
print “ -- $holder<br>”; // 1
?>
</body>
</html>
gamosaxulebebi PHP4-Si sxva enebis msgavsad iqmneba.
Ggamoiyeneba
+, -, /, * da % (gayofa modulis mixedviT) operatorebi.
striqonis striqonze gadabma ki aq wertilis meSveobiT
xorcieldeba:
Print “hello”.”world”;
am instruqciis Sedegad ekranze aisaxeba
hello world
striqoni.
mniSvnelobis miniWebis Cveulebrivi operatori x=x+4; aqac
SeiZleba sxvagvarad warmogvidges:
x += 4;
cxadia, gamoiyeneba
-=, /=, *= da %= operatorebic.
PHP-Si funqcionirebs .= operatoric. magaliTad, $x = $x . “test”
gamosaxuleba SeiZleba Seicvalos $x .= “test” gamosaxulebiT.
gamosaxulebebSi gamoiyeneba CvenTvis nacnobi Sedarebis
operatorebi:
==, !=, >, >=, <, <=
12
aq siaxles warmoadgens === operatori, romelic amowmebs
operandebis ara marto mniSvnelobebis, aramed maTi tipebis
Tanxvedrasac!
rac Seexeba logikur operatorebs, || da && operatorebTan
erTad gamoiyeneba imave daniSnulebis mqone or da and logikuri
operatorebi (SevniSnoT, rom pirvelebs ufro maRali prioriteti
gaaCniaT).
“uaryofis” logikur operacias axorcielebs ! operatori,
xolo “gamomricxavi an” operacias (niSnavs, rom mxolod erTi
operandia WeSmariti) – xor logikuri operatori.
PHP-Si vxvdebiT agreTve CvenTvis ukve nacnob, specifikuri
saxis mqone miniWebis operatorebs:
$x++
$x--
--$
++$
zogjer saWiroa, programaSi Tavidan aviciloT cvladis
mniSvnelobis da tipis Secvla. AmaSin cvlads mxolod erTxel,
programis dasawyisSi mieniWeba mniSvneloba (am mniSvnelobis
mixedviT - tipic) da mere is aRar icvleba. cvladis aseTi
nairsaxeoba faqtobrivad konstantaa. PHP-Si mas asec uwodeben.
misi gansazRvrisas $ simbolos ar iyeneben da saxels didi
asoebisagan adgenen:

listingi 6
<html>
<head>
<title>konstantis Seqmna </title>
</head>
<body>
<?php
define ( “USER”, “Gerald” );
print “Welcome ”.USER;
?>
</body>
</html>
mivaqcioT yuradReba - gamobeWdvisas konstanta striqons
mivuerTeT konkatenciis operatoris meSveobiT.
aRvniSnoT, rom PHP ena programists awvdis e.w.
winaswargansazRvruli konstantebis gamoyenebis saSualebasac.
magaliTad: _FILE_ konstanta Seicavs im failis saxels,
romelsac mocemul momentSi amuSavebs interpretatori, _LINE_ ki -
failis mimdinare striqonis nomers da sxv.
13

nakadis marTva
programis Sesrulebis mimdinareoba, situaciidan gamomdinare,
SeiZleba Seicvalos. aqac, programirebis nacnob enebTan Sedarebas
Tu movaxdenT, gansakuTrebul siaxleebs ar vxvdebiT.
moviyvanoT IF instruqciis gamoyenebis magaliTi:

listingi 7
<html>
<head>
<title> if instruqcia </title>
</head>
<body>
<?php
$mood = “sad”;
if ($mood == “happy” )
{
print “me karg gunebaze var!”;
}
?>
</body>
</html>
radgan aq IF-is momdevno bloki erTaderT instruqcias Seicavs,
SeiZleboda figuruli frCxilebi arc gamogveyenebina.
programa odnav gavarTuloT else blokis daniSnulebis
sademonstraciod:

listingi 8
<html>
<head>
<title> if instruqcia else blokiT </title>
</head>
<body>
<?php
$mood = “dardianada vaar”;
if ($mood == “happy” )
{
print “me karg gunebaze var!”
}
else
{
print $mood;
}
?>
</body>
</html>
14
Semdegi nabijia elseif konstruqciasTan gacnoba, romelic if-
instruqciaSi, else-blokisgan gansxvavebiT, erTmaneTis miyolebiT
ramdenjermec SeiZleba Segvxvdes (qvemoT moyvanil magaliTSi ki
igi mxolod erTgan figurirebs):
listingi 9
<html>
<head>
<title> else da elseif blokebis gamoyeneba </title>
</head>
<body>
<?php
$mood = “sad”;
if ($mood == “happy” )
{
print “me karg gunebaze var!”;
}
elseif ( $mood == “sad” )
{
print “ar idardo!”;
}
else
{
print “gaugebaria es $mood ”;
}
?>
</body>
</html>
programis Sesrulebis mimdevroba SeiZleba SevcvaloT CvenTvis
ukve nacnobi switch-instruqciis meSveobiTac:

listingi 10
<html>
<head>
<title>switch instruqcia </title>
</head>
<body>
<?php
$mood = “sad”;
switch ( $mood )
{
case “happy”:
print “me karg gunebaze var!”;
break;
case “sad”:
print “ar idardo!”;
15
break;
default:
print “gaugebaria es $mood ”;
}
?>
</body>
</html>
didi popularobiT ar sargeblobs, magram unda vicnobdeT ?
operatorsac:
(pirobis-Semowmeba)? Ggamosaxuleba_1_dadebiTi_pasuxisas:
gamosaxuleba_2_uaryofiTi_pasuxisas;
vaCvenoT misi funqcionireba Semdegi programis magaliTze:

listingi 11
<html>
<head>
<title> ? operatori </title>
</head>
<body>
<?php
$mood = “sad”;
$text = ($mood == ”happy”)? “me karg gunebaze var!”: “xasiaTze ver var”;
print “$text”;
?>
</body>
</html>

ciklebi
kidev erTxel gadavavloT Tvali CvenTvis nacnob ciklebs:
cikli while
listingi 12
<html>
<head>
<title> while cikli </title>
</head>
<body>
<?php
$counter = 1;
while ( $counter <= 12 )
{
print “$counter gavamravloT orze, iqneba ”. ($counter*2). “<br>”;
$counter++;
}
?>
16
</body>
</html>

cikli do… while


listingi 13
<html>
<head>
<title> do… while cikli</title>
</head>
<body>
<?php
$num = 1;
do
{
print “gatarebis nomeria: $num<br>\n”;
$num++;
}
while ( $ > 200 && $num < 400 );
?>
</body>
</html>
xazi unda gavusvaT moTxovnas _ ciklis bolos aucileblad
unda davsvaT ; simbolo.
gavixsenoT, rom do … while ciklis gamoyenebiT ciklis sxeulSi
mocemuli instruqciebi erTxel mainc sruldeba, Tundac ar
kmayofildebodes testuri piroba (zemoT moyvanil programaSi
zustad aseTi ram xdeba).

For cikli
rac am ciklis meSveobiT xorcieldeba, do … while cikliTac
miiRweva. magram, radganac for cikli ufro advilad aRsaqmelia,
xSirad mas aZleven upiratesobas.
qvemo magaliTSi 2-ze mravldeba pirveli 12 naturaluri ricxvi:

listingi 14
<html>
<head>
<title> for cikli </title>
</head>
<body>
<?php
for ($counter = 1; $counter <=12; $counter ++ )
{
print “$counter-is orze gamravlebiT miviRebT ”.($counter*2). “<br>”;
}
17
?>
</body>
</html>

axla ki vaCvenoT for ciklSi break da continue instruqciebis


gamoyenebis magaliTebi:

listingi 15
<html>
<head>
<title> break instruqciis gamoyeneba </title>
</head>
<body>
<?php
for ($counter = -4; $counter <=10; $counter++)
{
if ($counter == 0)
break;
$temp = 4000 / $counter;
print “40000-is $counter-ze gayofiT miviRebT $temp-s<br>”;
}
?>
</body>
</html>
roca $counter cvladis mniSvneloba nulis toli gaxdeba,
cikli Sewydeba.

listingi 16
<html>
<head>
<title> continue instruqciis gamoyeneba </title>
</head>
<body>
<?php
$counter = -4;
for ( ; $counter <= 10; $counter++ )
{
if ( $counter == 0 )
continue;
$temp = 4000/$counter;
print “Tu 4000-s gavyofT $counter-ze, miviRebT ”. $temp.”-s”.“<br>”;
}
?>
</body>
</html>
18
erTmaneTSi Cadgmuli ciklebi
ciklis sxeuli SeiZleba sxva ciklsac Seicavdes. aseTi
konstruqciis gamoyeneba gansakuTrebiT xelsayrelia cxrilebTan
muSaobisas.
moviyvanoT Cadgmuli ciklebis magaliTi, romelTac brouzeris
ekranze gamohyavs gamravlebis tabula:

listingi 17
<html>
<head>
<title> erTmaneTSi Cadgmuli for-ciklebi </title>
</head>
<body>
<?php
print “<table border=1\n>”;
for ( $y=1; $y<=12; $y++ )
{
print “<tr>\n”;
for ( $x=1; $x<=12; $x++ )
{
print “\t<td>”;
print ($x*$y);
print “</td>\n”;
}
print “</tr>\n”;
}
print “</table>”;
?>
</body>
</html>

SevniSnoT, rom print instruqciebiT zemoT moyvanil programaSi


xdeba HTML-enis konstruqciebis (mocemul SemTxvevaSi cxrilis)
ageba da Semdgom ekranze misi asaxva.
gare cikli uzrunvelyofs $y cvladis mier (1 – 12) diapazonSi
mniSvnelobebis miRebas. TiToeul iteraciaSi ekranze aisaxeba <TR>
tegi (cxrilis striqoni) da gaiSveba Sida cikli, romelic $x
cvlads aseve Tanmimdevrulad aniWebs mniSvnelobebs 1-dan 12-is
CaTvliT, gamohyavs <TD> tegi (cxrilis ujredi) da masSi aTavsebs
$x-is namravls $y-ze. programis muSaobis saboloo Sedegad miiReba
gamravlebis cxrili.
19

funqciebi
nebismieri, met-naklebad “seriozuli” programa, rogorc wesi,
funqciebs iyenebs.
funqcia SeiZleba avtomats SevadaroT. Mfunqcias miewodeba
garkveuli “nedleuli” - monacemebi, romlebsac igi gadaamuSavebs
da iZleva Sedegs, romelic SeiZleba ekranze gamovitanoT CvenTvis
dabrunebuli, mis mier gamoTvlili mniSvnelobis saxiT. xSirad
funqcia orive aRniSnul qmedebas TviTonve axorcielebs.
gansakuTrebiT sasurvelia funqciebis gamoyeneba mravaljer
ganmeorebadi moqmedebebis Sesasruleblad.
funqcia ganimarteba, rogorc instruqciebis bloki, romelic
sruldeba programis mier misi gamoZaxebis SemTxvevaSi.
funqciebi ori saxisaa: sistemaSi (PHP-Si) CaSenebuli da
momxmareblis mier Seqmnili.
umeteswilad funqcias miewodeba argumenti (argumentebi). isini
unda ganlagdnen mrgval frCxilebSi (rogorc ukve viciT,
gamonakliss warmoadgens print funqcia). im iSviaT SemTxvevebSic ki,
roca romelime funqcia argumentebs ar saWiroebs, frCxilebis
dasma mainc aucilebelia. siaSi argumentebi erTmaneTisagan mZimiT
gamoiyofa.
funqcia gvibrunebs mniSvnelobas. MmagaliTad, abs() funqcia
ukan abrunebs misTvis gadacemuli ricxvis absolutur
(arauaryofiT) mniSvnelobas. sainteresoa, rom mniSvnelobas
abrunebs print() funqciac – es gaxlavT true an false, raTa programam
Seityos, warmatebiT Sesrulda Tu ara es operacia.

listingi 18
<html>
<head>
<title> abs() CaSenebuli funqciis gamoZaxeba </title>
</head>
<body>
<?php
$num = -321;
$newnum = abs($num);
print $newnum; // ekranze gamodis “321”
?>
</body>
</html>

funqcias vqmniT function sakvanZo sityvis meSveobiT, misi


saxelisa da argumentebis siis gansazRvriT da figurul
frCxilebSi funqciis sxeulis aRweriT:
20
listingi 19
<html>
<head>
<title> funqciis Seqmna </title>
</head>
<body>
<?php
function bighello()
{
print “<h1>HELLO</h1>”; }
bighello();
?>
</body>
</html>
am konkretul SemTxvevaSi funqcias argumentebi ar gadaecema,
magram misi saxelis Semdeg mrgvali frCxilebis Cveneba mainc
aucilebelia.
axla ki vaCvenoT argumentis Semcveli funqciis magaliTi:

listingi 20
<html>
<head>
<title> argumentiani funqciis Seqmna </title>
</head>
<body>
<?php
function printBR($txt);
{
print (“$txt<br>\n”);
}
print (“es aris striqoni”);
print (“es momdevno striqonia”);
print (“ai, kidev erTi striqoni”);
?>
</body>
</html>
Semdegi nabiji iqneba iseTi funqciis Seqmna, romelic
mniSvnelobas gvibrunebs:

listingi 21
<html>
<head>
<title> mniSvnelobis dambrunebeli funqciis Seqmna </title>
</head>
<body>
21
<?php
function addNums($firstnum, $secondnum);
{
$result = $firstnum + $secondnum;
return $result;
}
print addNums(3,5); // daibeWdeba 8
?>
</body>
</html>
return operacias SeuZlia Sedegi sxvadasxva saxiT daabrunos:
return 4;
return ($a / $b);
return (function($ argument));
dasaSvebia funqciis dinamikurad gamoZaxebac. magaliTad,
momxmarebels SeuZlia kompiuterTan dialogSi gadawyvitos,
romeli funqcia unda Sesruldes; mis mier miwodebuli
informaciis safuZvelze moxdeba gamosaZaxebeli funqciis saxelis
formireba.

listingi 22L
<html>
<head>
<title> funqciis dinamikurad gamoZaxeba </title>
</head>
<body>
<?php
function sayHello( );
/* aq programis gasamartiveblad vgulisxmobT, rom ”sayHello”
striqoni programas garedan miewodeba momxmareblis mier
dialogis reJimSi */
{
print (“Hello<br>”);
}
$function_holder = (“sayHello”);
$function_holder = ( );
?>
</body>
</html>

cvladebis xilvadobis ubnebi


funqciaSi Seqmnili cvladi mxolod mis arealSi moqmedebs –
igi lokaluri moqmedebisaa. funqciis SigniT avtomaturad ver
mivmarTavT mis gareT Seqmnil cvladsac. AaseTi midgomebi Tavidan
gvacilebs sxvadasxva adamianebis mier erTi didi programis
Seqmnisas mosalodnel gaugebrobebs. Mmagram Zalian xSirad saWiro
22
xdeba iseTi cvladebis Seqmnac, romelTac programis nebismieri
ubnidan SeiZleba mivmarToT. anu dgeba sakiTxi e.w. globaluri
moqmedebis cvladebis Seqmnisa. am mizniT, PHP4 iyenebs global
instruqcias:

listingi 23
<html>
<head>
<title> globaluri cvladebis gamoyeneba </title>
</head>
<body>
<?php
$life = 42;
function meaningOfLife( );
{
global $life;
print “The meaning of life is $life<br>”;
}
meaningOfLife( );
?>
</body>
</html>
sakiTxis aseTi gadawyvetis Sedegad ekranze aisaxeba
Setyobineba:
The meaning of Life is 42

gamoZaxebebs Soris funqciis mdgomareobis damaxsovreba


davuSvaT, gvesaWiroeba iseTi funqciis Seqmna, romelsac
exsomeba, Tu ramdenjer moxda misi gamoZaxeba, amasTan, gvsurs
gamoZaxebaTa ricxvi ekranzec avsaxoT. swored, aq SeiZleba
warmatebiT gamoviyenoT global instruqcia:

listingi 24
<html>
<head>
<title>cvladis mniSvnelobis Senaxva gamoZaxebebs Soris</title>
</head>
<body>
<?php
$num_of_calls = 0;
function andAnotherThing($txt );
{
global $num_of_calls;
$num_of_calls++;
print (“<h1> $num_of_calls. $txt</h1>”;
}
23
andAnotherThing( “ Widgets” );
andAnotherThing( “ Doodads” );
?>
</body>
</html>
vxedavT, rom aqac wina magaliTis msgavs xerxs viyenebT.
funqciis gareT Seqmnil cvlads rom mivwvdeT, funqciaSi igive
cvladi global instruqciis meSveobiT unda gamovacxadoT.
magram aseT midgomas erTi mniSvnelovani nakli axasiaTebs.
funqcia, romelic global instruqcias iyenebs, ar SeiZleba
damoukidebel blokad CaiTvalos, radganac mas muSaoba SeuZlia
mxolod mocemuli programis farglebSi. sakiTxi SeiZleba
sxvagvaradac gadawydes, mxolod am SemTxvevaSi unda visargebloT
static instruqciiT.
Static-iT Seqmnili cvladi lokalurobis Tvisebas inarCunebs,
oRond am cvladebisTvis damaxasiaTebeli Rirseba is gaxlavT, rom
funqciisadmi yoveli mimarTvisas maT axsovT wina gamoZaxebisas
miRebuli mniSvneloba:

listingi 25
<html>
<head>
<title> STATIC instruqcia </title>
</head>
<body>
<?php
function andAnotherThing ($txt)
{
static $num_of_calls = 0;
$num_of_calls++;
print "<h1>$num_of_calls. $txt</h1>";
}
andAnotherThing ("Widgets");
andAnotherThing ("Doodads");
?>
</body>
</html>
aseTi funqcia ki ukve damoukidebeli saxis gaxlavT. Aaxla
saWiro aRaraa globaluri cvladebis Seqmnaze zrunva, Sedegi ki
iseTive iqneba, rogorsac wina programa uzrunvelyofda.
24
kvlav argumentebis Sesaxeb; maTi mniSvnelobis
gansazRvra dumilis principiT
funqciis gamoZaxebisas ama Tu im argumentis mniSvneloba
SesaZlebelia dumilis wesiTac ganisazRvros:

listingi 26
<html>
<head>
<title>funqcia arasavaldebulo argumentiT</title>
</head>
<body>
<?php
function fontWrap($txt, $size=3)
{
print "<font size=\"$size\"face=\"Helvetica, Arial, Sans-Serif\">$txt</font>";
}
fontWrap("A heading<br>",5);
fontWrap("some body text<br>");
fontWrap("some more body text <br>");
fontWrap("yet more body text <br>");
?>
</body>
</html>

funqciis im gamoZaxebebSi, romlebSic miTiTebuli ar aris


meore argumenti, Sriftis zomad airCeva funqciis gansazRvrisas
naCvenebi 3-is toli mniSvneloba. PHP4-Si aseTi midgomas erTi
uxerxuli mxarec axlavs Tan. Tuki funqciis gamoZaxebisas
romelime argumenti gamovtoveT, misgan marjvniv myofi sxva
argumentebisTvisac dumiliT gaTvaliswinebuli mniSvnelobebiT
unda davkmayofildeT.

dayrdnobis gziT argumentis gadacema


roca funqcias argumentad romelime cvlads gadavcemT, cxadia,
funqciis Sesruleba araviTar zegavlenas ar axdens adgilze
cvladis mniSvnelobaze. magram SesaZlebelia funqcias cvladi
gadavceT dayrdnobis xerxis gamoyenebiTac. AaseT SemTxvevaSi
funqciaSi argumentze ganxorcielebuli cvlilebebi adgilze
arsebul cvladebzec aisaxeba. ukve viciT, rom cvladis (am
SemTxvevaSi argumentis) cvladze dasayrdnobad viyenebT &
simbolos. amasTan, aRniSnuli xerxis gamoyeneba SesaZlebelia
rogorc funqciis gamoZaxebisas, aseve misi gansazRvris drosac.
qvemoT naCvenebia es orive varianti:
25
listingi 27
<html>
<head>
<title>funqciis gamoZaxebisas argumentis gadacema dayrdnobis
meSveobiT</title>
</head>
<body>
<?php
function addFive($num)
{
$num += 5;
}
$orignum = 10;
addFive(&$orignum);
print $orignum;
?>
</body>
</html>

listingi 28
<html>
<head>
<title>dayrdnobis meSveobiT argumentis gadacema</title>
</head>
<body>
<?php
function addFive($num)
{
$num += 5;
}
$orignum = 10;
addFive($orignum);
print $orignum;
?>
</body>
</html>

SevniSnoT, rom ufro “programagenurad” gamoiyureba meore


midgoma.

masivebi
masivi erTi saxelis mqone mniSvnelobaTa krebulia,
romelTagan TiToeuls SeiZleba mivmarToT indeqsis an teqsturi
striqonis meSveobiT.
davuSvaT, programaSi saWiroa, inaxebodes raime 100
mniSvneloba. SeiZleboda TiToeuli maTganisTvis gangvesazRvra
26
cvladi (Sesabamisi mniSvnelobiT), magram gacilebiT aadvilebs am
mniSvnelobebTan muSaobas maTi masivis saxiT gaformeba (magaliTad,
sortirebisas, ciklebis gamoyenebisas da a.S.). masividan elementis
amorCeva umeteswilad nomris CvenebiT xdeba (sxva SemTxvevebi
qvemoT iqneba ganxiluli). PHP-Si masivis pirveli elementis
nomeria ara “1”, aramed “0”. (gavixsenoT, rom JavaScript-Sic es
gadawyvetileba iyo miRebuli).
masivis elementebisTvis mniSvnelobebis miniWeba amave dros maT
gamocxadebasac niSnavs. MmniSvnelobebis miniWeba ori xerxiT
xorcieldeba:
I - $users = array (“gia”, “mzia”, “lia”, “gigi”);
II - $users[] “gia”;
$users[] “mzia”;
$users[] “lia”;
$users[] “gigi”;
cxadia, meore SemTxvevaSi SesaZlebeli iyo indeqsebis Cvenebac,
magram masivis Seqmnisas es arafers ar gvaZlevs, garda saqmis
garTulebisa.
masivs misi Seqmnis Semdegac SegviZlia davumatoT elementebi
(indeqsis CvenebiT an mis gareSec):
$users = array (“gia”, “mzia”, “lia”, “gigi”);
$users[] = “joni”;
$users[] = “jimi”;

asocirebuli masivebi
asocirebuli masivi iseTi masivi gaxlavT, romlis elementis
arCeva saxelis meSveobiT SegviZlia. daprogramebis sxva enebSi
amgvar masivebs struqturebs uwodeben.
PHP-Si, sxva mxriv, Cveulebriv da asocirebul masivebs Soris
araviTari gansxvaveba ar arsebobs. Tumca, cxadia, asocirebuli
masivebis aRniSnuli Tavisebureba maTi damuSavebisas gansxvavebul
midgomebs moiTxovs.
asocirebuli masivebis Seqmnac zemoT moyvanili orive xerxiT
aris SesaZlebeli:
$character = array (saxeli => “gia”,
gvari => “lomiZe”,
asaki => 22,
“damatebiti informacia” => “niangebze monadire”);
mivaqcioT yuradReba – Tu masivis elementis saxelSi
Sualedebi gvxvdeba, saWiroa es saxeli brWyalebSi movaTavsoT.
27
axla ukve SegviZlia mivmarToT masivis nebismier elements,
magaliTad, amgvarad:
print $character [ asaki];
vaCvenoT meore gzac:
$character [saxeli] = ”gia”,
$character [gvari] = ”lomiZe”,
$character [asaki] = 22,
$character [“damatebiti informacia”] = ”niangebze monadire”

mravalganzomilebiani masivebi
masivis elementis rolSi SeiZleba gamovides rogorc raime
tipis mniSvneloba, aseve obieqti da masivic ki.
mravalganzomilebiani masivi iseTi masivi gaxlavT, romlis
TiToeuli elementi masivs warmoadgens. Mmisi meSveobiT advilad
SeiZleba SevqmnaT monacemTa sakmaod rTuli struqturebi,
magaliTad, asocirebuli masivebis masivi:

listingi 29
<html>
<head>
<title>mravalganzomilebiani masivis Seqmna</title>
</head>
<body>
<?php
$characters = array (
array ( name=>"bob",
occupation=>"superhero",
age=>30,
speciality=>"x-ray vision" ),
array ( name=>" sally",
occupation=>"superhero",
age=>24,
speciality=>"superhuman strength"),
array ( name=>"Mary",
occupation=>"arch villain",
age=>63,
speciality=>"nanotechnology" )
);
print $characters[0] [occupation];
// prints "superhero"
?>
</body>
</html>
28
zogjer CvenTvis ucnobia masivSi elementebis ricxvi. AaseT
SemTxvevaSi SegviZlia visargebloT count() funqciiT, romelic
masivis elementebis ricxvs gvibrunebs. MmagaliTad, roca gvsurs
amovbeWdoT ucnobi sigrZis masivis bolo elementi, ase SeiZleba
vimoqmedoT:
$users = array (“gia”, “lia”, “mzia”, “gigi”);
print $users[count($users) - 1];
gadmocemuli zomis 1-iT Semcireba gamowveulia im mizeziT, rom
masivis elementebis danomrva 0-dan iwyeba!
masivis elementebis ekranze gamoyvana SeiZleba ganvaxorcieloT
ciklebis meSveobiT. Yyvelaze martivi xerxia specifikuri foreach
ciklis gamoyeneba, romlisTvisac saWiro ar gaxlavT miTiTeba, Tu
ramdeni elementia masivSi. samagierod, aucilebelia im cvladis
Cveneba, romelSic droebiT Seinaxeba masivis TiToeuli elementi.
moviyvanoT am ciklis gamoyenebis magaliTi:
$users = array (“gia”, “lia”, “mzia”, “gigi”);
foreach ($users as $val)
{
print “$val<br>” ;
}
asocirebuli masivis ciklSi CaTvaliereba ki ramdenadme
gansxvavebul midgomas moiTxovs:
foreach ($array as $saxeli => $mniSvneloba)
aq saWiro xdeba masivis elementebis saxelisa da
mniSvnelobebis droebiT Semnaxveli cvladebis gansazRvra.
moviyvanoT magaliTi:

listingi 30
<html>
<head>
<title>asocirebuli masivis CaTvaliereba</title>
</head>
<body>
<?php
$character = array (
name=>"bob",
occupation=>"superhero",
age=>30,
"special power"=>"x-ray vision"
);
foreach ( $character as $key=>$val )
{
print "$key = $val<br>";
}
29
?>
</body>
</html>
programis Sesrulebis Sedegad ekranze gamodis Semdegi
informacia:
name = bob
occupation = superhero
age = 30
special power = x-ray vision

mravalganzomilebiani masivis gamoyvana


zemoT ganxiluli meTodebis gamoyenebiT axla Cven ukve
SegviZlia ekranze avsaxoT mravalganzomilebiani masivebic:

listingi 31
<html>
<head>
<title>cikliT CaTvaliereba</title>
</head>
<body>
<?php
$characters = array (
array ( name=>"bob",
occupation=>"superhero",
age=>30,
speciality=>"x-ray vision" ),
array ( name=>" sally",
occupation=>"superhero",
age=>24,
speciality=>"superhuman strength"),
array ( name=>"Mary",
occupation=>"arch villain",
age=>63,
speciality=>"nanotechnology" )
);
foreach ( $characters as $val )
{
foreach ( $val as $key=>$final_val )
{
print "$key: $final_val<br>";
}
print "<br>";
}
?>
</body>
</html>
30
ekranze gamova asocirebuli masivebis masivi:

name: bob
occupation: superhero
age: 30
speciality: x-ray vision

name: sally
occupation: superhero
age: 24
speciality: superhuman strength

name: Mary
occupation: arch villain
age: 63
speciality: nanotechnology
am programaSi gare cikli CaaTvalierebs nomrebis mixedviT
mowesrigebul $users masivs da mis TiToeul elements moaTavsebs
$val cvladSi. TiToeuli am elementTagani TviTon warmoadgens
masivs, magram radganac es masivi asocirebuli gaxlavT, Sida
cikliT misi CaTvalierebisas gamoiyeneba zemoT ganxiluli Semdegi
sintaqsi - TiToeuli qveelementis saxeli da mniSvneloba
ganTavsdeba $key da $final_val cvladebSi.

masivebis gaerTianeba
masivebis gaerTianebas emsaxureba array_merge CaSenebuli
funqcia:
$first = array (“a”, “b”, “c”);
$second = array (“1”, “2”, “3”);
$third = array_merge ($first, $second);
foreach ($third as $val)
{
print “$val<BR>”;
}

masivisTvis elementebis damateba


masivs elementebi array_push() funqciis meSveobiTac SegviZlia
davumatoT (cxadia, es sxva operaciaa, vidre masivebis gaerTianeba,
rac Tavdapirveli masivebis ucvlelad datovebas gulisxmobs).
magaliTad:
$first=array (“a”, “b”, “c”);
$total=array_push($first, 1,2,3);
sainteresoa, rom am dros xdeba ara marto $first masivisTvis
sami axali elementis damateba, aramed $total cvladisaTvis
31
modificirebuli masivis sigrZis sididis miniWebac. AamovbeWdoT es
mniSvneloba da ekranze avsaxoT masivis elementebi:
print “\$first masivSi sul aris $total elementi <p>”);
foreach ($first as $val)
{
print “$val<BR>”;
}
zeda magaliTSi gamoyenebulia maskirebis xerxi _ “ \ ” simbolos
dasma “$” simbolos win interpretators acnobebs, rom daxrili
xazis momdevno sityva $first man unda aRiqvas ara rogorc cvladi,
aramed rogorc “$first” striqoni.

masivis pirveli elementis amogdeba


aRniSnul mizans emsaxureba array_shift() funqcia.
qvemoT programaSi ciklis meSveobiT xdeba masivisTvis pirveli
elementis mocilebebi manam, sanam masivi ar dacarieldeba, rasac
amowmebs count() funqcia. amogdebuli elementis mniSvneloba eniWeba
$val cvlads. ciklis TiToeul bijze xdeba am cvladisa da masivSi
darCenili elementebis ricxvis amobeWdva.
<?php
$an_array = array ( “a”, “b”, “c” );
While ( count ($an_array))
{
$val = array_shift ($an_array);
print “$val<BR>”;
print “ \ $an_array masivSi aris ”. count ($an_array) . “elementi<BR>”
}
?>

masividan qvemasivis miReba


es procesi xorcieldeba array_slice() funqciis meSveobiT.
sawyisi masivi, cxadia, ar icvleba:
$first = array ( “a”, “b”, “c”, “d”, “e”, “f” );
$second = array_slice ( $first, 2, 3 );
foreach ( $first as $val ) {
print “$val<BR>”;
}
32
masivebis sortireba
martivi masivebis sortirebas axdens sort() funqcia,
asocirebulebisas - asort(). moviyvanoT asort() funqciis gamoyenebis
magaliTi:
$an_array = array ( “x”, “a”, “f”, “c” );
sort ( $an_array );
foreach ($an_array as $val )
{
print “$val<BR>”;
}
SesaZlebelia asocirebuli masivis sortireba velebis
saxelebis mixedviTac, raSic gvexmareba ksort() funqcia:
$first = array ( “x”=>5, “a”=>5, “f”=>5);
ksort ($first);
foreach $first as $key=>$val )
{
print “$key = $val<BR>”;
}

obieqtebi
daprogramebaSi Semdgomi, mniSvnelovani nabiji iqna win
gadadgmuli, roca SemoRebul iqna obieqtis cneba.
obieqti aris raime arsis Tvisebebis da am Tvisebebis damamuSa-
vebuli funqciebis - meTodebis krebuli.
daprogramebis procesi mravali niuansis gaTvaliswinebas
moiTxovs. obieqtebi Tavis Tavze iReben “Sav” samuSaos da Tan
momxmarebels awvdian maTTan interfeisis damyarebis moxerxebul
saSualebebs.
TviT obieqtebi warmogvidgeba, rogorc garkveuli klasis
konkretuli egzemplarebi. amrigad, klasi gamodis specialuri
Sablonis rolSi, romlis mixedviTac SesaZlebelia Seiqmnes
obieqti.
maSasadame, obieqtebis Seqmnamde unda gamovacxadoT klasi -
ganvsazRvroT is Tvisebebi da meTodebi, romlebic daaxasiaTeben
aRniSnul klass da, Sesabamisad, mis bazaze Seqmnil klasis
egzemplarebs – obieqtebs.
obieqtebisTvis ki xdeba Tvisebebis konkretuli mniSvnelo-
bebis gansazRvra.
SevniSnoT, rom TvisebebisTvis mniSvnelobebis miniWeba,
dumilis principiT, klasis gamocxadebis drosac SeiZleba. amasTan,
33
dasaSvebia, rom obieqtisTvis yoveli Tvisebis mniSvnelobis
gansazRvra arc moxdes.
gamovacxadoT raime martivi klasi da mis safuZvelze ganv-
sazRvroT klasis egzemplarebi - obieqtebi:

class first_class
{
var $name=”harry”;
}
$obj1=new first_class();
$obj2=new first_class();
// pirveli obieqtis name Tvisebas mivaniWoT mniSvneloba.
// yuradReba mivaqcioT Tvisebis gansazRvris wess!
$obj1 -> name=”bob”;
// ekranze avsaxoT obieqtebis Tvisebis mniSvnelobebi.
Print “$obj1 -> name<br>”; // gamodis axali mniSvneloba ”bob”
Print “$obj2 -> name<br>”; // gamodis mniSvneloba ”harry” (dumiliT)

aRvniSnoT, rom obieqtidan Tvisebaze gasvla xdeba _>


operatoris meSveobiT.
axla ki ganvsazRvroT klasi, romelic meTods Seicavs:

listingi 32
<html>
<head>
<title>meTodis Semcveli klasi</title>
</head>
<body>
<?php
class first_class
{
var $name;
function sayHello()
{
print "hello";
}
}
$obj1 = new first_class();
$obj1->sayHello();
// "hello"
?>
</body>
</html>
34
meTodi, vxedavT, Cveulebrivi funqciaa, oRond is klasis
SigniT ganisazRvreba. obieqtisTvis misi gamoZaxeba xdeba _>
operatoris meSveobiT.
xazgasasmelia is garemoeba, rom meTods SeuZlia mimarTos
klasSi arsebul yvela Sida cvlads (Tvisebas). am SesaZleblobas
uzrunvelyofs cvladisadmi mimarTvisas misi saxelis win $this
maCveneblis dasma:

listingi 33
<html>
<head>
<title>Tvisebisadmi mimarTva meTodidan</title>
</head>
<body>
<?php
class first_class
{
var $name="harry";
function sayHello()
{
print "hello! my name is $this->name<BR>";
}
}
$obj1 = new first_class();
$obj1->sayHello();
// print "hello my name is harry"
?>
</body>
</html>

radgan meTodi funqcia gaxlavT, mas gamoZaxebisas, cxadia,


argumentic (argumentebi) SeiZleba gadavceT. magram aq xazi unda
gavusvaT erT mniSvnelovan garemoebas - Tu klasSi gamocxadebuli
meTodisa da TviT klasis saxelebi erTmaneTs emTxveva, maSin
obieqtis Seqmnisas avtomaturad xdeba aseTi meTodis gamoZaxebac.
am dros Sesabamisi argumentebis gadacemiT Cven SegviZlia
ganvsazRvroT obieqtis TvisebaTa mniSvnelobebi.
aseT specialur funqciebs konstruqtorebi ewodeba.
moviyvanoT konstruqtoris meSveobiT obieqtis TvisebisaTvis
dumiliT gansazRvruli mniSvnelobis Secvlis magaliTi:

listingi 34
<html>
<head>
<title>konstruqtoris Semcveli klasi</title>
</head>
<body>
35
<?php
class first_class
{
var $name;
function first_class( $n="anon" )
{
$this->name = $n;
}
function sayHello()
{
print "hello my name is $this->name<BR>";
}
}
$obj1 = new first_class("bob");
$obj2 = new first_class( "harry");
$obj2 = new first_class();

$obj1->sayHello();
// print "hello my name is bob"
$obj2->sayHello();
// print "hello my name is harry"
?>
</body>
</html>

$obj1 obieqtis Seqmnisas avtomaturad gamoiZaxeba first_class


saxelis mqone meTodi, mas argumentad gadaecema “bob” striqoni, $n
cvlads dumiliT gaTvaliswinebuli “anon” mniSvneloba Seecvleba
“bob”-iT. Aam funqciis SigniT arsebuli instruqciiT xdeba klasSi
gamocxadebuli $this _> name cvladisadmi mimarTva da misTvis $n1
mniSvnelobis (anu “bob”-is) miniWeba.
amave programaSi iqmneba meore da mesame obieqtebic. mesame
obieqtisaTvis, radganac misi Seqmnisas argumentis Cveneba ar xdeba,
mas mieniWeba saxelisTvis dumiliT gaTvaliswinebuli mniSvneloba
“anon”.
dabolos, klasSi TiToeuli obieqtisTvis xdeba sxva meTodis
gamoZaxeba, romlis daniSnuleba aris obieqtis saxelis Semcveli
striqonis formireba da misi ekranze gamotana.

magaliTi
mwyobrSi moviyvanoT obieqtebis Sesaxeb Cveni codna da SevqmnaT
klasi, romelic ekranze gamogviyvans dasaTaurebul svetebian
cxrils. am klasSi gaviTvaliswinoT meTodi, romelic Seqmnil
obieqts daumatebs masivis saxiT mocemul striqons.
dasasrul, kidev erTi meTodiT avsaxoT cxrili ekranze.
36
klasis Tvisebebi
programis agebas viwyebT Sesabamisi klasis formirebiT:
class Table
{
var $table_array = array(); // cxrilSi monacemebis masivi
var $headers = array(); // svetebis saxelTa masivi
var $cols; // svetebis raodenobis amsaxveli cvladi
}
konstruqtori
$headers masivis mniSvnelobebi unda ganisazRvros obieqtis
(cxrilis) Seqmnisas. am mizans emsaxureba konstruqtori-funqcia.
cxadia, misi saxeli unda emTxveodes klasis saxels, anu unda iyos
Table. Sinaarsobrivi mxaridan gamomdinare, argumentis (romelic
masivs warmoadgens) saxelad avirCioT $headers. Semdeg, CvenTvis
ukve nacnobi xerxiT unda ganisazRvros klasSi gamocxadebuli
svetebis saTaurTa masivis Semcveloba da count() funqciis meSveobiT
daTvlil iqnes masSi elementebis raodenoba.
konstruqtors eqneba aseTi saxe:
function Table ($headers)
{
$this -> headers=$headers;
$this -> cols=count ($headers);
}
aRvniSnoT, rom obieqtis TvisebebSi ganTavsebuli informacia
misawvdomia obieqtis nebismieri meTodisTvis.
addRow() meTodi
am meTodis yoveli gamoZaxebisas obieqts emateba monacemTa
striqoni. es striqoni masivis saxiT warmogvidgeba, romlis sigrZe
unda emTxveodes saTaurebis striqonis sigrZes (programa amowmebs
am pirobis Sesrulebas):
function addRow ( $row )
{
if ( count ( $row ) != $this->cols )
return false;
array_push ( $this->table_array, $row);
return true;
}

array_push funqcia cxrils, romelic aseve masivis saxiT


warmogvidgeba, axal striqons umatebs. dasamatebeli elementi
37
(striqoni) TviTon gaxlavT masivi, magram igi arsebul masivs
emateba, rogorc elementi.
amrigad, viRebT mravalganzomilebian masivs - masivTa masivs.

AddRowAssocArray() meTodi
wina meTodisagan gansxvavebiT, addRowAssocArray() meTods
argumentad gadaecema ara mowesrigebuli, aramed asocirebuli
masivi. amasTan, SesaZlebelia masivis sigrZe naklebic iyos cxrilis
siganeze, e.i. masSi yoveli svetis saxeli arc figurirebdes.
programa Semdegnairad igeba:
• funqcia zemoT aRniSnul asocirebul masivs $row-assoc
argumentis saxiT Rebulobs.
• yalibdeba $row carieli masivi, romelic TandaTanobiT ivseba
saWiro elementebiT asocirebuli masividan.
• $row masivisaTvis elementis dasamateblad foreach cikliT
CaTvalierdeba $this _ >headers saTaurTa masivi da misi
elementebis mniSvneloba Tanmimdevrulad mieniWeba h eader
cvlads.
• isset() CaSenebuli funqciiT ciklSi mowmdeba, arsebobs Tu ara
$row-assoc asocirebul masivSi $header saxelis mqone elementi.
Tu es ase ar gaxlavT, nakluli $row-assoc masivi ivseba am
saxelis mqone elementiT, romelsac carieli striqonis
mniSvneloba eniWeba. nebismier SemTxvevaSi amas mosdevs $row
masivis Sesabamisi elementis formirebac.
• ciklis damTavrebis Semdeg Cvens gankargulebaSia $row masivi,
romelSic figurirebs $row-assoc asocirebul masivSi
mocemuli elementebi. zogierTi adgili ki masSi, rogorc
vnaxeT, carieli mniSvnelobiTac SeiZleba ganisazRvros.
• programis boloSi array_push CaSenebuli meTodiT xdeba
$table_array cvladSi ganlagebuli masivisTvis $row masivis,
rogorc elementis, damateba.
• return true instruqciiT xdeba Setyobineba, rom funqciis
muSaoba normalurad damTavrda.

output() meTodi
am meTodiT programas ekranze gamohyavs table-array mraval-
ganzomilebiani masividan cxrilis svetebis saTaurebi da masivis
mniSvnelobebi.
Tavdapirvelad foreach ciklis meSveobiT CaTvalierdeba
svetebis saTaurebis masivi da yoveli maTgani ekranze aisaxeba.
gadavdivarT Semdeg striqonze. foreach cvladSi CaTvalierdeba
table-array masivis elementebic, magram radganac es masivi
mravalganzomilebiania (mocemul SemTxvevaSi - organzomilebiani),
saWiro xdeba misi TiToeuli elementis (masivis) Sida foreah ciklSi
38
CaTvalierebac da calkeuli elementebis ekranze gamoyvana. zeda
donis TiToeul elements (anu masivis) amobeWvdis Semdeg programas
kursori momdevno striqonze gadahyavs.
sabolood, mTliani programa da misi muSaobis Sedegi aseTi
saxiT warmogvidgeba:

listingi 35
<html>
<head>
<title> Table klasi</title>
</head>
<body>
<?php
class Table
{
var $table_array = array ();
var $headers = array ();
var $cols;

function Table ($headers )


{
$this->headers = $headers;
$this->cols = count ( $headers );
}

function addRow ($row )


{
if ( count ($row ) != $this->cols )
return false;
array_push($this->table_array, $row);
return true;
}

function addRowAssocArray ($row_assoc)


{
$row = array();
foreach ($this->headers as $header )
{
if ( ! isset( $row_assoc[$header] ))
$row_assoc[$header] = " " ;
$row[] = $row_assoc[$header];
}
array_push($this->table_array, $row);
return true;
}
39
function output ()
{
print "<pre>";
foreach ( $this->headers as $header )
print "<b>$header</B> ";
print "\n";
foreach ( $this->table_array as $y )
{
foreach ( $y as $xcell )
print "$xcell ";
print "\n";
}
print "</pre>";
}
}
$test = new table (array("a","b","c") );
$test->addRow( array(1,2,3) );
$test->addRow( array(4,5,6) );
$test->addRowAssocArray( array ( b=>0, a=>6, c=>3 ));
$test->output();
?>
</body>
</html>
programis muSaobis Sedegs eqneba saxe:
abc
123
456
603

kvlav klasebis Sesaxeb


memkvidreobiToba
klasebs momxmareblisTvis mravalferovani servisis gaweva
SeuZliaT. magaliTad, advilad gansaxorcielebelia arsebulis
bazaze modificirebuli klasebis Seqmnac. amasTan, axal klasebs
memkvidreobiT gadmoecemaT mSobeli klasis warmomadgenlebisaTvis
damaxasiaTebeli funqciuri rolebic.
moviyvanoT amgvari klasis Seqmnis magaliTi:

listingi 36
<html>
<head>
<title>arsebuli klasis bazaze axlis Seqmna</title>
</head>
<body>
40
<?php
class first_class
{
var $name = "harry";

function first_class( $n )
{
$this->name = $n;
}

function sayHello()
{
print "hello my name is $this->name<br>";
}
}

class second_class extends first_class


{

}
$test = new second_class("son of harry");
$test->sayHello();

// print "hello my name is harry"


?>
</body>
</html>
programaSi, first_class-is garda, mis bazaze SevqmeniT
second_class-ic. am mizniT, sakmarisi aRmoCnda misi gamocxadebisas
extends sakvanZo satyvis gamoyeneba.
vxedavT, rom first_class Seicavs konstruqtor-funqciasac.
(gavixsenoT, aseTi funqciis saxeli klasis saxels emTxveva).
sainteresoa, rom memkvidre klasis Seqmnisas avtomaturad xdeba
masSi ara marto meTodebis gadatana, aramed konstruqtorisaTvis
Sesabamisi saxelis gansazRvrac. swored, aseTi gadawyvetilebis
gamo gaxda SesaZlebeli, koreqtulad emuSava mocemul programaSi
test-sayHello() instruqcias.

mSobeli klasis meTodebis saxecvlileba


wina magaliTSi warmoebuli klasis obieqtebi zustad iseve
iqceodnen, rogorc mSobeli klasis obieqtebi. ra Tqma unda,
dasaSvebia, es yovelTvis ase arc iyos. magaliTad, SevqmnaT second-
class warmoebul klasisaTvis sakuTari meTodi sayHello():

listingi 37
<html>
<head>
41
<title>meTodis xelaxla gansazRvra</title>
</head>
<body>
<?php
class first_class
{
var $name = "harry";

function first_class( $n )
{
$this->name = $n;
}

function sayHello()
{
print "hello my name is $this->name<br>";
}
}

class second_class extends first_class


{
function sayHello()
{
print "I’m not going to tell you my name<br>";
}
}

$test = new second_class("son of harry");


$test->sayHello();

// beWdavs "I’m not going to tell you my name"


?>
</body>
</html>

mSobeli klasis meTodis gamoZaxeba


gamoricxuli ar gaxlavT, zogjer saWiro Seiqnes warmoebuli
klasis obieqtidan mSobeli klasis meTodis gamoZaxebac. obieqtze
orientirebuli daprogrameba uSvebs aseT SesaZleblobas; igi
xorcieldeba martivi instruqciis safuZvelze:

listingi 38
<html>
<head>
<title>mSobliuri klasis meTodis gamoZaxeba</title>
</head>
42
<body>
<?php
class first_class
{
var $name = "harry";

function first_class( $n )
{
$this->name = $n;
}

function sayHello()
{
print "hello my name is $this->name<br>";
}
}

class second_class extends first_class


{
function sayHello()
{
print "I'm not going to tell you my name --";
first_class::sayHello();
}
}

$test = new second_class("son of harry");


$test->sayHello();
/ / gamohyavs "I'm not going to tell you my name" --HEllo my name is
son of harry"
?>
</body>
</html>

memkvidreobis gadacemis magaliTi


adre ganxiluli Table klasis bazaze SevqmnaT axali,
HTMLTable-d wodebuli klasi, romelsac damatebiTs funqciebs
davakisrebT. kerZod, maTi meSveobiT moxdeba cxrilSi gamosayvani
monacemebis daformateba da maTi Cveulebrivi, html cxrilis saxiT
gamoyvana. am magaliTSi momxmarebels SeeZleba, airCios cellpadding
da bgcolor atributebis mniSvnelobebi.
wina programaSi unda SevitanoT Semdegi cvlilebebi:
vacxadebT table-s memkvidre HTMLTable klass -
43
class HTMLTable extends Table
{
var $bgcolor;
var $cellpadding = 2; // mniSvneloba ganisazRvreba dumiliT
}

konstruqtoris Seqmna
warmoebul klasSi SevqmnaT sakuTari konstruqtori
(winaaRmdeg SemTxvevaSi obieqtis Seqmnisas moxdeboda mSobeli
klasis konstruqtoris gamoZaxeba):
function HTMLTable ($headers, $bg=”#f f f f f f “)
{
Table: :Table ($headers);
$this -> bgcolor=$bg;
}
vxedavT, rom, mSobeli klasis konstruqtoris funqciebis
Sesrulebis garda, warmoebuli klasis konstruqtori Tavis Tavze
iRebs obieqtis (cxrilis) axali TvisebebisTvis argumentis miRebisa
da am TvisebebisaTvis mniSvnelobis miniWebis funqciebsac.
aRvniSnoT, rom Tu obieqtis Seqmnisas $bg argumentisaTvis
mniSvneloba ar ganisazRvreba, maSin mas mieniWeba dumiliT
gaTvaliswinebuli mniSvneloba (mocemul SemTxvevaSi #f f f f f f ).

SetCellPadding meTodis damateba


davamatoT klasSi misi sakuTari meTodebic. magaliTad,
SevqmnaT meTodi, romlis gamoZaxebis Sedegad Seicvleba dumiliT
gaTvaliswinebuli mniSvneloba cellpadding TvisebisaTvis:
function SetCellPadding ($padding)
{
$this -> cellpadding = $padding;
}

Output() meTodi
dabolos, mogviwevs mSobliur klasSi arsebuli am meTodis
mTlianad gardaqmna, raTa SevZloT monacemebis HTML-cxrilis
saxiT gamoyvana:
function Output()
{
print "<table cellpadding=\”$this->cellpadding\” border=1>";
foreach ( $this->headers as $header )
print "<td bgcolor=\”$this->bgcolor\”><b> $header </b></td>";
foreach ( $this->table_array as $row=>$cells )
{
44
print "<tr>”;
foreach ( $cells as $cell )
print "<td bgcolor=\”$this->bgcolor\”> $cell</td>";
print "/<tr>”;
}
print "</table>”;
}
damatebiT imisa, rac xdeboda mSobliur klasSi, aq
gaTvaliswinebulia cellpadding da bgcolor atributebis formirebac.
mTliani programa da misi muSaobis Sedegi aseTi saxiT
warmogvidgeba:

listingi 39
<html>
<head>
<title> Table da HTMLTable klasebi </title>
</head>
<body>
<?php
class Table
{
var $table_array = array();
var $headers =array ();
var $cols;
function Table ($headers )
{
$this->headers =$headers;
$this->cols =count ( $headers );
}
function addRow ( $row )
{
if ( count ($row) != $this->cols )
return false;
array_push($this->table_array, $row);
return true;
}
45
function addRowAssocarray( $row_assoc )
{
if ( count ($row_assoc) != $this->cols )
return false;
$row = array();
foreach ( $this->headers as $header )
{
if ( ! isset( $row_assoc[$header] ))
$row_assoc[$header] = " ";
$row[] = $row_assoc[$header];
}
array_push($this->table_array, $row);
}

function output ()
{
print "<pre>";
foreach ( $this->headers as $header )
print "<B>$header</B> ";
print "\n";
foreach ( $this->table_array as $y )
{
foreach ( $y as $xcell )
print "$xcell ";
print "\n";
}
print "</pre>";
}
}

class HTMLTable extends Table


{
var $bgcolor;
var $cellpadding = "2";
46
function HTMLTable ( $headers, $bg="#ffffff" )
{
Table::Table($headers);
$this->bgcolor=$bg;
}

function setCellpadding( $padding )


{
$this->cellpadding = $padding;
}

function output ()
{
print "<table cellpadding= \"$this->cellpadding\" border =1>";
foreach ( $this->headers as $header )
print "<td bgcolor=\”$this->bgcolor\”><b> $header </b></td>";
foreach ( $this->table_array as $row=>$cells )
{
print "<tr>";
foreach ( $cells as $cell )
print "<td bgcolor=\"$this-> bgcolor\"> $cell</td>";
print "</tr>";
}
print "</table>";
}
}
$test = new HTMLTable( array( "a", "b", "c"), "#00FF00");
$test->setCellpadding( 7 );
$test->addRow( array(1,2,3));
$test->addRow( array(4,5,6));
$test->addRowAssocArray ( array ( b=>0, a=>6, c=>3 ));
$test->output();
?>
</body>
</html>
47
programis muSaobis Sedegia Semdegi cxrili:

a b c

1 2 3

4 5 6

6 0 3

formebTan muSaoba
dasawyisSi gavixsenoT, rom programebSi globaluri cvladebis
gamocxadeba xdeba yvela funqciis gareT, ase vTqvaT, programis
pirvel doneze. TiToeuli globaluri cvladi inaxeba CaSenebul
asocirebul $GLOBALS masivSi.
vnaxoT, rogor xerxdeba am cvladebis CaTvaliereba:

listingi 40
<HTML>
<head>
<title> $GLOBALS masivis CaTvaliereba </title>
</head>
<body>
<?php
$user1 = "Bob";
$user2 = "Harry";
$user3 = "Mary";
foreach ( $GLOBALS as $key=>$value )
{
print "\$GLOBALS[\"$kay\"] == $value<br>";
}
?>
</body>
</html>

sistemurTan erTad ekranze aisaxeba Cven mier programaSi


gamocxadebuli 3 globaluri cvladic.
48
momxmareblis mier Sevsebuli formis
damuSavebis programa
jer SevqmnaT kodi Web-furclisaTvis, romelzec moxdeba
formis Sevseba:

listingi 42
<HTML>
<head>
<title> martivi html-forma </title>
</head>
<body> <form action="eg9.3.php" method="GET">
<input type="text" name="user">
<br>
<textarea name="address" rows="5" cols="40">
</textarea>
<br>
<input type="submit" value="hit it!">
</form>
</body>
</html>

am martiv formaSi ganlagebulia “user” saxelis mqone teqsturi


veli, “address” teqsturi ubani da monacemTa gadacemis Rilaki
“Submit”.
formis Sevsebis Semdeg Rilakze dawkapunebiT gamoiZaxeba
mimdinare saqaRaldeSi myofi eg9.3.php programa, romelic
daamuSavebs formis meSveobiT gadacemul monacemebs:

listingi 43
<HTML>
<head>
<title> wina listingidan formis monacemebis wakiTxva </title>
</head>
<body>
<?php
print "Welcome <b>$user</b><p>\n\n";
print "your address is:<p>\n\n<b>$address</b>";
?>
</body>
</html>

zemoT moyvanil programas principuli siaxle axasiaTebs - misi


gamoZaxeba xdeba ara uSualod, aramed HTML-formidan. programa
sruleba serverze. formiT gadacemul monacemebs programebi
globaluri cvladebis meSveobiT iReben.
49
mravali mniSvnelobis mqone elementebis damuSaveba
ganvixiloT iseTi SemTxveva, roca momxmarebeli programas
gadascems erT elementTan dakavSirebul ramdenime mniSvnelobas,
magaliTad, CamoSlad siaSi arCeul ramdenime produqtis
dasaxelebas, gvars da a.S.
es sakiTxi Semdegnairad wydeba – elementis saxelis Semdeg
unda dasmul iqnes carieli kvadratuli frCxilebi:

listingi 44
<HTML>
<head>
<title> HTML forma Select tegiT </title>
</head>
<body>
<form action="eg9.5.php" method="POST">
<input type="text" name ="user">
<br>
<textarea name="address" rows="5" cols="40">
</textarea>
<br>
<select name="produqts[]" multiple>
<option>Sonic Screwdriver
<option>Tricorder
<option>ORAC AI
<option>HAL 2000
</select>
</br>
<input type="submit" value="hit it!">
</form>
</body>
</html>
select tegSi gadasacemi elementisaTvis products saxeli rom
migveniWebina, maSin programa daamuSavebda siaSi SerCeul mxolod
erT mniSvnelobas, products[] saxeli ki uzrunvelyofs am
programisaTvis momxmareblis mier arCeuli mniSvnelobebis
gadacemas $products masivis saxiT.
programas SeiZleba aseTi saxe hqondes:

listingi 45
<HTML>
<head>
<title> wina listingidan formis monacemebis damuSaveba </title>
</head>
<body>
<?php
50
print "Welcome <b>$user</b><p>\n\n";
print "Your address is:<p>\n\n<b>$address</b><p>\n\n";
print "Your product choices are:<p>\n\n";
print "<ul>\n\n";
foreach ( $products as $value )
{
print "<li>$value<br>\n";
}
print "</ul>";
?>
</body>
</html>
SevniSnoT, rom, select-is garda, erTi saxis ramdenime
mniSvnelobis gadacema sxva elementebsac SeuZliaT (magaliTad,
erTi saxelis qveS gaerTianebul almebis mimdevrobas).

HTML-teqstisa da PHP-programebis
erT furcelze ganlageba
zogjer, Tu es SesaZlebelia, umjobesia HTML-teqsti da PHP-
programa erT Web-furcelze ganvalagoT. magaliTad, maSin, roca
formis Sevseba ramdenjerme xdeba. igulisxmeba SemTxveva, roca
programa teqstisgan gancalkevebulia da ara masSi mraval “naWrad”
gafantuli, rac mis wakiTxvas arTulebs.
SevqmnaT skolamdeli asakis bavSvebisaTvis programa,
romelic «Caifiqrebs» ricxvs da pasuxobs kiTxvaze, momxmareblis
mier misTvis gadacemuli ricxvi metia Tu naklebia am ricxvze.
daviwyoT HTML-formis Seqmna. igi Seicavs erTaderT teqstur
vels. aseT SemTxvevaSi formis gadasacemad Tanamedrove
brouzerebs aRar sWirdebaT Submit Rilakis arseboba – am mizniT
sakmarisia <Return> klaviSze xelis daWera.
Semdeg, action elementSi php programuli failis nacvlad
miTiTebulia $PHP_SELF cvladi. maSasadame, formis Sevsebis
Semdeg mimarTva xdeba sakuTari Tavisadmi:

listingi 46
<HTML>
<head
<title>Tavis Tavis gamomZaxebeli HTML forma </title>
</head>
<body>
<form action=<?php print $PHP_SELF?>" method="POST">
Type your guess here: <input type="text" name="guess">
</form>
</body>
</html>
51
cxadia, ekranze araferi aisaxeba, amitom Semdegi nabiji iqneba
imave Web-furcelze php-programis teqstis damateba:

listingi 47
<?php
$num_to_guess = 42;
$message = " ";
if ( ! isset( $guess ) )
{
$message = "Welcome to the guessing machine!";
}
elseif ( $guess > $num_to_guess )
{
$message = "$guess is too big! Try a smaller number";
}
elseif ( $guess < $num_to_guess )
{
$message = "$guess is too small! Try a larger number";
}
else
{
$message = "Well done!";
}
?>
<html>
<head>
<title> ricxvis gamomcnobi programa </title>
</head>
<body>
<form action=<?php print $PHP_SELF?>" method="POST">
Type your guess here: <input type="text" name="guess">
</form>
</body>
</html>

pirvel rigSi, unda ganisazRvros ricxvi, romelic


momxmarebelma unda gamoicnos. realur (arasacdel) programaSi,
cxadia, SemTxveviTi ricxvis generirebas movaxdendiT. amjerad, am
saqmes ufro martivad vwyvetT - cvlads pirdapir vaniWebT
mniSvnelobas.
amis Semdeg unda gairkves, momxmarebelma pirvelad mimarTa
Web-furcels, Tu forma ukve Seavso da igi ganmeorebiT gamoiZaxa.
am kiTxvaze pasuxi imis da mixedviT gaicema, gansazRvrulia Tu ara
$guess globaluri cvladi. gavixsenoT, am sqemaSi gvexmareba isset()
CaSenebuli funqcia.
Semdeg ki pirobebis Semmowmebel operatorebSi garkveva
sirTules aRar warmoadgens.
52
Tu momxmarebeli pirvelad moxvda Web-furcelze, maSin,
cxadia, $guess cvladi gansazRvruli ver iqneba da $message
striqonSi Segvaqvs misalmebis gamomxatveli striqoni. winaaRmdeg
SemTxvevaSi xdeba Semowmeba, aWarbebs Tu ara $guess-is mniSvneloba
winaswar aRebul ricxvs da amisda mixedviT, momxmarebels
miewodeba Sesabamisi Setyobineba $messaqe cvladis meSveobiT.

mdgomareobis dasafiqsireblad
damaluli velebis gamoyeneba
davuSvaT, gvsurs, davimaxsovroT, Tu ramdeni cda gamoiyena
momxmarebelma.Aam saqmeSi SegviZlia e.w. damaluli veli davixmaroT.
igi teqsturi velia, romelic ekranze uSualod ar aisaxeba.
vaCvenoT, Tu rogor SeiZleba Web-furcelze aseTi velis
damateba da PHP-programiT misi damuSaveba:

listingi 48
<?php
$num_to_guess = 42;
$message = " ";
$num_tires = ( isset( $num_tries ) ) ? ++$num_tries : 0;

if ( ! isset( $guess ) )
{
$message = "Welcome to the guessing machine!";
}
elseif ( $guess > $num_to_guess )
{
$message = "$guess is too big! Try a smaller number";
}
elseif ( $guess < $num_to_guess )
{
$message = "$guess is too small! Try a larger number";
}
else // must be equivalent
{
$message = "Well done!";
}
53
$guess = (int) $guess;
?>
<html>
<head>
<title> damaluli velis meSveobiT mdgomareobis damaxsovreba
</title>
</head>
<body>
<h1>
<?php print $message ?>
</h1>
Guess number: <?php print $num_tries?>
<form action=<?php print $PHP_SELF?>" method="POST">
Type your guess here:
<input type="text" name="guess" value="<?php print $guess?>">
<input type="hidden" name="num_tries" value="<?php print $num_tries?>">
</form>
</body>
</html>
vxedavT, rom formaSi davamateT num_tries saxelis mqone
damaluli veli (velis tipze migviTiTebs type = ”hidden” atributi).
mis value atributs vaniWebT mniSvnelobas:
value = ”<?php print $num_tries ?>”
aseve viqceviT guess velisTvisac. masac imave wesiT vaniWebT
mniSvnelobas:
value = ”<?php print $guess ?>”
magram ramdenadac es veli damaluli araa, ekranze aisaxeba
misi mniSvnelobac, anu is mniSvneloba, romelic bolo cdaze
Seitana momxmarebelma am velSi. SedegadD momxmarebels uadvildeba
axali arCevanis gakeTeba velSi arsebuli mniSvnelobis koreqti-
rebis meSveobiT.
programas dasawyisSi emateba Semowmebis operatori, romelic,
Tu $num_tries globaluri cvladi Seqmnilia, 1-iT zrdis mis
mniSvnelobas, winaaRmdeg SemTxvevaSi qmnis cvlads da aniWebs
nulovan mniSvnelobas.
54
klientis “gadarTva” sxva Web-furcelze
Cven mier zemoT ganxilul programas erTi arsebiTi nakli
gaaCnia - mas maSinac gamohyavs ekranze Sesavsebi forma, roca
klientma ricxvi ukve gamoicno.
Tu ki Cven gamoviyenebT e.w programis saTauris gadmomgzavn
header() funqcias, momxmareblis mier ricxvis gamocnobis
SemTxvevaSi SevZlebT mas ekranze gamovuyvanoT sxva - milocvis
Setyobineba, risTvisac sakmarisia wina programaSi else-bloki
Semdegnairad SevcvaloT:
else
{
header ( “Location: congrats.html” );
exit;
}
saerTod, serveridan klientis mier programis gamoZaxebis
SemTxvevaSi mas avtomaturad gadmoegzavneba gadmocemuli Web-
furclis Sesaxeb informaciis Semcveli saTauri. masSi arsebuli
informacia ekranze, Tu specialur zomebs ar mivmarTeT, ar
aisaxeba - am informaciis brouzeri iyenebs.
ai, tipuri saTauri, romelsac PHP-programa brouzers
ugzavnis:

listingi 49
HEAD /matt/php-book/forms/eg9.1.php HTTP/1.0
HTTP/1.1 200 OK
Date: Sun, 09 Feb 2005 17:23:45 QMT
Server: Apache/1.3.9 (UNIX) PHP 4.0
Connection: close
Content-Type: text/html

SesaZlebelia wina programa ise iqnes modificirebuli, rom


ricxvis gamocnobis SemTxvevaSi momxmarebeli davakavSiroT im
WEB-furcelTan, romelzec iqneba Sesabamisi Setyobineba milocvis
teqstiT. es xdeba header("Location: congrats.html"); operatoris
meSveobiT:

listingi 50
<?php
$num_to_guess = 42;
$message = " ";
$num_tries = ( isset( $num_tries ) ) ? ++$num_tries : 0;
if ( ! isset( $guess ) )
{
$message = "Welcome to the guessing machine!";
}
55
elseif ( $guess > $num_to_guess )
{
$message = "$guess is too big! Try a smaller number";
}
elseif ( $guess < $num_to_guess )
{
$message = "$guess is too small! Try a larger number";
}
else // must be equivalent
{
header( "Location: congrats.html" );
exit;
}
$guess = (int) $guess;
?>
<html>
<head>
<title> header() funqciis meSveobiT momxmareblisaTvis saitis
misamarTis gagzavna
</title>
</head>

<body>
<h1>
<?php print $message ?>
</h1>
Guess number: <?php print $num_tries?>

<form action=<?php print $PHP_SELF?>" method="POST">


Type your guess here:
<input type="text" name="guess" value="<?php print $guess?>">
<input type="hidden" name="num_tries" value="<?php print
$num_tries?>">
</form>

</body>
</html>

failebis serverze gadasacemad


saWiro formebi da programebi
Tu gvsurs HTML-formis daxmarebiT serverze raime faili
gadavagzavnoT (magaliTad, GIF formatSi naxati), formaSi Sesabamis
velebTan erTad unda gaviTvaliswinoT enctype argumenti:
enctype = “multipart/form-data”
56
formaSi aucilebelia gaviTvaliswinoT max_file_size saxelis
mqone damaluli velic, romelSic miTiTebuli iqneba gadasacemi
failis SesaZlo maqsimaluri sigrZe (igi ar unda aRematebodes
php.ini failSi naCveneb zomas, romelic, Cveulebriv, 2 megabaitis
tolia). mas mosdevs INPUT elementi TviT failis gadacemisaTvis.
fails SegviZlia davarqvaT nebismieri saxeli:

listingi 51
<HTML>
<HEAD>
<TITLE> failis gadasacemi martivi forma </title>
</head>
<BODY>
<FORM enctype=”multipart/form-data” action=”<? print $PHP_SELF ?>”
metod=”POST>”
<INPUT type=”hidden” name=”MAX_FILE_SIZE” value=”55555”>
<INPUT type=”file” name=”fupload” ><br>
<INPUT type=”submit” value=”upload!” >
</form>
</body>
</html>

vxedavT, rom forma imave furcels gamoiZaxebs, romelzec


TviTon aris Cawerili. es saWiroa im php-programisadmi mimarTvis
gansaxorcieleblad, romelic gadacemul fails daamuSavebs.

serverze gadacemis Semdeg fails mieniWeba unikaluri saxeli


da Seinaxeba droebiTi failebis katalogSi (UNIX-Si, Cveulebriv, es
katalogia /tmp). failisken mimavali mTeli gza Caiwereba im
globalur cvladSi, romlis saxeli emTxveva failis gadamcemi
velis saxels. maSasadame, mocemul SemTxvevaSi es iqneba $fupload.
serverze gadacemuli failis Sesaxeb PHP sxva informaciebsac
inaxavs garkveuli wesiT saxeldebul Semdeg globalur cvladebSi:

cvladis saxeli aRwera magaliTi


$fupload gza droebiT failamde /tmp/php5Pq3fu
$fupload_name gadacemuli failis saxeli test.gif
$fupload_size failis sigrZe baitebSi 5555
$fupload_type failis tipi MIME sistemaSi image/gif

formiT erTdroulad ramdenime failis gadacemac SeiZleba.


maTTvis servers damatebiTi cvladebi aqvs gaTvaliswinebuli (am
sakiTxs aq ar ganvixilavT).
57
axla ki davweroT programa, romelic ekranze gamoiyvans
gadacemuli failis Sesaxeb informacias. amasTan, Tu failis
gafarToeba iqneba GIF, maSin ekranze naxatic aisaxeba:

listingi 52
<HTML>
<head>
<title> gadacemuli failis damuSavebis programa </title>
</head>
<?php
$file_dir= "/home/matt.htdocs/uploads";
$file_url= "http://www.corrosive.co.uk/matt/uploads";
if (isset( $fupload ) )
{
print "path: $fupload<br>\n";
print "path: $fupload_name<br>\n";
print "path: $fupload_size bytes<br>\n";
print "path: $fupload_type<p>\n\n";
if ($fupload_type == "image/gif")
{
copy ($fupload, "$file_dir/$fupload_name") or die ("Couldn't copy");
print "<img src=\"$file_url/$fupload_name\"><p>\n\n";
}
}
?>
<body>
<form enctype="multipart/form-data" action="<?php print $PHP_SELF ?>"
method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="51200">
<input type="file" name="fulpload"><br>
<input type="submit" value="Send file!">
</form>
</body>
</html>

Tavdapirvelad programa amowmebs, gansazRvrulia Tu ara


$fupload cvladi. Tu es asea, faili gadacemuli aris serverisaTvis,
mis Sesaxeb informacia Cawerilia globalur cvladebSi da ekranze
gamodis 4 cvladis saxelebisa da mniSvnelobebis amsaxveli
informacia.
Semdeg mowmdeba, $fupload_type cvladSi mocemuli tipi Tu
emTxveva “image/gif”-s? dadebiTi pasuxis SemTxvevaSi xdeba droebiTi
katalogidan Cvens katalogSi failis kopireba. Copy() funqcia
moiTxovs or arguments: gzas sawyis failamde da mis axal
mdebareobas (failis saxelis CvenebasTan erTad). pirveli
gansazRvrulia $fupload cvladSi, xolo axali mdebareoba naCvenebia
programis dasawyisSive, $file_dir cvladSi.
58

failebTan muSaoba
failebis CarTva PHP-dokumentSi
include() direqtiviT SesaZlebelia nebismier php-dokumentSi (anu
failSi) sxva, aseve failis saxiT gaformebuli php-programis
CarTva.
CasarTavi faili SeiZleba mravalma sxva php-dokumentmac
gamoiZaxos, rac friad aadvilebs daprogramebis process. kerZod,
Tu saWiroa programul failSi informaciis koreqtireba, es
procesi ganxorcieldeba mxolod masSi, anu erTaderT adgilze.
include() funqcias sWirdeba mxolod failis saxeli (da misken
gzis) Cveneba. moviyvanoT am funqciis gamoyenebis magaliTi:
listingi 53
<HTML>
<head>
<title>include() funqciis gamoyeneba </title>
</head>
<body>
<?php
include("eg10.2.php");
?>
</body>
</html>

Tu gamoZaxebuli faili, vTqvaT, aseTi striqonia: I have been


included! - igi damuSavdeba, rogorc ubralo H HTML -teqsti da aseve
aisaxeba ekranze. magram Tuki gvsurs, rom gamoZaxebuli faili
aRqmuli iqnes, rogorc PHP-programa, maSin igi Sesabamis tegebSi
unda movaTavsoT:

listingi 54
<?php
print "I have been included!!<BR>";
print "but now I can add up... 4+4 = ".(4+4);
?>

CasarTav failebs PHP-Si, funqciebis darad, return operatoris


meSveobiT SeuZliaT programis Sesrulebis Sewyveta da mniSvnelo-
bebis dabruneba. moviyvanoT magaliTebi:

listingi 55
<HTML>
<head>
<title> include() funqcia abrunebs mniSvnelobas </title>
</head>
<body>
59
<?php
$addResult = include("eg10.6.php");
print "The include file returned $addResult";
?>
</body>
</html>
listingi 56
<?php
$retval = ( 4 + 4 );
return $retval;
?>
{ es teqsti ekranze, cxadia ver aisaxeba! }

aRvniSnoT, rom include() instruqciis gamoyeneba SesaZlebelia


pirobiT operatorebsa da ciklebSic. moviyvanoT magaliTebi:
$test = false;
if ($test)
{
include (“a_file.txt”);
}
listingi 57
<html>
<head>
<title> include() funqciis ciklSi gamoyeneba </title>
</head>
<body>
<?php
for ($x=1; $x<=3; $x++)
{
$incfile="incfile$x".".txt";
print "vrTavT $incfile fails <br>";
include ("$incfile");
print "<p>";
}
?>
</body>
</hmtl>

am programis Sesrulebisas Web-furclis teqstSi Caemateba 3


faili:
incfile1.txt, incfile2.txt da incfile3.txt
SevniSnoT, rom sanam failTan muSaobas daviwyebdeT,
SesaZlebelia rigi funqciebiT mis Sesaxeb informaciis miReba-
Semowmeba.
60
magaliTad, funqcia file_exists (“failis_sruli_misamarTi an
mxolod failis_dasaxeleba”) amowmebs failis arsebobas. am
kiTxvaze dadebiTi pasuxi niSnavs funqciis mier true-s dabrunebas,
winaaRmdeg SemTxvevaSi – false-s:
if (file_exists(“test.txt”))
print “test.txt faili moiZebna”;
arsebobs failis sxva Tvisebebis (statusis, sigrZis, Seqmnis
TariRis da a.S.) Sesaxeb informaciis momwodebeli funqciebic.
dabolos, SesaZlebelia PHP-programebis meSveobiT failebis
katalogebis Seqmna, koreqtireba da ganadgureba (amjerad am
sakiTxebs ar ganvixilavT).

DBM-funqciebTan muSaoba
PHP-programebs SeuZliaT imuSaon iseT mZlavr monacemTa
bazebTan, rogoricaa, magaliTad, ORACLE, SQL, MySQL Dda sxv. Tumca
monacemebis Senaxva bazis falebis saxiT da maTTan muSaoba maSinac
ki aris SesaZlebeli, roca zemoT CamoTvlili bazebi Cvens
gankargulebaSi ar imyofeba. saqme isaa, rom PHP-s e.w. DBM-
funqciebis meSveobiT SeuZlia monacemTa bazis funqciebis
emulireba. cxadia, aseTi gziT Seqmnil elementarul bazebs zemoT
moyvanili bazebis yvela SesaZlebloba ar gaaCniaT, magram rig
praqtikul SemTxvevebSi maT warmatebiT SeuZliaT, gadawyviton Cven
mier dasaxuli amocanebi.

DBM-monacemTa bazis gaxsna-daxurva


am mizans emsaxureba dbmopen() funqcia. mas esaWiroeba ori
argumentis gadacema:
DBM-failebamde gzis da almebis striqonis.
gansaxilveli funqciiT sargeblobisas vqmniT cvlads -
gaRebuli monacemTa bazis identifikators, romliTac Semdgom
sargebloben sxva DBM-funqciebic.
SevniSnoT, rom am funqciis gamomyenebul programebs unda
hqondes im katalogTan SeRwevaze nebarTva, romelSic
ganTavsebulia monacemTa baza.
qvemoT CamoTvlili almebi gansazRvraven monacemTa bazasTan
muSaobis reJimebs:
• r – monacemTa baza iReba mxolod wakiTxvisaTvis;
• w _ monacemTa baza iReba wakiTxvisa da CawerisTvis;
• c _ dasaSvebia monacemTa bazis Seqmna (anda misi wakiTxva da
koreqtireba, Tu igi ukve arsebobs);
• n _ iqmneba axali monacemTa baza (nadgurdeba am saxelis
mqone bazis Semcveloba, Tuki igi ukve arsebobs).
61
qvemoT moyvanil magaliTSi iReba monacemTa baza, magram Tuki
aseTi saxelis mqone baza ar arsebobs, maSin iqmneba axali:

$dbh = dbmopen(“./data/products”, “c”) or die/(“ver moxerxda DBM-is


gaReba”);

bazebTan muSaobas dasamTavreblad funqciaSi mieTiTeba


gaRebuli bazis identifikatori. mocemul SemTxvevaSi mas eqneba
saxe:
dbmclose($dbh);

bazebSi monacemebis damateba


am mizans emsaxureba dbminsert() funqcia. moviyvanoT misi
gamoyenebis magaliTi:

listingi 58
<HTML>
<head>
<title> DBM_monacemTa bazaSi monacemTa damateba </title>
</head>
<body>
Adding produts now...
<?php
$dbh = dbmopen( "./data/products", "c") or die( "couldn't open DBM" );

dbminsert($dbh, "snic crewdriver", "23.20" );


dbminsert($dbh, "Tricorder", "55.50" );
dbminsert($dbh, "ORAC AI", "2200.500" );
dbminsert($dbh, "HAl 2000", "4500.50" );

dbmclose($dbh);
?>
</body>
</html>

xazi gavusvaT Semdeg garemoebas. monacemTa bazaSi yoveli


saxis informacia, maT Soris cifrulic, striqonis saxiT Seitaneba.
am moTxovnis gamo zeda magaliTSi fasis amsaxveli monacemic
brWyalebSi CavsviT.
Tu amis Semdeg SecdomiT igive saxis mqone velis damatebas
SevecdebiT, amas araviTari Sedegi ar mohyveba:
sistema daabrunebs ara operaciis warmatebulad Catarebis
gamomxatvel “0” kods (an Secdomis Sesaxeb mauwyebel “-1”-s), aramed
– “1”-s.
62
bazis monacemTa koreqtireba
bazaSi monacemTa koreqtireba xorcieldeba dbmreplece()
funqciis gamoyenebiT. zeda programaSi Camatebis funqcia swored am
funqciiT SevcvaloT. cxadia, vcvliT velebis mniSvnelobebsac:

listingi 59
<HTML>
<head>
<title> DBM_monacemTa bazaSi monacemTa damateba an Secvla
</title>
</head>
<body>
Adding produts now...
<?php
$dbh = dbmopen( "./data/products", "c") or die( "couldn't open DBM" );

dbmreplace($dbh, "snic screwdriver", "25.20" );


dbmreplace($dbh, "Tricorder", "56.50" );
dbmreplace($dbh, "ORAC AI", "2209.50" );
dbmreplace($dbh, "HAl 2000", "4535.50" );

dbmclose($dbh);
?>
</body>
</html>

bazidan monacemebis wakiTxva


bazidan monacemebi dbmfetch() funqciis meSveobiT waikiTxeba. mas
argumentad unda gadaeces gaRebuli monacemTa bazis identifi-
katori da wasakiTxi elementis saxeli. funqcia monacemebs
striqonis saxiT dagvibrunebs.
magaliTad, Tricorder elementis fasis gasagebad vwerT Semdeg
operators:
$price = dbmfetch ($dbh, “Tricorder”);
Tu am saxelis mqone elementi bazaSi ar arsebobs, funqcia
daabrunebs cariel striqons.
magram rogor moviqceT, Tuki bazaSi Senaxuli elementebis
saxelebi CvenTvis ucnobia, anda zustad ar gvaxsovs?
vTqvaT, gvainteresebs bazaSi Setanili yoveli produqtis fasi.
aseT SemTxvevebSi dagvexmareba dbmfirstkey() funqcia, romelic
gvibrunebs bazaSi pirveli elementis saxels. SesaZlebelia, es
elementi pirvelad Setanili arc gaxldeT (aseTia DBM bazis Sida
organizebis Tavisebureba). Tumca es saqmes maincdamainc ar
gvirTulebs - dbmnexthey() funqciis meSveobiT Cven SegviZlia bazis
sxva elementebsac (aq maT gasaRebebs uwodeben) mivadgeT. elementis
63
mniSvnelobis wakiTxva ki, rogorc viciT, xorcieldeba dbmfetch()
funqciiT.
samive funqciisaTvis saWiro gaxlavT monacemTa bazis
identifikatoris miTiTeba.
miviRoT informacia bazaSi arsebuli yoveli saqonlis Sesaxeb:

listingi 60
<HTML>
<head>
<title> DBM_monacemTa bazidan monacemTa wakiTxva </title>
</head>
<body>
Here at the Impossible Gadget Shop
we're offering the following exciting
products:
<p>
<table border=1 cellpadding ="5">
<tr>
<td aling="center"> <b>product</b></td>
<td aling="center"> <b>price</b></td>
</tr>
<?php
$dbh = dbmopen( "./data/products", "c") or die( "couldn't open DBM" );
$skey = dbmfirstkey($dbh);
while ($skey !="")
{
$value=dbmfetch($dbh,$key);
print "<tr><td align =\"left\">$skey </td>";
print "<td align = \"right\">\$$value </td></tr>";
$key =dbmnextkey($dbh,$key);
}
dbmclose($dbh);
?>
</table>
</body></html>

bazaSi elementis arsebobis Semowmeba


monacemebis koreqtirebis win umjobesia, SevamowmoT bazaSi
misi arseboba. am mizniT viyenebT dbmexists() funqcias:
if (dbmexists ($dbh, “Tricorder”))
print dbmfetch ($dbh, “Tricorder”))

monacemTa bazidan elementis amogdeba


xorcieldeba martivad dbmdelete() funqciis meSveobiT.
magaliTad:
dbmdelete($dbh, “Tricorder”)
64
bazaSi ufro rTuli struqturebis Senaxva
radganac bazaSi informacia mxolod striqonis saxiT inaxeba,
principSi SesaZlebelia (magram sakmaod rTuldeba) masSi masivebisa
da obieqtebis Senaxva. am mizniT iyeneben specialur funqciebs.
Tumca roca saqme aseTi struqturebis gamoyenebamde midis, maSin
umjobesia visargebloT ufro mZlavri bazebis SesaZleblobebiT.

monacemTa bazebTan kavSiri MySQL-is magaliTze


pirveli, rac unda gavakeToT, es gaxlavT serverTan
dakavSireba. PHP-Si am mizans emsaxureba mysql-connect() funqcia. mas
gaaCnia sami argumenti:
1. kompiuteris saxeli,
2. momxmareblis saxeli,
3. paroli.
Tu serverTan dakavSireba moxerxda, funqcia gvibrunebs
mierTebis identifikators. mas cvladis mniSvnelobad imaxsovreben
da bazasTan Semdgomi muSaobisTvis iyeneben.
vaCvenoT aRniSnuli funqciis gamoyenebiT bazasTan SeerTebis
magaliTi:
$link = mysql_connect (“locallhost”, “root”, “nickel”);
if ( ! $link )
die (“couldn’t connect to MySQL”);
aRvniSnoT, rom mysql_connect() funqciaSi kompiuteris
saxelad “locallhost”-is gamoyenebiT kavSirs vamyarebT Cvensave
kompiuterze ganTavsebul SQL bazasTan.
Apache Web-serverTan mierTeba mysql-pconnect() funqciis
meSveobiTac aris SesaZlebeli. aseT SemTxvevaSi serverTan
SeerTeba ar wydeba programis muSaobis damTavrebisTanave anda
mysql_close() funqciis gamoZaxebiT.

monacemTa bazis arCeva


serverTan SeerTebis Semdeg virCevT monacemTa bazas
mysql_select_db() funqciis meSveobiT. am funqcias pirvel argumentad
gadavcemT bazis saxels, meore _ araaucilebul argumentSi
mieTiTeba serverTan mierTebis identifikatori. Tu es
identifikatori ar vuCveneT, dumiliT airCeva bolo mierTebis
identifikatori. magaliTad:
$database = ”sample”;
mysql_select_db($sample) or die (“couldn’t open $sample”);
65
Secdomebis damuSaveba
aqamde funqciis Sesrulebisas Secdomis SemTxvevaSi die
funqciis meSveobiT vwyvetdiT programis muSaobas. magram
SesaZlebelia Secdomis Sesaxeb dawvrilebiTi informaciis miRebac,
rac mis aRmosafxvrelad gamogvadgeba. kerZod, mysql_errno()
funqciiT ganisazRvreba Secdomis nomeri, xolo mysql_error()-iT –
striqoni misi aRweriT.
vaCvenoT mysql_error()-funqciis gamoyenebis magaliTi:

listingi 61
<HTML>
<head>
<title>monacemTa bazis gaReba, mierTeba da amorCeva </title>
</head>
<body>
<?php
$user = "harry";
$pass = "elbomonkey";
$db = "sample";
$link = mysql_connect( "localhost", $user, $pass );
if ( ! $link )
die( "Coudn't connect to MySQL");
print “Successfully connected to server<P>”;
mysql_select_db( $db) or die (“Could/t open $db: “.mysql_error());
print “Successfully selected database \”$db\”<P>”;
mysql_close( $link);
?>
</body>
</html>

Tu am programaSi $db cvlads, ararsebuli bazis saxels,


(vTqvaT, “sample2”-s) mivaniWebT, die() funqcia gamogviyvans Semdeg
Setyobinebas:
Couldn’t open sample2: Access denied for user: ‘harry@localhost’ to database
‘sample2’

cxrilSi monacemebis Camateba


davuSvaT, vqmniT Web-kvanZs, romelSic momxmareblebs SeuZliaT
TavianTvis domenuri saxelebis yidva.
Sample monacemTa bazaSi SevqmnaT cxrili domains saxeliTa da
4 veliT:
create table domains ( id INT NOT NULL AUTO_INCREMENT,
PRIMARY EY ( id ),
domain VARCHAR (20),
66
sex CHAR (1),
mail VARCHAR (20) );
am cxrilSi konkretuli monacemebis Casamateblad saWiroa
bazas mivmarToT SQL-moTxovniT. amisaTvis PHP-Si gaTvalis-
winebulia mysql-query() funqcia, romelsac gadaecema moTxovnis
striqoni da araaucilebeli mierTebis identifikatori:

listingi 62
<html>
<head>
<title> cxrilSi Canaweris damateba
</title>
</head>
<body>
<?php
$user = "harry";
$pass = "elbomonkey";
$db = "sample";
$link = mysql_connect( "localhost", $user, $pass );
if ( ! $link )
die( "Coudn't connect to MySQL");
mysql_select_db( $db, $link)
or die ("Couldn't open $db: ".mysql_error() );
$query=”INSERT INTO domains ( domain, sex, mail )
values( ‘123xyz.com’, ‘F’, sharp@adomain.com’ )”;
mysql_query( $query, $link)
or die ("Couldn't add data to \”domains\” table: “.mysql_error() );
mysql_close( $link);
?>
</body>
</html>
mivaqcioT yuradReba: bazaSi striqonis Camatebisas id-
velisaTvis mniSvneloba ar gviCvenebia. gavixsenoT, gasaReburi
velis mniSvneloba sistemis mier avtomaturad ganisazRvreba.
am programas ramdenjerac SevasrulebT, cxrils imdenjerve
daemateba erTi da igive monacemebis Semcveli axali Canaweri
(gansxvaveba mxolod id velis mniSvnelobaSi iqneba).
qvemoT mogvyavs programa, romelic cxrilSi amatebs
momxmareblis mier formaSi Setanil monacemebs:

listingi 63
<HTML>
<HEAD>
<TITLE> bazaSi momxmareblis mier mowodebuli informaciis
damateba
</title>
67
</head>
<BODY>
<?php
if ( isset( $domain ) && isset( $sex ) && isset( $domain ) )
{
// check user input here!
$dberror = "";
$ret = add_to_database( $domain, $sex, $mail, $dberror);
if ( ! $ret )
print "Error: $dberror<BR>";
else
print “Thank you very much”;
}
else {
write_form();
}

function add_to_database( $domain, $sex, $mail, &$dberror)


{
$user = "harry";
$pass = "elbomonkey";
$db = "sample";
$link = mysql_pconnect( "localhost", $user, $pass );

if ( ! $link )
{
$dberror = "Couldn't connect to MySQL server";
return false;
}
if (! mysql_seleqt_db( $db, $kink ) )
{
$dberror =mysql_error();
return faise;
}

$query = "INSERT INTO domains ( domain, sex, mail )


values( '$domain','$sex','$mail' )";

if ( ! mysql_query( $query, $link ) )


{
$dberror = mysql_error();
return false;
}
return true;
}

function write_form()
{
68
global$PHP_SELF;
print"<form action=\'$PHP _SELF\" method=\"POST\">\n";
print"<input type=\"text\" name=\"domain\"> ";
print"The domain you would like<p>\n";
print"<input TYPE=\"text\" name\"mail\"> ";
print"Your mail address<p>\n" ;
print"<select name=\"sex\">\n";
print"\t<option value=\"F\" Female\n";
print"\t<option value=\"M\"> Male\n";
print"\select>\n";
print"<input type=\"submit\" value=\"submit! \">\n</form>\n";
}

?>
</body>
</html>

Tavdapirvelad programa amowmebs $domain, $mail da $sex


cvladebis arsebobas (isini formidan gadmoicema). dadebiTi pasuxis
SemTxvevaSi gamoiZaxeba add_to_database() funqcia. (manamde ki
ganisazRvreba jerjerobiT carieli mniSvnelobis mqone $dberror
cvladi).
Tu aRmoCnda, rom forma jergadmocemula (mivaqcioT
ar
yuradReba - forma gadmoecema amave programas) an masSi Tundac
erTi cvladis mniSvneloba ar iqneba gansazRvruli, gamoiZaxeba
write-form() funqcia.
add_to_database() funqcias 4 argumenti gaaCnia - momxmareblis
mier gadmocemul 3 mniSvnelobas emateba $dberror cvladTan
dakavSirebuli $dberror striqonic. romelime operaciis uSedegod
dasrulebisas xdeba Secdomis Sesaxeb informaciis Cawera Cven mier
gamocxadebul $dberror gare cvladSic (da ara marto mis aslSi).
rogorc vxedavT, add_to_database() funqciaSi xorcieldeba
mcdelobebi:
• serverTan dakavSirebis,
• bazis SerCevis,
• SQL-moTxovnis gacemis.
Tu romelime maTgani warumateblad dasrulda, Secdomis
Sesaxeb informacia gadaecema $dberror cvlads, xolo
add_to_database() funqcia PHP-programas daubrunebs false mniSvne-
lobas.
sapirispiro SemTxvevaSi add_to_database() funqcia programas
ubrunebs true mniSvnelobas. nebismieri SemTxvevisTvis programas
gamohyavs Sesabamisi Setyobineba (mowmdeba $ret cvladi).
69
avtomaturad koreqtirebadi velis mniSvnelobis gageba
am mizniT dasaSvebia SQL-moTxovnis gamoyeneba, magram Tu
gvsurs informacia Canaweris SeqmnisTanave miviRoT, unda
visargebloT mysql_insert_id() funqciiT, romelic gvibrunebs bolo
jerze cxrilisTvis damatebuli Canaweris gasaReburi velis
mniSvnelobas.
magaliTad, roca gvsurs momxmarebels SevatyobinoT nomeri,
romelic mis SekveTas mieniWa, viyenebT aseT programul fragments:
$query=”INSERT INTO domains ( domain, sex, mail ) values (‘$domain’,
‘$sex’, ‘$mail’ )”;
mysql_query( $query, $link );
$id=mysql_insert_id ();
print “Thank you. Your tranzaction number is $id. Please quote it in any queries.”;

bazidan monacemebis wakiTxva


amjerad viyenebT Select tipis SQL-moTxovnas. misi warmatebiT
Sesrulebisas mysql_query() funqcia abrunebs moTxovnis Sedegis
identifikators, romelic SeiZleba gadavceT miRebuli Sedegis
damamuSavebul funqciebs.

SQL-moTxovniT moZebnili Canawerebis ricxvis gansazRvra


am informaciis miReba SesaZlebelia mysql_num_rows() funqciiT.
igi iyenebs erTaderT arguments _ manamde Sesrulebuli moTxovnis
identifikators. magaliTad:

listingi 64
<HTML>
<head>
<title>striqonebis ricxvis gansazRvra</title>
</head>
<body>
<?php

$user = "harry";
$pass = "elbomonkey";
$db = "sample";
$link = mysql_connect( "localhost", $user, $pass );
if ( ! $link )
die ( "Could't connect to MySQL");
mysql_select_db($db, $link)
or die ("Couldn't open $db: ".mysql_error() );
$result=sql_query("SELECT *FROM domains");
$num_rows=mysql_num_rows( $result);
70
print "There are currently $num_rows rows in the table<P>";
mysql_close ($link);
?>
</body>
</html>

moTxovnis Sedegebis CaTvaliereba


es procesi ciklSi SeiZleba ganvaxorcieloT. moZebnili
CanawerebisaTvis PHP qmnis e.w Sinagan maCvenebels. roca mimdinare
CanawerTan vmuSaobT, es maCvenebeli migviTiTebs momdevno Canaweris
poziciaze. amasTan, mysqe-fetch() funqciiT SesaZlebelia TiToeuli
CanawerisaTvis miviRoT misi velebisgan Semdgari masivi. cxadia, am
funqciasac unda gadaeces moTxovnis identifikatori. Canawerebis
CaTvalierebis bolos funqcia gvibrunebs false-s:

listingi 65
<HTML>
<head>
<title>cxrilis yvela Canaweris gamoyvana</title>
</head>
<body>
<?php

$user = "harry";
$pass = "elbomonkey";
$db = "sample";
$link = mysql_connect( "localhost", $user, $pass );
if ( ! $link )
die ( "Could't connect to MySQL");
mysql_select_db($db, $link)
or die ("Couldn't open $db: ".mysql_error() );
$result=sql_query("SELECT *FROM domains");
$num_rows=mysql_num_rows( $result);
print "There are currently $num_rows rows in the table<P>";
print "<table border=1>\n";
while ( $a_row =mysql_fetch ($result))
{
print "<tr>\n";
foreach ($a_row as $field)
print "\t<td>$field</td>\n";
print"</tr>\n";
}
print "</table>\n";
mysql_close ($link);
?>
</body>
</html>
71
WHILE-ciklSi mysql_fetch() funqciis mier dabrunebul mniSvne-
lobas vaniWebT $a_row cvlads.
WHILE-ciklSi gvaqvs kidev erTi cikli foreach. misi meSveobiT
xdeba masivis CaTvaliereba da misi TiToeuli elementis gamoyvana
cxrilis ujredSi.
SevniSnoT, rom Canaweris velebs saxeliTac SeiZleba
mivmarToT. amasTan, SeiZleba gamoviyenoT Semdegi ori gzidan erT-
erTi:
• pirveli gulisxmobs WHILE-is saTaurSi mysql_fetch()
funqciis nacvlad mysql_fetch_array-is gamoyenebas (igi
asocirebul masivs gvibrunebs;
• meore _ striqonis aRqmas obieqtad da misi Tvisebebis
mysql_fetch_object() funqciiT wakiTxvas:

print “<TABLE BORDER=1>\n”;


while ( $a_row = mysql_fech_array ($result) )
{
print “<TR>\n”;
print “<TD>$a_row [mail]</TD><TD>$a_row [domain]</TD>\n”;
print “</TR>”\n”;
}
print “</TABLE>\n”;

print “<table border=1>\n”;


while ( $a_row=mysql_fetch_object ($result) )
{
print “<tr>\n”;
print “<td> $a_row -> mail</td><td>$a_row -> domain </td>\n”;
print “</tr>\n”;
}
print “</table>\n”;

monacemebis Secvla
mysql-query() funqcias viyenebT cxrilSi monacemebis Sesacvle-
ladac.
moTxovnaSi figurirebs UPDATE instruqcia, romelsac
SeiZleba, vTqvaT, aseTi saxe hqondes:
UPDATE cxrilis-saxeli SET velis-saxeli =axali-mniSvneloba
Where pirobis Sesruleba;
UPDATE-instruqciis warmatebiT Sesruleba jer kidev ar
niSnavs monacemebis faqtobrivad Secvlas, risi Semowmebac SeiZleba
moxdes mysql_affected_rows() funqciiT (es funqciac mxolod
mierTebis identifikators saWiroebs argumentad da aqac, Tu am
72
identifikators ar mivuTiTebT, gamoyenebuli iqneba ukanaskneli
mierTebis identifikatori).
SevniSnoT, rom am funqciis gamoyeneba SeiZleba monacemebSi
nebismieri cvlilebebis Setanisas.
qvemoT naCvenebia programa, romelic monacemTa bazis
administrators saSualebas aZlevs, cvlilebebi Seitanos domains
cxrilis domain velSi:

listingi
<HTML>
<head>
<title>mysql-funqciis gamoyeneba cxrilSi monacemebis
Sesacvlelad
</title>
</head>
<body>
<?php

$user = "harry";
$pass = "elbomonkey";
$db = "sample";
$link = mysql_connect( "localhost", $user, $pass );
if ( ! $link )
die ( "Could't connect to MySQL");
mysql_select_db($db, $link)
or die ("Couldn't open $db: ".mysql_error() );
if ( isset ($domain) && isset ($id))
{
$query="UPDATE domains SET domain = '$domain' where id=$id";
$result = mysql_query ($query);
if (! $result)
die ("Couldn't update: ".mysql_error());
print "<h1>Table updated ". mysql_affected_rows().
"row(s) hanged</h1><p>";
?>

<form action = "<? print $PHP_SELF ?>" method = 'POST'>


<select name = "id">
<?
$result=mysql_query("SELECT domain, id FROM domains");
while ($a_row = mysql_fetch_object ($result))
{
print "<OPTION VALUE = \"$a_row->id\"";
if ( isset ($id) && $id == $a_row->id)
print "SELECTED";
print" > $a_row->domain\n";
}
73
mysql_close ($link);
?>
</select>
<input type = "text" name = "domain">
</form>
</body>
</html>

serverTan SeerTebisa da monacemTa bazis arCevis Semdeg


vamowmebT $domain da $id cvladebis arsebobas. dadebiTi pasuxis
SemTxvevaSi vayalibebT SQL-moTxovnas. masSi domain velis
mniSvnelobas vcvliT im CanawerebisTvis, romlebisTvisac id velis
mniSvneloba emTxveva (formaSi gansazRvrul) $id cvladis
mniSvnelobas.
aRvniSnoT, rom Secdomis Sesaxeb Setyobinebas ver miviRebT, Tu
$id-isTvis SevirCevT ararsebul mniSvnelobas, an roca domain velis
mniSvneloba daemTxveva Cven mier SeTavazebuls. ubralod, aseT
SemTxvevebSi mysql_affected_rows() funqcia dagvibrunebs “0”-s.
sapirispiro SemTxvevebSi ki (magaliTad, zemoT moyvanili
programisaTvis) dagvibrundeba mniSvneloba “1”, romelic ekranzec
aisaxeba.
programas ekranze gamohyavs forma, romlis daxmarebiTac
SeiZleba cxrilSi cvlilebebi SevitanoT. Domain da id velebis
Sesaxeb informacias gvawvdis mysql_query() funqcia, romelic HTML-
teqstSi siis formirebisaTvis gamoiyeneba. siaSi virCevT monacemebs,
maTi Semdgomi koreqtirebis mizniT. Tu forma ukve gadaveciT da
amorCeuli id mniSvneloba emTxveva mimdinare velis mniSvnelobas,
Option elementSi Segvyavs Selected striqoni, raTa axali
mniSvneloba gamoirCes formaSi.

garemos Sesaxeb informaciis


Semcveli cvladebi
zogierT aseT cvlads, romelTac PHP an serveri qmnis, ukve
gavecaniT.
serverze ganTavsebul PHP-programidan Cven SegveZleba am
cvladebis meSveobiT miviRoT informacia, magaliTad, sakuTari
saitis mnaxvelebis Sesaxeb. magram radganac yovelTvis ver
xerxdeba sistemaSi an serverze arsebul resursebTan SeRweva,
jobs, winaswar SevamowmoT mcdelobaTa Sedegebi. moviyvanoT amis
magaliTi:

listingi 67
<HTML>
<head>
74
<title>brouzeris fanjaraSi cvladebis mniSvnelobebis
gamoyvana
</title>
</head>
<body>
<?php
$envs = array ( "HTTP_REFERER", "HTTP_USER_AGENT",
"REMOTE_ADDR", "REMOTE_HOST",
"QUERY_STRING", "PATH_INFO");
foreach ($envs as $env)
{
if ( isset ($$env))
print "$env: ${$env}<br>";
}
?>
</body>
</html>

vxedavT, rom, striqonebis aseTive saxelis mqone cvladebad


gardaqmnis mizniT, programaSi gamoyenebulia dinamikuri cvladebi.
ekranze aisaxeba zemoT moyvanili programis muSaobis Sedegebi.
miRebuli monacemebi generirdeba am programis gamoZaxebis Sedegad
sxva WEB-furclidan Semdeg kavSirze dawkapunebiT:
<A HREF=’eg13.1.php/my_path_info?query_key=query_value’> go </a>
Cans, rom programis gamosaZaxeblad kavSiri iyenebs fardobiT
gzas. damatebiTi informacia gzis Sesaxeb Caiwereba dokumentis
my_path_info saxelis Semdeg da gadaecema $PATH_INFO cvlads.
? kiTxvis niSnis Semdeg ganTavsebulia e.w. moTxovnis striqoni
query_key=query_value, romelic aisaxeba Sesabamis $QUERY-STRING
cvladSi.
moTxovnis striqoni Sedgeba &-simboloebiT gayofil Semdegi
wyvilebisgan:
saxeli/mniSvneloba
amasTan, orazrovnobis Tavidan asacileblad es wyvilebi
eqvemdebareba e.w. URL-kodirebas _ “saeWvo” simboloebi icvleba
maTi 16-obiTi ekvivalentebiT.
PHP uzrunvelyofs striqonSi miTiTebuli TiToeuli cvladis
globalur cvladad aRqmis SesaZleblobas. magaliTad, Cveni
SemTxvevisTvis es globaluri cvladi iqneba $query_value, xolo
misi dekodirebuli mniSvneloba ki _ “query_value”.
75
klientis serverTan HTTP oqmiT SeerTebis
zogierTi sakiTxi
internetSi hiperteqsti, Cveulebriv, http oqmiT gadaigzavneba.
amasTan, klienti da serveri erTmaneTs informacias ucvlian
sakuTari Tavis Sesaxebac. bevri ram am informaciidan garemos
amsaxvel cvladebSic SeiZleba movipovoT.
moTxovna
klientis mier serveridan monacemebis moTxovna sam komponents
SeiZleba moicavdes:
• moTxovnis striqoni // aucilebeli komponentia
• saTaurebis striqoni // zogjer ar iyeneben
• moTxovnis saxeli // zogjer ar iyeneben
moTxovnis striqonSi miTiTebulia:

• meTodi (Cveulebriv, airCeva GET, HEAD an POST);


• moTxovnili dokumentis misamarTi;
• gamoyenebuli HTTP-is versia (HTTP/1.0 an HTTP/1.1).

moTxovnis striqonis tipuri magaliTia:


GET / mydoc.html HTTP.1.0
moyvanil magaliTSi klienti servers gadascems GET moTxovnas,
anu igi moiTxovs mTeli dokumentis gadmogzavnas, TviTon ki,
faqtobrivad, monacemebs ar agzavnis (dasaSvebia mxolod mcire
moculobis monacemebi “mivabaT” moTxovnis striqonis saxiT URL-
misamarTs).
HEAD meTods maSin viyenebT, roca mxolod dokumentis Sesaxeb
gvsurs informaciis miReba.
POST meTodi gamoiyeneba klientidan serverze ufro “soli-
duri” informaciis gadagzavnis saWiroebisas (magaliTad, HTML-
formebis).
rogorc zemoT aRvniSneT, zogjer ifarglebian moTxovnis
mxolod pirveli komponentiT. oRond aseT SemTxvevaSi servers
moTxovnis dasrulebis Sesaxeb unda vamcnoT am moTxovnis bolos
carieli striqonis gadagzavniT.
klientebis umetesoba servers ugzavnis saxeli / mniSvneloba
wyvilebisagan Semdgar saTaurebis ganyofilebasac. zogierTi am
wyvilebidan misawvdomi xdeba garemos cvladebis meSveobiT.
wyvilebi (saTaurebi) Sedgeba erTmaneTisgan orwertiliT
gamoyofili gasaRebis dasaxelebisa da mniSvnelobisagan.
cxrilSi moyvanilia informacia zogierTi gasaRebis Sesaxeb:
76

saxeli aRwera

Accept informaciis matarebelTa tipebi,


romlebTanac SeuZlia klients muSaoba.
Accept-Encoding monacemTa SekumSvis tipebi, romleb-
Tanac SeuZlia klients muSaoba.
Accept-Charset simboloTa cxrilebi, romelTac
klienti upiratesobas aniWebs.
Accept-Language klientisaTvis sasurveli ena (magali-
Tad, “en” - inglisuri)
Host kompiuteris qseluri saxeli, romelsac
momxmarebelis moTxovna egzavneba.
Referer dokumenti, romlidanac moxda
moTxovnis gacema.
User-Agent informaciis programa-klientis tipisa
da versiis Sesaxeb.

GET da HEAD meTodebSi servers carieli striqoni egzavneba


saTaurebis ubnis, xolo POST meTodSi – moTxovnis sxeulis
Semdeg.
rac Seexeba moTxovnis sxeuls, is Zalze hgavs moTxovnis
striqons - masSic figurireben URL-kodirebuli wyvilebi:
saxeli /mniSvneloba
qvemoT moyvanilia Netscape-brouzeris mier serverisadmi
gadagzavnili moTxovnis tipuri varianti:

listingi 68
GET / HTTP / 1.0
Referer: http:/ zink.demon.co.uk :8080/ matt/ php-book/ network/ test2.php
Connection: Keep-Alive
User-Agent: Mozilla 4.6 (XLL; T; Linux 2.2.6-15ampac ppc)
Host : www.corrosive.co.uk
Accept: image/ gif,image/ x-xbitmap, image/ jpg,image/ pjpeg, image/png,
*/*
Accept-Encoding: gzip
Accept-Language: en
Accept-Charset: iso-8859-1, *, utf-8
77
pasuxi
moTxovnis miRebis Semdeg serveri klients ugzavnis pasuxs,
romelic, Cveulebriv, aseve sami nawilisgan Sedgeba:
• statusis striqoni;
• saTaurebis ubani;
• moTxovnis sxeuli.
zogierTi saTauri SeiZleba iseTive iyos, rogorc wina
SemTxvevaSi (pirvel yovlisa, es exeba informacias mesame punqtis
Sesaxeb).
statusis striqoni Sedgeba serveris mier gamoyenebuli HTTP
oqmis versiis, pasuxis kodisa da am kodis aRmweri informa-
ciisgan.
moviyvanoT zogierTi kodi Tavisi aRwerilobiT:

kodi teqsti aRwera

200 OK moTxovna warmatebiT


Sesrulda.
moTxovnili monacemebi
gadmoica.

301 Moved Permanetly ganlagebis amsaxvel


(monacemebi saTaurSi gamodis axali
gadaadgilebulia) misamarTi.

302 Moved Temporarily ganlagebis amsaxvel


(monacemebi droebiT saTaurSi gamodis axali
adgilSecvlilia) misamarTi.

404 Not Found am misamarTze monace-


(monacemebi ver mebi ver moiZebna.
moiZebna)

500 Internal Server Error problemebia serverze


(serveris Sida an CGI-programaSi.
Secdoma)

ai, warmatebulad Sesrulebuli moTxovnis amsaxveli pasuxi:H


HTTP/1.1 200 OK
axla ki moviyvanoT serveridan gadmogzavnili pasuxebis
yvelaze tipuri saTaurebi:
78

saxeli aRwera

Date mimdinare TariRi


Server serveris saxeli da versia
Content-Type sxeulis Semcvelobis MIME-tipi
Content-Length sxeulis zoma (baitebSi)
Location alternatiuli dokumentis sruli misamarTi

literatura:

1. Освой самостоятельно PHP4 за 24 часа. Мэт Зандстра, «Вильямс», 2001.

2. А. Гончаров. Самоучитель HTML, «Питер» , 2001.

3. А. Матросов, А. Сергеев, М. Чаунин. HTML4, «Вильямс» , 2003.

4. Учебный курс «Компьютерные сети», Microsoft Press. Санкт-Петербург,


1999.

5. Освой самостоятельно JavaScript за 24 часа. Майкл Монкур, «Вильямс»,


2002.

6. Э. Кингсли-Хью, К. Кингсли-Хью, JavaScript 1.5, Учебний курс, «Питер» ,


2001.
79
sarCevi

ƒ Sesavali ------------------------------------------------------------------------ 3
ƒ PHP–is dayeneba ------------------------------------------------------------ 4
ƒ Apache serveris konfigurireba ---------------------------------- 5
ƒ Cveni pirveli programa PHP-ze ---------------------------------- 5
ƒ cvladebi ----------------------------------------------------------------------- 7
ƒ nakadis marTva --------------------------------------------------------------- 13
ƒ ciklebi -------------------------------------------------------------------------- 15
ƒ funqciebi ----------------------------------------------------------------------- 19
ƒ masivebi ------------------------------------------------------------------------- 25
ƒ obieqtebi ---------------------------------------------------------------------- 32
ƒ kvlav klasebis Sesaxeb------------------------------------------------ 39
ƒ formebTan muSaoba ------------------------------------------------------ 47
ƒ failebTan muSaoba ----------------------------------------------------- 58
ƒ DBM-funqciebTan muSaoba ------------------------------------------ 60
ƒ monacemTa bazebTan kavSiri MySQL-is magaliTze ---- 64
ƒ garemos Sesaxeb informaciis Semcveli cvladebi ---- 73
ƒ klientis serverTan HTTP oqmiT SeerTebis
zogierTi sakiTxi ------------------------------------------------------ 75
ƒ literatura ---------------------------------------------------------------- 78