LoginSignup
0
1

More than 3 years have passed since last update.

【php】関数の作成【function】

Last updated at Posted at 2019-08-19

関数の作成

重複する処理を一箇所にまとめることで、コードに変更があった時に関数の中身を変更するだけ良くなる為便利。

円の面積を求める

$radius1 = 3;
echo $radius1 * $radius1 * 3;

$radius2 = 5;
echo $radius2 * $radius2 * 3;

//上記は同じ処理を実行している。

//関数の定義
function printCircleArea($radius){
  echo $radius * $radius * 3;
}
//関数の呼び出し
printCircleArea(3); //結果: 27
printCircleArea(5); //結果: 75

関数の記述方法

関数を作るには「function 関数名(){処理}」の形式で記述する。関数名は自由。呼び出しは「関数名()」。

//関数の定義
function hello(){
 echo 'Hello, world!';
}

//関数の呼び出し
hello();
//結果: Hello, world!

引数有り

//関数の定義
function printSum($num1,$num2){
  echo $num1 + $num2;
}

//関数の呼び出し
printSum(8,14);
//結果: 22;
0
1
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
0
1