Website Development Prices

Search Blog

Friday, September 25, 2015

Koriscenje if iskaza (Using if statement)

if(izraz)
iskaz

Izraz je izraz cija vrednost moze da se proceni na tacno ili netacno. Npr: ako je 7 > 3, on vraca tacno. Ako je vrednost izraza tacno, iskaz se izvrsava. Ako je vrednost izraza netacno, iskaz se ne izvrsava. 

Upotrebom iskaza if, u skripti mozete da donosite odluke  u trenutku izvrsenja. Te odluke se zasnivaju na trenutnim vrednostima  podataka, kao sto su npr:

  • tekst koji je korisnik uneo  na web strani npr. lozinka
  • vrednosti koje se vracaju iz baze podataka npr. raspolozive zalihe
  • podaci koji su preuzeti sa drugog websajta npr. cene
Primer:


<?php

$kubikazamotora = 150;
if ($kubikazamotora < 200)
echo "Nice motorcycle.";

?>

Ovaj fajl sacuvajte kao iskaz-if.php.

Rezultat: Nice motorcycle.

Objasnjenje:

U ovom iskazu promenljiva $kubikazamotora ima vrednost 150, tako da ce se izvrsiti iskaz echo

Ako imate vise iskaza i zelite da ih izvrsite ako je zadovoljen neki uslov.  Resenje: vise jednostavnih iskaza uokvirite viticastim zagradama ( {} ).

Primer 2:

<?php

$minuti = 60;
if($minuti > 59) {
echo "Vase vreme je isteklo.<br>";
echo "Molim vas, spustite olovke na sto.";
$test_je_zavrsen = TRUE;
}

?>

Rezultat: 

Vase vreme je isteklo.
Molim vas, sprustite olovke na sto.

U PHP-u postoje funkcije koje vracaju tip promenljive, neke od njih su: is_int, is_float, is_array.

Primer 3:

<?php

if (is_int($promenljiva))
$promenljiva = $promenljiva + 10;

?>

if (expression)
statement

Povezani clanci:
Koriscenje iskaza else
Koriscenje iskaza elseif

The expression is an expression whose value can be estimated on the true or false. For example: if 7 > 3, it returns true. If the expression is true, the statement is executed. If the expression is false, the statement is not executed.

Using the if statement, in the script, you can make decisions in the moment of execution. These decisions are based on the current values of data, such as, for example:
  • text that user has entered on the web page for example. password
  • values that are returned from a database for example, the available supplies
  • data that are downloaded from the other website for example. prices
Example:

<?php

$motorcycle_displacement = 150;
if ($motorcycle_displacement < 200)
echo "Nice motorcycle.";

?>

Save this file as a if-statement.php.

Result: Nice motorcycle.

Explanation:

In this statement, the variable $motorcycle_displacement has value 150, so the echo statement will be executed.

If you have several statements and you want to execute them if some condition is met. The solution: more simple expressions frame with the curly braces ( {} ).

Example 2:

<?php

$minutes = 60;
if($minutes > 59) {
echo "Your time is up.<br>";
echo "Please, put your pencils on the table.";
$test_is_over = TRUE;
}

?>

Result:

Your time is up.
Please, put your pencils on the table.

In PHP there are functions that can return a variable type, some of them are: is_int, is_float, is_array.

Example 3:

<?php

if (is_int($variable))
$variable = $variable + 10;


No comments:

Post a Comment

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