if else elseif/else if while do-while for foreach break continue switch declare return require include require_once include_once goto
if ($i == 1) { // do something }
ชื่อประโยค (expr): endชือประโยค;
if ($i == 1): // do something elseif ($i == 2): // do something else: // do something endif;
foreach ($items as $item): // do something endforeach;
while (($row = mysql_fetch_assoc($result))): // do something endwhile;
if else elseif for foreach while switch
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Test</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <?php if (isset($_SESSION['username'])) { ?> Welcome <?php echo htmlspecialchars($_SESSION['username']); ?> <?php } else { ?> Welcome guest <?php echo $_SESSION['counter']; ?> <?php } ?> <hr /> <?php foreach ($item_groups as $i => $group) { ?> <h3>Group <?php echo $i; ?></h3> <hr /> <?php foreach ($group as $id => $item) { ?> <div id="item<?php echo $id; ?>"> Item <?php echo htmlspecialchars($item['name']); ?> </div> <?php } ?> <?php } ?> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Test</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <?php if (isset($_SESSION['username'])): ?> Welcome <?php echo htmlspecialchars($_SESSION['username']); ?> <?php else: ?> Welcome guest <?php echo $_SESSION['counter']; ?> <?php endif; ?> <hr /> <?php foreach ($item_groups as $i => $group): ?> <h3>Group <?php echo $i; ?></h3> <hr /> <?php foreach ($group as $id => $item): ?> <div id="item<?php echo $id; ?>"> Item <?php echo htmlspecialchars($item['name']); ?> </div> <?php endforeach; ?> <?php endforeach; ?> </body> </html>