2014. szeptember 11., csütörtök

PHP beépített abs függvény használata

pl:
<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 6.1 Calling the Built-in abs() function</title>
</head>
<body>
<div>
<?php
$num = -321;
$newnum = abs( $num );
print $newnum;
// prints "321"
?>
</div>
</body>
</html>
Eredmény:

321

HTML használata 1 kód-blokk belsejében

Vagyis a PHP vége, kezdete jelzés közé írhatunk HTML kódot.
pl:
<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 5.14 Returning to HTML Mode Within a Code Block</title>
</head>
<body>
<div>
<?php
$display_prices = true;

if ( $display_prices ) {
?>
    <table border="1">
    <tr><td colspan="3">todays prices in dollars</td></tr><tr>
    <td>14</td><td>32</td><td>71</td>
    </tr></table>
<?php
}

?>
</div>
</body>
</html>
Eredmény:

todays prices in dollars
143271

Egy kód-blokk tartalmazhat több kiírást - formázáshoz

pl:
<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 5.13 A Code Block Containing Multiple print() statements</title>
</head>
<body>
<div>
<?php
$display_prices = true;

if ( $display_prices ) {
    print "<table border=\"1\">";
    print "<tr><td colspan=\"3\">";
    print "todays prices in dollars";
    print "</td></tr><tr>";
    print "<td>14</td><td>32</td><td>71</td>";
    print "</tr></table>";
}

?>
</div>
</body>
</html>
Eredmény:

todays prices in dollars
143271

Egymásba ágyazott ciklusok

A külső ciklus minden számlálása között lefut 1 teljes belső ciklus.
Pl:
<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 5.12 Nesting Two for Loops</title>
</head>
<body>
<div>
<?php
print "<table border=\"0\">\n";
for ( $y=1; $y<=12; $y++ ) {
    print "<tr>\n";
    for ( $x=1; $x<=12; $x++ ) {
        print "\t<td>";
        print ($x*$y);
        print "</td>\n";
    }
    print "</tr>\n";
}
print "</table>";
?>
</div>
</body>
</html>
Eredmény:

1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8 10 12 14 16 18 20 22 24
3 6 9 12 15 18 21 24 27 30 33 36
4 8 12 16 20 24 28 32 36 40 44 48
5 10 15 20 25 30 35 40 45 50 55 60
6 12 18 24 30 36 42 48 54 60 66 72
7 14 21 28 35 42 49 56 63 70 77 84
8 16 24 32 40 48 56 64 72 80 88 96
9 18 27 36 45 54 63 72 81 90 99 108
10 20 30 40 50 60 70 80 90 100 110 120
11 22 33 44 55 66 77 88 99 110 121 132
12 24 36 48 60 72 84 96 108 120 132 144

continue: folytatás a következő ciklustól

pl:
<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 5.11 Using the continue Statement</title>
</head>
<body>
<div>
<?php
$counter = -4;
for ( ;  $counter <= 10; $counter++ ) {
    if ( $counter == 0 ) {
        continue;
    }
    $temp = 4000/$counter;
    print "4000 divided by $counter is... $temp<br />";
}
?>
</div>
</body>
</html>
Eredmény:

4000 divided by -4 is... -1000
4000 divided by -3 is... -1333.3333333333
4000 divided by -2 is... -2000
4000 divided by -1 is... -4000
4000 divided by 1 is... 4000
4000 divided by 2 is... 2000
4000 divided by 3 is... 1333.3333333333
4000 divided by 4 is... 1000
4000 divided by 5 is... 800
4000 divided by 6 is... 666.66666666667
4000 divided by 7 is... 571.42857142857
4000 divided by 8 is... 500
4000 divided by 9 is... 444.44444444444
4000 divided by 10 is... 400

break: kiugrás a ciklusból

pl:
<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 5.10 Using the break Statement</title>
</head>
<body>
<div>
<?php
$counter = -4;
for ( ; $counter <= 10; $counter++ ) {
    if ( $counter == 0 ) {
        break;
    }
    $temp = 4000/$counter;
    print "4000 divided by $counter is... $temp<br />";
}
?>
</div>
</body>
</html>
Eredmény:

4000 divided by -4 is... -1000
4000 divided by -3 is... -1333.3333333333
4000 divided by -2 is... -2000
4000 divided by -1 is... -4000

A for ciklus

Formája:
for ( kezdőérték; feltétel; számolási parancs) {
   }

pl:
<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 5.8 Using the for Statement</title>
</head>
<body>
<div>
<?php
for ( $counter=1; $counter<=12; $counter++ ) {
    print "$counter times 2 is ".($counter*2)."<br />";
}
?>
</div>
</body>
</html>
Eredmény:
1 times 2 is 2
2 times 2 is 4
3 times 2 is 6
4 times 2 is 8
5 times 2 is 10
6 times 2 is 12
7 times 2 is 14
8 times 2 is 16
9 times 2 is 18
10 times 2 is 20
11 times 2 is 22
12 times 2 is 24