<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PHPの練習</title>
<style>
blockquote {border: solid 1px black}
pre {margin:5px; font-size:small}
</style>
</head>
<body>
<h2>日時</h2>
<?php echo date("Y/m/d H:m:s"); ?>
<?php
print "\n<h2>arrary</h2>\n";
$ary = array("小杉", "中原", "新城", "溝口");
print $ary[1];
print "\n<h2>explode</h2>\n";
$ary = explode(",", "小杉,中原,新城,溝口");
print $ary[2];
print "\n<h2>連想配列</h2>\n";
$ary = array("cat" => "猫", "dog" => "犬", "beef" => "牛肉");
print "<table border=\"1\">";
while(list ($key, $val) = each($ary)) {
print "<tr><td>$key</td><td>$val</td></tr>\n";
}
print "</table>";
foreach($ary as $key => $val) {
print "($key,$val)";
}
print "\n<h2>置換</h2>\n";
$str = "これはなんといいますか";
print "『".$str."』→『".(str_replace("いい","(・∀・)イイ!",$str))."』";
print "\n<h2>条件分岐</h2>\n";
switch(date("s")) {
case "00"; case "20"; case "30"; case "40"; case "50";
print "あたり";
break;
default:
print "はずれ";
}
print "\n<h2>ユーザ定義関数</h2>\n";
function strong($str) {
return "<em>$str</em>";
}
print "これは".strong("ペン")."です";
?>
<h2>クッキー</h2>
<?php
if (isset($_COOKIE["php4649testdate"])) {
print "前回来たのは {$_COOKIE["php4649testdate"]} みたいですよ。";
} else {
print "クッキーを設定しておきますね。";
}
setcookie("php4649testdate", date("Y/m/d H:m:s"));
?>
<h2>セッション</h2>
<?php
session_set_cookie_params(0);
session_start();
if (! isset($_SESSION['step']) || $_SESSION['step'] == 0) {
print "セッションスタートです。ブラウザを閉じるまで続きます。";
$_SESSION['step'] = 0;
} else if ($_SESSION['step'] >= 3) {
print "手続き終了です。リロードすると再スタートです。";
$_SESSION['step'] = -1;
}
$_SESSION['step']++;
print "<ul>";
for ($i = 1; $i <= 3; $i ++) {
print "<li>Step$i";
if ($_SESSION['step'] == $i) {
print " ←今ここです(リロードすると進みます)";
}
}
print "</ul>";
?>
session name = <?php echo session_name(); ?>,
session ID = <?php echo htmlspecialchars(session_id()); ?>
<h2>フォーム</h2>
<form method="post">
<textarea name="message" rows="3" cols="40"></textarea><br>
<input type="submit"></form>
<?php
if ($_POST['message'] != "") {
print "<blockquote><pre>".htmlspecialchars($_POST['message']).
"</pre></blockquote>";
}
?>
<h2>ファイル</h2>
ソースです。
<?php
$lines = file("php-test.php");
foreach ($lines as $line_num => $line) {
$all .= htmlspecialchars($line);
}
print "<blockquote><pre>$all</pre></blockquote>";
?>
</body>
</html>