LoginSignup
2
2

More than 1 year has passed since last update.

関数の呼び出し(PHP)

Last updated at Posted at 2022-12-31

関数の呼び出しを使ったサンプルプログラムです

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>関数テスト</title>
  <style>
      .a {
        font-size:18px;
        color:#2F7;
      }
      .b {
        font-weight: bold;
        color:#0725BA;
      }
  </style>  
</head>
<body>
  <?php
    //名前を出力する関数
    function show($name){
        echo "<p class='a'><span class='b'>${name}</span>は乃木坂46のメンバーです</p>";
    }
    //名前配列
    $array = ["秋元真夏","遠藤さくら","賀喜遥香"];
    // ループ文で名前を引数にして関数を呼び出す
    foreach($array as $line){
      show($line);
    }

  ?>

</body>
</html>

ポイントは

function show($name){
        echo "<p class='a'><span class='b'>${name}</span>は乃木坂46のメンバーです</p>";
    }

関数にして処理をまとめます

$array = ["秋元真夏","遠藤さくら","賀喜遥香"];

    foreach($array as $line){
      show($line);
    }

引数を使って関数を呼び出して処理します。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2