0% found this document useful (0 votes)
31 views

WP Predavanja PHP

PHP is a server-side scripting language that can be embedded into HTML code to add dynamic functionality. Some key features of PHP include: - It uses tags like <?php ?> to embed PHP code into HTML documents - It has various data types like integers, floats, booleans, strings, arrays and objects that can be used to store and manipulate data - Variables in PHP are prefixed with a $ symbol and can be assigned values dynamically - PHP supports various operators, loops, conditions and functions for control flow and programming logic

Uploaded by

edukacija11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

WP Predavanja PHP

PHP is a server-side scripting language that can be embedded into HTML code to add dynamic functionality. Some key features of PHP include: - It uses tags like <?php ?> to embed PHP code into HTML documents - It has various data types like integers, floats, booleans, strings, arrays and objects that can be used to store and manipulate data - Variables in PHP are prefixed with a $ symbol and can be assigned values dynamically - PHP supports various operators, loops, conditions and functions for control flow and programming logic

Uploaded by

edukacija11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

PHP

Miladin Stefanovi
[email protected]

PHP

Karakteristike

Server side script jezik


Ukljuuje se u HTML

Osnovni tagovi
<?php
... PHP code ...
?>

HTML i PHP

<html>
<head></head>
<body>

<?php
// Komentar u jednoj liniji

Zdravo! Kako si?


<br />

/* komentar koji obuhvata


vie linija */

<?php
// print output
echo Odlino';
?>

?>

</body>
</html>

Komentari u php-u

Promenljive

<html>
<head></head>
<body>
Tvoji lini podaci?
<br />
<?php
// definisanje promenljivih
$name = Petar';
$rank = Student';
$serialNumber = 173;
// print output
echo Ja sam <b>$name</b>, redovni <b>$rank</b>. Moj broj je, <b>
$serialNumber</b>.";
?>
</body>
</html>

Oznaavanje promenljivih

<?php
$var = Pera';
$Var = Mika';
echo "$var, $Var";

// outputs Pera, Mika"

$4site = 'not yet'; // loe; poinje brojem


$ _4site = 'not yet'; // moe
$tyte = 'mansikka'; // moe; '' je (Extended) ASC
II 228.
?>

Dodeljivanje vrednosti promenljivima

<?php
$age = $dob + 15;

<?php
$ugao1 = $ugao =
$ugao = 60;

?>
?>

Dodeljivanje vrednosti preko refernce

<?php
$foo = Mika'; // Dodeljovanje 'Bob' za $foo
$bar = &$foo; // Referenca $foo preko $bar
$bar = "My name is $bar"; // Menja se $bar...
echo $bar;
echo $foo; // $foo je takodje promenjeno
?>

Tipovi promenljivih - Boolean


Boolean: true / false
<?php
$auth = true;
?>

0 netano
Bilo koji negativni (-1)
ili pozitivan broj tano

Tipovi promenljivih - Integer


Integer: 75, -95, 2000 ili 1.

<?php

$a = 1234; // pozitivan
$a = -123; // negativan
$a = 0x1A; // heksadecima
lan broj
?>

<?php
$godine = 99;
?>

Tipovi promenljivih - Floating-point

<?php
$temperatura = 26.89;
?>

<?php
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>

Primeri

<?php
var_dump(25/7); // float(3.5714285714286)
var_dump((int) (25/7)); // int(3)

<?php
echo (int) ( (0.1+0.7) * 10 ); // echoes 7!
?>

Primeri:

<?php
$float = 1.5;
// float(1.5)
$float = 1,5;
// Parse error: syntax error,
unexpected ','
$float = (float) '1.5'; // float(1.5)
$float = (float) '1,5'; // float(1)
?>

Tipovi promenljivih - String

<?php
$identity = 'James Bond';
$car = 'BMW';
// "James Bond vozi BMW"

$sentence = "$identity vozi a <hr> $car";


echo $sentence;
?>

Primer

<html>
<head>
</head>
<body>
<?php
// set quantity
$quantity = 1000;
// set original and current unit price
$origPrice = 100;
$currPrice = 25;
// calculate difference in price
$diffPrice = $currPrice - $origPrice;
// calculate percentage change in price
$diffPricePercent = (($currPrice - $origPrice) *
100)/$origPrice
?>

<table border="1" cellpadding="5"


cellspacing="0">
<tr>
<td>Quantity</td>
<td>Cost price</td>
<td>Current price</td>
<td>Absolute change in price</td>
<td>Percent change in price</td>
</tr>
<tr>
<td><?php echo $quantity ?></td>
<td><?php echo $origPrice ?></td>
<td><?php echo $currPrice ?></td>
<td><?php echo $diffPrice ?></td>
<td><?php echo $diffPricePercent ?>%</td>
</tr>
</table>
</body>
</html>

Primeri

<?php
// ovo
$a = 5;
$a = $a + 10;
// ...i ovo je isto
$a = 5;
$a += 10;
?>

Primeri

<?php
// set up some string variables
$a = 'the';
$b = 'games';
$c = 'begin';
$d = 'now';
// combine them using the concatenation operator
// this returns 'the games begin now<br />'
$statement = $a.' '.$b.' '.$c.' '.$d.'<br />';
print $statement;
// and this returns 'begin the games now!'
$command = $c.' '.$a.' '.$b.' '.$d.'!';
print $command;
?>

Konstante

<?php
// Ispravno
define("FOO", "something");
define("FOO2", "something else");
define("FOO_BAR", "something more"
);
// Neispravno
define("2FOO", "something");
// Moze ali nije preporucljivo
define("__FOO__", "something");
?>

<?php
define("CONSTANT", "He
llo world.");
echo CONSTANT; // outpu
ts "Hello world."

Globalne promenljive

$GLOBALS
$_SERVER
$_GET
$_POST
$_FILES
$_COOKIE
$_SESSION

Magine konstante
Nazi v
__LINE__

Opis
Trenutni broj linije u fajlu

__FILE__

Puno ime i putanja fajla. Korisno kod naredbe include

__DIR__

Direktorijum u kome se nalazi fajl. Ekvivalentno dirname(__FILE__).

__FUNCTION__

The Naziv funkcije.

__CLASS__

Naziv klase.

__METHOD__

Naziv metode

__NAMESPACE__

Naziv namespacea.

Operatori 1
Primer

Naziv

Primer

Naziv

-$a

Promena znaka

$a & $b

And

$a + $b

Sabiranje

$a - $b

Oduzimanje

$a | $b

Or (inclusive or)

$a * $b

Mnoenje

$a ^ $b

Xor (exclusive or)

$a / $b

Deljenje

~ $a

Not

$a % $b

Moduo
$a << $b

Shift left

$a >> $b

Shift right

Operatori 2
Primer

Naziv

Example

Name

$a == $b

Jednako

++$a

Pre-inkrement

$a === $b

Identino

$a++

Post-inkrement

$a != $b
$a <> $b

Nije jednako
Nije jednako

--$a

Pre-dekrement

$a--

Post-dekrement

$a !== $b

Nije identicno

$a < $b
$a > $b
$a <= $b
$a >= $b

Manje od
Vece od
Manje ili jednako
Vece ili jednako

Operatori 3

<?php
$a = "Hello ";
$b = $a . "World!"; // $b sadrzi "Hello World!"
$a = "Hello ";
$a .= "World!";
?>

// $a sadrzi "Hello World!"

Primeri

<?php
echo "thr"."ee";
//"three"
echo "twe" . "lve";
//"twelve"
echo 1 . 2;
//"12"
echo 1.2;
//1.2
echo 1+2;
//3
?>

Nizovi

<?php
$arr = array("foo" => "bar", 12 => true);
echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>

Nizovi

<?php
$arr = array("somearray" => array(6 => 5, 13
=> 9, "a" => 42));
echo $arr["somearray"][6]; // 5
echo $arr["somearray"][13]; // 9
echo $arr["somearray"]["a"]; // 42
?>

Nizovi

<?php
// Ovaj niz je isti kao ...
array(5 => 43, 32, 56, "b" => 12);
// ...ovaj niz
array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
?>

Nizovi
<?php
$arr = array(5 => 1, 12 => 2);
$arr[] = 56;

// Ovo je isto kao $arr[13] = 56;


// u ovoj tacki skripta

$arr["x"] = 42; // Ovo dodaje novi element sa kljucem "x"


unset($arr[5]); // Uklanja element iz niza
unset($arr); // Brisanje celog niza
?>

Da li ovo radi?

<body bgcolor="<?php echo '#993333'; ?>">

<? $knjige=rand(1,18); ?>


<A CLASS="copy" HREF="http://www.cqm.rs/
<? echo $knjige; ?>.php" TARGET="_parent">
<IMG SRC="http://www.cqm.rs/knjige/<? echo
$knjige; ?>.jpg" border=1></IMG></A>

You might also like