intro to phptnt.etf.rs/~oe4ppt/pdf/vezbe/oe4ppt_2017_3.pdf · karakteristike •podrška bazama...

28
Intro to PHP

Upload: others

Post on 10-Mar-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Intro to PHP

Page 2: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

PHP?• PHP (recursive acronym "PHP: Hypertext

Preprocessor")

• Open Source general-purpose scripting language

• Web development

• Ugrađen u HTML.• HTML script sa kodom koji nešto radi

• Izvršavanje na serveru

Page 3: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

PHP?

• Primer:<html>

<head>

<title>Prvi PHP program</title>

</head>

<body>

<?php

echo "<p>Zdravo Djaci!</p>";

print "<p>Zdravo Djaci?</p>"

?>

</body>

</html>

Page 4: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Karakteristike

• Podrška bazama podataka:• Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase,

Ovrimos, Empress, FrontBase, PostgreSQL, FilePro (read-only), mSQL, Solid Hyperwave, Direct MS-SQL, Sybase, IBM DB2, MySQL, Velocis, Informix, ODBC, Unix dbm

• DBX database apstrakcija

• ODBC, the Open Database Connection standard,

• Podrška protokolima kao što su LDAP, IMAP, SNMP, NNTP, POP3, HTTP, COM (on Windows) itd...

Page 5: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Osnovna sintaksa

• Prelaz iz HTML1. <? echo ("this is the simplest, an SGML processing instruction\n"); ?>

<?= expression ?> This is a shortcut for "<? echo expression ?>"

2. <?php echo("if you want to serve XHTML or XML documents, do like this\n"); ?>

3. <script language="php">

echo ("some editors (like FrontPage) don’t

like processing instructions");

</script>

4. <% echo ("You may optionally use ASP-style tags"); %>

<%= $variable; # This is a shortcut for "<%

echo . . ." %>

Page 6: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Osnovna sintaksa

• Malo napredniji prelaz<?php

if ($expression) {

?>

<strong>This is true.</strong>

<?php

} else {

?>

<strong>This is false.</strong>

<?php

}

?>

Page 7: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Separacija instrukcija

• Kao C ;

• tag (?>) isto podrayumeva kraj instrukcije

• Ekvivalentno je:

<?php

echo "This is a test";

?>

<?php echo "This is a test" ?>

Page 8: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Comments

• Isto kao C, C++<?php

echo "This is a test"; // This is a one-

line c++ style comment

/* This is a multi line comment

yet another line of comment */

echo "This is yet another test";

echo "One Final Test"; # This is shell-

style style comment

?>

Page 9: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Tipovi podataka

• Osnovni.• 4 skalarna:

• boolean

• integer

• floating-point number (float)

• string

• 2 složena:• array

• object

• 2 specijalna:• resource

• NULL

Page 10: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Scalari

• Primer:• $A = true; (boolean)

• $B = 20; (integer)

• $C = 3.1415; (float)

Page 11: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Stringovi

• Jedan navod• Promenljive su literali

• Dupli navod• Promenljive se prvo obračunaju

• $A = 20;

• echo “The value of A is $A”;

• echo ‘The value of A is $A’;

Page 12: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Stringvi - funkcije

• string_Funkcije.doc

Page 13: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Nizovi

• Primer:• <?php

$cars = array("Volvo", "BMW", "Toyota");echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";?>

• Vrste:• Indeksirani numerički index

• Asociativni – imenovani ključevi

• Multidimenzioni

Page 14: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

• <?php$cars = array("Volvo", "BMW", "Toyota");$arrlength = count($cars);

for($x = 0; $x < $arrlength; $x++) {echo $cars[$x];echo "<br>";

}?>

Page 15: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

<?php$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $x_value) {echo "Key=" . $x . ", Value=" . $x_value;// konkatenacija stringova

echo "<br>";}?>

Page 16: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Objekti

• Inicijalizacija• new statement pravi insatncu

<?php

class foo

{

function do_foo()

{

echo "Doing foo.";

}

}

$bar = new foo;

$bar->do_foo();

?>

Page 17: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Null

• Promenjiva nema dodeljenu vrednost

Page 18: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Koverzija tipova

• primer$foo = "0"; // $foo is string (ASCII 48)

$foo += 2; // $foo is now an integer (2)

$foo = $foo + 1.3; // $foo is now a float

(3.3)

$foo = 5 + "10 Little Piggies"; // $foo

is integer (15)

$foo = 5 + "10 Small Pigs"; // $foo is

integer (15)

Page 19: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Promenjive

• primer$var = "Bob";

$Var = "Joe";

echo "$var, $Var"; // outputs "Bob, Joe"

$4site = ’not yet’; // invalid; starts

with a number

$_4site = ’not yet’; // valid; starts

with an underscore

$täyte = ’mansikka’; // valid; ’ä’ is

ASCII 228.

Page 20: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

PHP predefinisane promenjive

• $argv• Array of arguments passed to the script.

• $argc• Contains the number of command line parameters passed to the script (if run on the

command line).

• $PHP_SELF• The filename of the currently executing script, relative to the document root.

• $HTTP_COOKIE_VARS• An associative array of variables passed to the current script via HTTP cookies.

• $_COOKIE• An associative array of variables passed to the current script via HTTP cookies.

• $HTTP_GET_VARS• An associative array of variables passed to the current script via the HTTP GET method.

Page 21: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

PHP predefinisane promenjive

• $_GET• An associative array of variables passed to the current script via the HTTP GET

method.

• $HTTP_POST_VARS• An associative array of variables passed to the current script via the HTTP POST

method.

• $_POST• An associative array of variables passed to the current script via the HTTP POST

method.

• $HTTP_POST_FILES• An associative array of variables containing information about files uploaded

via the HTTP POST method.

• $_FILES• An associative array of variables containing information about files uploaded

via the HTTP POST method

Page 22: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

PHP predefinisane promenjive

• $HTTP_ENV_VARS• An associative array of variables passed to the current

script via the parent environment.• $_ENV

• An associative array of variables passed to the current script via the parent environment.

• $HTTP_SERVER_VARS• An associative array of variables passed to the current

script from the HTTP server. • $_SERVER

• An associative array of variables passed to the current script from the HTTP server.

Page 23: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

PHP predefinisane promenjive

• $HTTP_SESSION_VARS• An associative array of session variables passed to the

current script.

• $_SESSION• An associative array of session variables passed to the

current script.

• $_REQUEST• An associative array merged from the GET, POST, and

Cookie variables. In other words - all the information that is coming from the user, and that from a security point of view, cannot be trusted.

Page 24: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Scope

• Prom. deklarisane van funkcija i klasa su globalne ali van funkicja

• Nisu automatski definisane unutar funkcija!

• Eksplicitna deklaracija:global $a, $b;

• $GLOBALS niz$foo = $GLOBALS[“a”]

Page 25: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

HTML Forme (GET & POST)

• Kada se forma pošalje u PHP script, svaka proenjiva postaje dostupna.

• Locirana u asocijativnim niyovima $HTTP_POST_VARS, $HTTP_GET_VARS, and/or $HTTP_POST_FILES,

• Primer. Jednostavna forma<form action="foo.php" method="post">

Name: <input type="text" name="username"><br>

<input type="submit">

</form>

• Kada se prehodna forma posalje serveru vrednost sledeceg tekstuakbog unosa je u $HTTP_POST_VARS[’username’].

Page 26: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Control structures

• If, then, else, elseif, while, foreach, do…while, for, break, continue, switch, case, return

• Jako slično kao C/C++

Page 27: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Ostalo

• require(/path/to/file.php)• Includes and evaluates the specified file

• include(...)

• Same thing

• include_once(...)

• Makes sure same file is not included multiple times

Page 28: Intro to PHPtnt.etf.rs/~oe4ppt/pdf/vezbe/OE4PPT_2017_3.pdf · Karakteristike •Podrška bazama podataka: •Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase, Ovrimos, Empress,

Funkcije

• sintaksa:function funct1 ($arg_1, $arg_2, ...,

$arg_n)

{

echo "Example function.\n";

return $retval;

}