<?php
function printMessage($from, $to)
{
echo "Hi {$to}! from { $from}" . PHP_EOL;
}
printMessage(from: "Taro", to: "Jiro");
printMessage(to: "Shiro", from: "Saburo");
カテゴリー: PHP
PHP 複数のreturn
<?php
function getTotal($price, $amount, $rate = 1.1)
{
if($amount >= 100){
return $price * $amount;
}else{
return $price * $amount * $rate;
}
}
echo getTotal(100, 100) . PHP_EOL;
echo getTotal(1000, 10) . PHP_EOL;
PHP 関数 引数のデフォルト値
<?php
function getTotal($price, $rate = 1.1, $amount)
{
return $price * $amount * $rate;
}
echo getTotal(100, 10) . PHP_EOL;
echo getTotal(150, 20) . PHP_EOL;
echo getTotal(200, 30) . PHP_EOL;
echo getTotal(120, 40, 1.08) . PHP_EOL;
PHP 関数 return NULL
<?php
function sum($a, $b)
{
return $a + $b;
}
function printSum($a, $b)
{
echo $a + $b . PHP_EOL;
//return NULL;
}
//echo sum(3, 7) . PHP_EOL;
//var_dump(printSum(3, 7));
echo sum(3, 7) * 3 . PHP_EOL;
printSum(3, 7) * 3;
PHP 関数
<?php
function sum($a, $b)
{
return $a + $b;
}
function printSum($a, $b)
{
echo $a + $b . PHP_EOL;
}
echo sum(3,7) . PHP_EOL;
printSum(3,7);
PHP Todo-list
<?php
// タスクを保存するファイル
$tasks_file = 'tasks.txt';
// タスクを追加
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['new_task'])) {
$new_task = trim($_POST['new_task']);
if (!empty($new_task)) {
$task_entry = $new_task . '|0'; // 0は未完了ステータスを示す
file_put_contents($tasks_file, $task_entry . PHP_EOL, FILE_APPEND);
}
}
// タスクの完了ステータスを変更
if (isset($_GET['toggle'])) {
$tasks = file($tasks_file, FILE_IGNORE_NEW_LINES);
$index = $_GET['toggle'];
if (isset($tasks[$index])) {
$task_data = explode('|', $tasks[$index]);
$task_data[1] = $task_data[1] == '0' ? '1' : '0'; // ステータスを切り替える
$tasks[$index] = implode('|', $task_data);
file_put_contents($tasks_file, implode(PHP_EOL, $tasks) . PHP_EOL);
}
}
// タスクを削除
if (isset($_GET['remove'])) {
$tasks = file($tasks_file, FILE_IGNORE_NEW_LINES);
unset($tasks[$_GET['remove']]);
file_put_contents($tasks_file, implode(PHP_EOL, $tasks) . PHP_EOL);
}
// 現在のタスクを取得
$tasks = file_exists($tasks_file) ? file($tasks_file, FILE_IGNORE_NEW_LINES) : [];
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>TODOリスト</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}
h1 {
text-align: center;
}
form {
display: flex;
justify-content: center;
margin-bottom: 20px;
}
input[type="text"] {
width: 300px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
padding: 10px 20px;
border: none;
background-color: #5cb85c;
color: white;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #4cae4c;
}
ul {
list-style-type: none;
padding: 0;
}
li {
background-color: #fff;
margin: 5px 0;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
display: flex;
justify-content: space-between;
align-items: center;
}
.completed {
text-decoration: line-through;
color: gray;
}
a {
text-decoration: none;
color: #d9534f;
margin-left: 10px;
}
a:hover {
color: #c9302c;
}
</style>
</head>
<body>
<h1>TODOリスト</h1>
<!-- タスクを追加するフォーム -->
<form method="post" action="">
<input type="text" name="new_task" placeholder="新しいタスクを入力" required>
<button type="submit">追加</button>
</form>
<!-- タスクを表示 -->
<ul>
<?php foreach ($tasks as $index => $task): ?>
<?php list($task_text, $status) = explode('|', $task); ?>
<li>
<span class="<?php echo $status == '1' ? 'completed' : ''; ?>">
<?php echo htmlspecialchars($task_text); ?>
</span>
<div>
<a href="?toggle=<?php echo $index; ?>">
<?php echo $status == '0' ? '完了にする' : '未完了に戻す'; ?>
</a>
<a href="?remove=<?php echo $index; ?>">削除</a>
</div>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
PHP else if
<?php
echo "Score? ";
$score = (int) fgets(STDIN);
if ($score >= 90) {
echo "A" . PHP_EOL;
} elseif($score >= 95){
echo "Super!" . PHP_EOL;
} else{
echo "C" . PHP_EOL;
}
PHP if-else 条件分岐
<?php
echo "Score?";
$score = (int) fgets(STDIN);
if($score >= 90){
echo "A" . PHP_EOL;
}else{
echo "Not A" . PHP_EOL;
}
PHP 変数
<?php
// echo 150 * 120 . PHP_EOL;
// echo 150 * 130 . PHP_EOL;
// echo 150 * 140 . PHP_EOL;
$price = 150;
echo $price * 120 . PHP_EOL;
echo $price * 130 . PHP_EOL;
echo $price * 140 . PHP_EOL;
PHP 数値
<?php
echo 10 + 3 . PHP_EOL;
echo 10 - 3 . PHP_EOL;
echo 10 * 3 . PHP_EOL;
echo 10 ** 3 . PHP_EOL;
echo 10 / 3 . PHP_EOL;
echo 10 % 3 . PHP_EOL;
echo 10 + 2 * 3 . PHP_EOL;
echo (10 + 2) * 3 . PHP_EOL;