Website Development Prices

Search Blog

Saturday, May 9, 2015

Tipovi podataka u PHP-u (Data Types in PHP)

Osam tipova podataka koje PHP podrzava:

  • boolean
  • ineteger
  • float
  • string
  • array
  • object
  • resource
  • NULL
boolean - sadrzi vrednost true/false (tacno/ne tacno).

ineteger - sadrzi cele brojeve, kao sto su -2, 7, 0, itd.

float - sadrzi brojeve sa pokretnim zarezom (tip double) kao sto 3.141 ili 2.758.

string - sadrzi tekst, npr. "Pozdrav iz Beograda" .

array - sadrzi niz sa podacima.

object - sadrzi objekte iz programa.

resourcesadrzi resurse sa podacima.


NULL - sadrzi vrednost NULL.

PHP sam odredjuje tip promenljive na osnovu podatka.

<?php

//Ovaj iskaz ce promenljivoj $variable dodeliti tip string
$variable = "Pozdrav iz Beograda <br>"; 

echo "$variable";

//Ovaj iskaz ce napraviti promenljivu tipa float
$variable = 1.2345;

echo "$variable <br>";

//Ovaj iskaz ce napraviti promenljivu tipa boolean
$variable = TRUE;

echo "$variable <br>";
?>

Ovaj fajl sacuvajte kao tipovipodataka.php.

Ako pocnete da mesate tipove podataka, moze doci do problema. Npr. kada promenljivoj $variable dodate vrednost pomocu operatora + (o cemu ce biti reci u delu Rukovanje nizovima i stringovima).

Primeri:

<?php

/*
$variable = "Pozdrav iz Beograda <br>";
echo "$variable";

$variable = 1.2345;
echo "$variable <br>";

$variable = TRUE;
echo "$variable <br>";
*/

$variable = "0 <br>"; // $variable je string podesen na 0
echo "$variable";

$variable = $variable + 2; // $variable je sad ceo broj sa vrednoscu 2
echo "$variable <br>";

$variable = $variable + 1.1; // $variable je sada tipa float sa vrenoscu 3.1
echo "$variable <br>";

$variable = 2 + "7 motora"; // $variable je sada ceo broj sa vrednoscu 9
echo "$variable";

?>

Eight types of data that PHP supports:

  • boolean
  • ineteger
  • float
  • string
  • array
  • object
  • resource
  • NULL


boolean - contains true/false value (true / not true).

integer - contains whole numbers, such as -2, 7, 0, and so on.

float - contains floating point (type double) as 3.141 or 2.758.

string - contains text, for example. "Greetings from Belgrade".

array - contains a arrays of data.

object - contains objects from the program.

resource - contains resources data.

PHP defines the variable type based on the data.

<? php

// This statement will assign to variable $variable type string
$variable = "Greetings from Belgrade <br>";

echo "$variable";

// This statement will create a variable of type float
$variable = 1.2345;

echo "$variable <br>";

// This statement will create a variable of type Boolean
$variable = TRUE;

echo "$variable <br>";
?>

This file save as datatypes.php.

If you start to mix the types of data, problems may occur. For example, when to the variable $variable add value by using the + operator (which will be discussed in part handling arrays and strings).


Examples:

<?php

/*
$variable = "Hello from Belgrade <br>";
echo "$variable";

$variable = 1.2345;
echo "$variable <br>";

$variable = TRUE;
echo "$variable <br>";
*/

$variable = "0 <br>"; // $variable is string st to 0
echo "$variable";

$variable = $variable + 2; 
// $variable is now whole number with value 2
echo "$variable <br>";

$variable = $variable + 1.1; // $variable is now float type with value 3.1
echo "$variable <br>";

$variable = 2 + "7 motobikes"; // $variable is now whole number with value 9
echo "$variable";

?>

No comments:

Post a Comment

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