PHP基礎

PHP基礎


PHP

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>ようこそ!</title>
</head>

<body>
<?php
print "ようこそPHPへ!<br>\n";
print "PHPを始めましょう!";
?>
</body>
</html>


PHP

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>変数を使う</title>
</head>

<body>
<?php
$product = "鉛筆";
print "$product";
print "を販売しています。\n";
?>
</body>
</html>


PHP

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>変数の値を変更する</title>
</head>

<body>
<?php
$product = "鉛筆";
print "$product";
print "を販売しています。<br>\n";

$product = "消しゴム";
print "$product";
print "を販売しています。\n";
?>
</body>
</html>


PHP

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>文字列に変数を埋め込む</title></title>
</head>

<body>
<?php
$product = "消しゴム";
print "{$product}を販売しています。\n";
?>
</body>
</html>


PHP

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>複数の変数</title></title>
</head>

<body>
<?php
$product = "鉛筆";
$num = 10;

print "{$product}を{$num}個販売しています。\n";	
?>
</body>
</html>

演算子の基本

式の評価

PHP

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>式の値を出力する</title></title>
</head>

<body>
<?php
$product = "鉛筆";

print "{$product}を";
print 1+2;
print "個販売しています。\n";
?>
</body>
</html>

変数を演算する


PHP

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>変数を演算する</title></title>
<style type="text/css">
<!--

table {
	width: 300px;
	border-collapse: collapse;
}
	
th {
	background-color: #CCC;
}

th, td {
	padding: 6px;
}

-->
</style>
</head>

<body>
<?php
$product = "消しゴム";
$num = 10;
$price = 100;

$total = $num*$price; /* $numと$priceを掛け合わせ
ものを$totalに代入 */
$total = $total-100; /* $totalから100減じたものを
$totalに代入*/
?>

<table border="1" >
<tr><th>内容</th><th>金額</th></tr>
<?php
print "<tr><td>品名</td><td>{$product}</td></tr>\n";
print "<tr><td>単価</td><td>{$price}円</td><tr>\n";
print "<tr><td>個数</td><td>{$num}個</td></tr>\n";
print "<tr><td>計</td><td>{$total}円(但100円引)</td>
</tr>\n";

?>
</table>

</body>
</html>

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>色々な演算子を利用する</title></title></title>
<style type="text/css">
<!--

table {
	width: 300px;
	border-collapse: collapse;
}
	
th {
	background-color: #CCC;
}

th, td {
	padding: 6px;
}

-->
</style>
</head>
<body>
<?php
$num1 = 10;
$num2 = 5;
$num3 = $num1 + $num2;
$num4 = $num1 - $num2;
$num5 = $num1 * $num2;
$num6 = $num1 / $num2; 
$num7 = $num1 % $num2;	
?>

<table border="2">
<tr><th>項目</th><th>結果</th></tr>
<?php
print "<tr><td>\$num1</td><td>{$num1}</td></tr>\n";
print "<tr><td>\$num2</td><td>{$num2}</td></tr>\n";
print "<tr><td>\$num1+\$num2</td><td>{$num3}</td></tr>\n";
print "<tr><td>\$num1-\$num2</td><td>{$num4}</td></tr>\n";
print "<tr><td>\$num1*\$num2</td><td>{$num5}</td></tr>\n";
print "<tr><td>\$num1/\$num2</td><td>{$num6}</td></tr>\n";
print "<tr><td>\$num1%\$num2</td><td>{$num7}</td></tr>\n";
?>
</table>
</body>
</html>