Intro to PHP
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
PHP?
• Primer:<html>
<head>
<title>Prvi PHP program</title>
</head>
<body>
<?php
echo "<p>Zdravo Djaci!</p>";
print "<p>Zdravo Djaci?</p>"
?>
</body>
</html>
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...
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 . . ." %>
Osnovna sintaksa
• Malo napredniji prelaz<?php
if ($expression) {
?>
<strong>This is true.</strong>
<?php
} else {
?>
<strong>This is false.</strong>
<?php
}
?>
Separacija instrukcija
• Kao C ;
• tag (?>) isto podrayumeva kraj instrukcije
• Ekvivalentno je:
<?php
echo "This is a test";
?>
<?php echo "This is a test" ?>
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
?>
Tipovi podataka
• Osnovni.• 4 skalarna:
• boolean
• integer
• floating-point number (float)
• string
• 2 složena:• array
• object
• 2 specijalna:• resource
• NULL
Scalari
• Primer:• $A = true; (boolean)
• $B = 20; (integer)
• $C = 3.1415; (float)
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’;
Stringvi - funkcije
• string_Funkcije.doc
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
• <?php$cars = array("Volvo", "BMW", "Toyota");$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {echo $cars[$x];echo "<br>";
}?>
<?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>";}?>
Objekti
• Inicijalizacija• new statement pravi insatncu
<?php
class foo
{
function do_foo()
{
echo "Doing foo.";
}
}
$bar = new foo;
$bar->do_foo();
?>
Null
• Promenjiva nema dodeljenu vrednost
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)
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.
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.
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
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.
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.
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”]
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’].
Control structures
• If, then, else, elseif, while, foreach, do…while, for, break, continue, switch, case, return
• Jako slično kao C/C++
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
Funkcije
• sintaksa:function funct1 ($arg_1, $arg_2, ...,
$arg_n)
{
echo "Example function.\n";
return $retval;
}