1 / 41

PHP sintakse

Web tehnoloģiju apskats. PHP sintakse. PHP skripts. <html> <head><title>Hello World</title></head> <body><h1>Hello World!</h1></body> </html>. PHP tegi. <html> <head><title>Hello World</title></head> <body><h1>Hello World!</h1> < p> Datums : <? echo date(" d.m.Y "); ?> </body> </html>.

urbano
Download Presentation

PHP sintakse

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Web tehnoloģiju apskats PHP sintakse

  2. PHP skripts • <html> <head><title>Hello World</title></head> <body><h1>Hello World!</h1></body> </html>

  3. PHP tegi • <html> <head><title>Hello World</title></head> <body><h1>Hello World!</h1> <p>Datums: <? echo date("d.m.Y"); ?> </body> </html>

  4. Mainīgie <? $a = 'test‘; $copyOf_a = $a; $Number100 = 100; echo $a; echo $copyOf_a; echo $Number100; ?>

  5. Rindas <? $greeting = ‘Sveiks‘; $name = ‘Jānis‘; $message = "$greeting, $name!“; echo $message; ?>

  6. Kombinācijas • \r– Carriage return(CR) • \n– Jauna līnija(NL) • \" - pēdiņas • \$ - dolāra simbols($) • \\ - slipsvītra(\) • <? echo '$greeting, $name!'; ?>

  7. Apostrofi <? $greeting = ‘Sveiks‘; $name = ‘Jānis‘; $message = $greeting . ',' . $name. '!‘; echo $message; ?>

  8. Nosacījumi • == • != • < • > • <= • >= • === • !==

  9. if if(nosacījums) { // ... }

  10. If...else if(nosacījums) { // ... } else { //... }

  11. If...elseif...else if(nosacījums) { // ... } elseif { //... } else { //... }

  12. switch switch (mainīgais) { case 1: vērtība // ... break; case 2: vērtība // ... break; default: // ... }

  13. Cikli • while • do .. while • for • foreach • break • continue

  14. Masīvi <? $i = 1024; $a[1] = 'abc‘; $a[2] = 100; $a['test'] = $i - $a[2]; echo $a[1] . "<br>\n“; echo $a[2] . "<br>\n“; echo $a['test'] . "<br>\n“; ?>

  15. print_r <? $operating_systems = array( 'Windows', 'Linux', 'FreeBSD', 'OS/2'); $operating_systems[] = 'MS-DOS‘; echo "<pre>“; print_r($operating_systems); echo "</pre>“; ?>

  16. Asociatīvie masīvi <? $languages = array( 1 => 'Assembler', 'C++', 'Pascal', 'scripting' => 'bash' ); $languages['php'] = 'PHP‘; $languages[100] = 'Java‘; $languages[] = 'Perl‘; echo "<pre>“; print_r($languages); echo "</pre>"; ?>

  17. Cikls foreach priekš a. masīviem <? $languages = array( 1 => 'Assembler', 'C++', 'Pascal', 'scripting' => 'bash' ); $languages['php'] = 'PHP‘; $languages[100] = 'Java'; $languages[] = 'Perl'; ?> <table> <tr> <th>Indeks</th> <th>Vērtība</th> </tr> <? foreach($languages as $key => $value) // foreach ($languages as $value) { echo '<tr><td>' . $key . '</td><td>' . $value . '</td></tr>‘; } ?> </table>

  18. Konstrukcijas list uneach <? $browsers = array( 'MSIE' => 'Microsoft Internet Explorer 6.0', 'Gecko' => 'Mozilla Firefox 0.9', 'Opera' => 'Opera 7.50' ); list($a, $b) = each($browsers); list($c, $d) = each($browsers); list($e, $f) = each($browsers); echo $a.':'.$b."<br>\n“; echo $c.':'.$d."<br>\n“; echo $e.':'.$f."<br>\n“; ?>

  19. Konstrukcijas list uneach ar ciklu <? $browsers = array( 'MSIE' => 'Microsoft Internet Explorer 6.0', 'Gecko' => 'Mozilla Firefox 0.9', 'Opera' => 'Opera 7.50' ); while (list($key,$value)=each($browsers)) { echo $key . ':' . $value . "<br>\n“; } ?>

  20. Konstantes <? define ('MY_NAME', ‘Jānis'); echo ‘Mani sauc' . MY_NAME; ?>

  21. Izteiksmes PHP sinakse

  22. Aritmētiskas operācijas • $a + $b • $a - $b • $a * $b • $a / $b (sprintf, number_format) • $a % $b • += -+ *= /= %=

  23. Rindas operācijs • $c = $a.$b; • $c .= $e;

  24. Bināras operācijas • $a & $b —AND • $a | $b — OR • $a ^ $b — XOR • ~ $a — NOT • $a << $b — nobīde • $a >> $b — nobīde

  25. Loģiskas operācijas • $a and $b // AND • $a && $b // AND • $a or $b // OR • $a || $b // OR • $a xor $b // XOR •  !$a // NOT

  26. Trenāraisoperātors • $res= (expr1) ? (expr2) : (expr3);

  27. Increment un decremet • ++$a • $a++ • --$a • $a--

  28. Datu tipi • integer • float / double • string • array • object

  29. Automātiska konvertācija • (int), (integer) - cast to integer • (real), (double), (float) - cast to double • (string) - cast to string • (array) - cast to array • (object) - cast to object

  30. Tipa noteikšana • gettype() • is_long() • is_double() • is_string() • is_array() • is_object().

  31. Mainīgu darbības apgabals $a = 1; /* globālais */ function Test () { $a=2; echo $a; /* lokalais */ } Test (); echo $a;

  32. global $a = 1; /* globālais */ function Test () { global $a; $a=2; echo $a; /* lokalais */ } Test (); echo $a;

  33. $GLOBAL $a = 1; /* globālais */ function Test () { $GLOBALS[‘a’]=2; echo $a; /* lokalais */ } Test (); echo $a;

  34. Statiskie mainīgie function Test() { static $a = 0; echo $a; $a++; }

  35. Formas

  36. $_GET, $_POST <html> <body> <? if ($_SERVER['REQUEST_METHOD'] == 'POST‘) { echo '<h1>Sveiks, <b>' . $_POST['name'] . '</b></h1>!‘; } ?> <form method="POST" action="<?=$_SERVER['PHP_SELF']?>“> Jūsu vārds:<input type="text" name="name"> <br> <input type="submit" name="okbutton" value="OK“> </form> </body> </html>

  37. Short tags <?=$_SERVER['PHP_SELF']?> <? echo $_SERVER['PHP_SELF']; ?>.

  38. Validācija <html> <body> <? if (isset($_POST['name'], $_POST['year'])) { if ($_POST['name'] == '‘) { echo ‘Ievadiet vārdu!<br>‘; } elseif($_POST['year'] < 1900 || $_POST['year'] > 2004) { echo ‘Ievadiet dzimšanas gadu! Diapazons:1900..2008<br>‘; } else { echo ‘Labdien, ' . $_POST['name'] . '!<br>‘; $age = 2008- $_POST['year']; echo ‘Jums ir' . $age . ' gads(i)<br>‘; } echo '<hr>‘; } ?> <form method="post" action="<?=$_SERVER['PHP_SELF']?>"> Jūsu vārds:<input type="text" name="name"> <br> Jūsu dzimšanas gads: <input type="text" name="year"> <input type="submit" value="OK"> </form> </body> </html>

  39. Ievadīto datu saglabāšana <html> <body> <? $name = isset($_POST['name']) ? $_POST['name'] : ''; $year = isset($_POST['year']) ? $_POST['year'] : ''; if (isset($_POST['name'], $_POST['year'])) { if ($_POST['name'] == '') { echo 'Ievadietvārdu!<br>'; } else if ($_POST['year'] < 1900 || $_POST['year'] > 2008){ echo 'Ievadiet dzimšanas gadu! Diapazons: 1900..2008<br>'; } else { echo 'Labdien, ' . $_POST['name'] . '!<br>'; $age = 2008 - $_POST['year']; echo 'Jumsir ' . $age . ' gads(i)<br>'; } echo '<hr>'; } ?> <form method="post" action="<?=$_SERVER['PHP_SELF']?>"> Jūsuvārds: <input type="text" name="name" value="<?=$name?>"> <br> Jūsudzimšanas gads: <input type="text" name="year" value="<?=$year?>"> <input type="submit" value="OK"> </form> </body> </html>

  40. Ievaddatu validācija • htmlspecialchars()

  41. Literatūra • http://phpwiki.ru/PHP_Wiki

More Related