PHP sintakse

Download Report

Transcript PHP sintakse

Web tehnoloģiju apskats

<html> <head><title>Hello
World</title></head> <body><h1>Hello
World!</h1></body> </html>

<html> <head><title>Hello
World</title></head> <body><h1>Hello
World!</h1> <p>Datums: <? echo
date("d.m.Y"); ?> </body> </html>
<?
$a = 'test‘;
$copyOf_a = $a;
$Number100 = 100;
echo $a;
echo $copyOf_a;
echo $Number100;
?>
<?
$greeting = ‘Sveiks‘;
$name = ‘Jānis‘;
$message = "$greeting, $name!“;
echo $message;
?>

\r – Carriage return (CR)
\n – Jauna līnija (NL)
\" - pēdiņas
\$ - dolāra simbols ($)
\\ - slipsvītra (\)

<? echo '$greeting, $name!'; ?>




<?
$greeting = ‘Sveiks‘;
$name = ‘Jānis‘;
$message = $greeting . ',' . $name. '!‘;
echo $message;
?>








==
!=
<
>
<=
>=
===
!==
if(nosacījums)
{
// ...
}
if(nosacījums)
{
// ...
}
else
{
//...
}
if(nosacījums)
{
// ...
}
elseif
{
//...
}
else
{
//...
}
switch (mainīgais)
{
case 1: vērtība
// ...
break;
case 2: vērtība
// ...
break;
default:
// ...
}






while
do .. while
for
foreach
break
continue
<?
$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“;
?>
<?
$operating_systems = array( 'Windows', 'Linux',
'FreeBSD', 'OS/2');
$operating_systems[] = 'MS-DOS‘;
echo "<pre>“;
print_r($operating_systems);
echo "</pre>“;
?>
<?
$languages = array( 1 => 'Assembler', 'C++',
'Pascal', 'scripting' => 'bash' );
$languages['php'] = 'PHP‘;
$languages[100] = 'Java‘;
$languages[] = 'Perl‘;
echo "<pre>“;
print_r($languages);
echo "</pre>";
?>
<?
$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>
<?
$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“;
?>
<?
$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“;
}
?>
<?
define ('MY_NAME', ‘Jānis');
echo ‘Mani sauc ' . MY_NAME;
?>
PHP sinakse

$a + $b
$a - $b
$a * $b
$a / $b (sprintf, number_format)
$a % $b

+= -+ *= /= %=






$c = $a.$b;
$c .= $e;






$a & $b —AND
$a | $b — OR
$a ^ $b — XOR
~ $a — NOT
$a << $b — nobīde
$a >> $b — nobīde






$a and $b // AND
$a && $b // AND
$a or $b // OR
$a || $b // OR
$a xor $b // XOR
!$a // NOT

$res= (expr1) ? (expr2) : (expr3);




++$a
$a++
--$a
$a--





integer
float / double
string
array
object





(int), (integer) - cast to integer
(real), (double), (float) - cast to double
(string) - cast to string
(array) - cast to array
(object) - cast to object






gettype()
is_long()
is_double()
is_string()
is_array()
is_object().
$a = 1; /* globālais */
function Test ()
{
$a=2;
echo $a; /* lokalais */
}
Test ();
echo $a;
$a = 1; /* globālais */
function Test ()
{
global $a;
$a=2;
echo $a; /* lokalais */
}
Test ();
echo $a;
$a = 1; /* globālais */
function Test ()
{
$GLOBALS[‘a’]=2;
echo $a; /* lokalais */
}
Test ();
echo $a;
function Test()
{
static $a = 0;
echo $a;
$a++;
}
<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>
<?=$_SERVER['PHP_SELF']?>
<? echo $_SERVER['PHP_SELF']; ?>.
<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>
<html>
<body>
<?
$name = isset($_POST['name']) ? $_POST['name'] : '';
$year = isset($_POST['year']) ? $_POST['year'] : '';
if (isset($_POST['name'], $_POST['year'])) {
if ($_POST['name'] == '') {
echo 'Ievadiet vā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 'Jums ir ' . $age . ' gads(i)<br>';
}
echo '<hr>';
}
?>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
Jūsu vārds: <input type="text" name="name" value="<?=$name?>">
<br>
Jūsu dzimšanas gads: <input type="text" name="year" value="<?=$year?>">
<input type="submit" value="OK">
</form>
</body>
</html>

htmlspecialchars()

http://phpwiki.ru/PHP_Wiki