Website Development Prices

Search Blog

Saturday, May 9, 2015

Kreiranje konstanti (Creating constants)

Ako vam zatreba da se promenljiva ne menja tj. treba da njena vrednost bude fiksna, napravite konstante.

Konstante se prave pomocu funkcije define (bez znaka $). Ime konstante pocinje slovom ili podvucenom crtom (_). Konstanti se dodeljuju ime i vrednost

Primer:

define ("pi", 3.141)

Ime konstante je uvek pod duplim navodnicima, dok je vrednost koja se dodeljuje pod navodnicima samo ako je u pitanju string (niz karaktera).


<!DOCTYPE html>
<html>
<head>
<title>Koriscenje PHP konstanti</title>
<!--
<link rel="stylesheet" type="text/css" href="css/style.css">
-->
</head>
<body>

<?php
define ("pi", 3.141);

echo "Konstanta pi sadrzi " , pi, "<br>"; // rezultat: Konstanta pi sadrzi 3.141
?>

</body>

</html>

Ovaj fajl sacuvajte kao phpkonstante.php. Fajl se pokrece unosom u address bar http://localhost/PHPtuts/phpkonstante.php. Ukoliko vam se fajl ne pokrene proverite da li ste pokrenuli XAMPP.

If you need, variable not to change or you need value to be fixed, make constants.

Constants are made either via define function (no sign $). Constant name starts with a letter or underscored (_). The constants are assigned the name and value.

Example:

define ("pi", 3.141)

Constant name is always under the double quotation marks, while the value of which is given in quotation marks only if it comes to a string (character string).

<! DOCTYPE html>
<html>
<head>
<title> Using PHP constants </title>
<! -
<link rel = "stylesheet" type = "text/css" href = "css/style.css">
->
</head>
<body>

  <? php
define ("pi", 3.141);

echo "Constant contains pi " , pi, "<br>"; // R: Constant contains pi 3.141
  ?>

   </body>

</html>


This file save as phpconstants.php. The file is run by entering the address bar http: //localhost/PHPtuts/phpconstants.php. If you file does not start check if you started XAMPP.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.