Help us understand the problem. What is going on with this article?

【初心者向け】関数について

意味      
関数 一つの処理をまとめたもの
引数 関数に渡せる値
返り値 関数から戻ってくる値
初期値 引数がない時の値

関数とは

処理をひとまとめにすることで使いまわせる
似たような処理をまとめられる

//定義
function sayHello() {

//処理
  echo "Hello!";
}

//呼び出し
sayHello();// Hello!
sayHello();// Hello! 何度も呼び出し可能

引数とは

関数に渡す値

//渡された引数が$nameに入る
function sayHello($name) {

//$nameは関数の中で使用できる
  echo "Hello!" . $name;
}

//関数に引数を渡す
sayHello("Tom");// Hello!Tom
sayHello("Bob");// Hello!Bob


//引数は複数指定できる
function sayHello($name,$ago) {

  echo "Hello!" . $name . $ago;
}

sayHello("Tom",19);// Hello!Tom19
sayHello("Bob",20);// Hello!Bob20

返り値

処理を終了させ、関数から戻ってくる値

function sayHello($name) {

//値を返す
  return "Hello!" . $name;

  echo "ここは処理をされない。";
}

$test = "jon";
//sayHello関数の呼び出し
echo sayHello($test);// Hello!jon

初期値

引数を指定しなった時の値

//初期値指定
function sayHello($name = "Jon") {

  echo "Hello!" . $name;
}

sayHello("TOM");// Hello!Tom
sayHello("Bob");// Hello!Bob
sayHello();     // Hello!Jon 初期値
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした