ホームページ  >  記事  >  バックエンド開発  >  PHPの関数

PHPの関数

王林
王林オリジナル
2024-08-29 12:46:08365ブラウズ

PHPでは組み込み関数やユーザー定義関数など多くの関数が使用されます。それぞれの関数には独自の機能とプロパティがあります。関数は、プログラム内で記述された一連のステートメントであり、コード内の必要な場所で複数回使用できます。関数内に記述されたステートメントを実行するには、関数呼び出しが必要です。これは、1 つ以上の入力をパラメータとして受け取り、それを処理して値を返すコードの一部です。プログラマは関数を作成し、必要に応じてプログラム内でその関数を呼び出すだけです。

広告 このカテゴリーの人気コース PHP 開発者 - 専門分野 | 8コースシリーズ | 3 つの模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

PHP の関数の種類

PHP では、プログラマは主に 2 つの関数を使用します。それらは次のとおりです:

1.ユーザー定義

これらの関数は、開発者またはプログラマが独自のコード ロジックを実行する必要がある場合に使用されます。これらの関数は、キーワード function を使用して定義され、関数内には、関数呼び出しが発生したときにそれを実行するための一連のステートメントが記述されます。関数呼び出しは、functionname() のような関数を呼び出すだけで行うことができ、関数が実行されます。

2.内蔵

これらの関数は、組み込みライブラリ関数を提供します。 PHP は、インストール パッケージ自体でこれらの関数を提供し、この言語をより強力で便利なものにしています。関数のプロパティを使用するには、目的の結果を取得するために必要な場所で関数を呼び出すだけです。

PHP には、日付、数値、文字列など、多くの組み込み関数が使用されます。

  • 文字列関数: これらの関数には、文字列を操作するための PHP の事前定義された機能があります。 PHP には、strpos()、strncmp()、strrev()、strlen()、
  • などのさまざまな文字列関数があります。
  • 日付関数: これらの関数は PHP で事前定義された機能で、その形式は人間が判読できる形式である UNIX 日付と時刻です。
  • 数値関数: これらの関数には、数値演算に使用される PHP によって提供される独自の事前定義ロジックがあります。結果はブール形式または数値形式で返されます。数値関数には、 is_number()、number_format()、round() などが含まれます。

なぜ PHP で関数を使用する必要があるのですか?

以下は、php で関数を使用する必要がある理由を説明するポイントです:

  • 再利用性: どのプログラミング言語でも、関数は複数回記述するコード行を減らすために使用されます。これにより、開発者やプログラマーの時間と労力が軽減されます。共通のコードを複数の領域で使用する必要がある場合は、それを関数に含めるだけで、必要なときにいつでもどこでも呼び出すことができます。これは、同じプログラム内で関数を呼び出すか、別のプログラムで使用される関数を呼び出すことで実現できます。
  • より簡単なエラー検出: コードは一括して記述されるのではなく、関数に分割または分割されるため、発生したエラーは簡単に検出でき、エラーを迅速かつ簡単に修正できます。
  • 保守が簡単: 関数はプログラム内で使用されるため、関数またはコード行を変更する必要がある場合、関数内で簡単に変更でき、変更が反映されます。したがって、どこでもメンテナンスが簡単です。

PHP で関数はどのように使用されますか?

前に説明したように、PHP には 2 つの関数 (組み込み関数とユーザー定義関数) があります。これらの関数についてさらに理解しましょう:

例 #1

文字列関数の場合

コード:

<!DOCTYPE html>
<html>
<body>
<?php
print_r(str_split("Hi This is a test sample"));
?>
</body>
</html>

出力:

PHPの関数

上記のプログラムの説明: 上の例では、関数 str_split() 内で渡した文字列が、文字列を 1 つの文字に分割して出力を生成します。

例 #2

コード:

<!DOCTYPE html>
<html>
<body>
<?php
echo strcmp("Hi this is test","Hi this is test");
?>
<p>If this function returns 0, the two strings are same.</p>
</body>
</html>

出力:

PHPの関数

上記のプログラムの説明: 上記の例では、関数 strcmp () は文字列を比較し、文字列が同じである場合はゼロを返し、文字列が等しくない場合はゼロを返します。他の番号を返します。

例 #3

コード:

<!DOCTYPE html>
<html>
<body>
<?php
echo strpos("I love coding, I love php too!","coding");
?>
</body>
</html>

出力:

PHPの関数

The explanation for the above program: This function strpos() will check the position of the string that is passed as a parameter.

Example #4

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo strrev("Hi world!");
?>
</body>
</html>

Output:

PHPの関数

The explanation for the above program: In the above example, the function strrev() will reverse the string passed as a parameter and provides the desired output.

Example #5

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo str_word_count("Hello this is the new world!");
?>
</body>
</html>

Output:

PHPの関数

The explanation for the above program: In the above example, the str_word_count() function will count the number of strings passed as a parameter and provides the desired output.

Example #6

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo strlen("Hello this is the test sample!");
?>
</body>
</html>

Output:

PHPの関数

The explanation for the above program: In the above example, the strlen() function will count the number of characters present in the string and provides the count as the desired output.

Example #1

For Numeric Functions

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo(abs(5.8) . "<br>");
echo(abs(-5.8) . "<br>");
echo(abs(-2) . "<br>");
echo(abs(3));
?>
</body>
</html>

Output:

PHPの関数

The explanation for the above program: In the above example, the numeric function abs() will provide us the absolute value of the number that is passed as a parameter to the function.

Example #2

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo(round(0.65) . "<br>");
echo(round(0.75) . "<br>");
echo(round(0.30) . "<br>");
?>
</body>
</html>

Output:

PHPの関数

Example #3

Code:

<!DOCTYPE html>
<html>
<body>
<?php
echo(sqrt(0) . "<br>");
echo(sqrt(7) . "<br>");
echo(sqrt(2) . "<br>");
echo(sqrt(0.45) . "<br>");
echo(sqrt(-3));
?>
</body>
</html>

Output:

PHPの関数

The explanation for the above program: In the above example, the parameters passed to the function sqrt() fetches the result by calculating the square root of the number and produces the desired output.

Example #4

Code:

<!DOCTYPE html>
<html>
<body>
<?php
// Check if the type of a variable is integer or not
$x = 456;
var_dump(is_int($x));
echo "<br>";
// Check whether the type of variable is integer or not
$x = 66.58;
var_dump(is_int($x));
?>
</body>
</html>

Output:

PHPの関数

The explanation for the above program: In the above example, the var_dump() function will check the data type of a particular number passed as a parameter. In the above screenshot, the output is printed as true or false in the condition that the number should be an integer. If the number is not an integer it will return false else true.

Example #5

Code:

<!DOCTYPE html>
<html>
<body>
<?php
// Invalid calculation will return a NaN value
$x = acos(10);
var_dump($x);
?>
</body>
</html>

Output:

PHPの関数

The explanation for the above program: In the above example, the function var_dump() will check the datatype of the number passed as a parameter. In this example, the function acos() cannot calculate the number specified as a parameter and hence produces the output NAN which means that the calculation is incorrect.

Example #6

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$x = 11.35;
var_dump(is_float($x));
?>
</body>
</html>

Output:

PHPの関数

The explanation for the above program: In the above example, the function is_float() will check whether the number passed as a parameter is of float datatype. This function always returns a Boolean value. If the result is positive then it will return true and if the result is negative it will return false.

Example #1

For User-defined functions

Code:

<!DOCTYPE html>
<html>
<body>
<?php
function Writefunction() {
echo "Hello world!";
}
Writefunction();
?>
</body>
</html>

Output:

PHPの関数

Example #2

Code:

<!DOCTYPE html>
<html>
<body>
<?php
function employee($ename) {
echo "$ename Patil.<br>";
}
employee("Akshay");
employee("Leela");
employee("Sharda");
employee("Subhadra");
employee("Akash");
?>
</body>
</html>

Output:

PHPの関数

Example #3

Code:

<!DOCTYPE html>
<html>
<body>
<?php
function Employee($ename, $id) {
echo "employee name is $ename. Employee id is $id <br>";
}
Employee("Heetal","778456");
Employee("Clark","567890");
Employee("Mohit","567894");
?>
</body>
</html>

Output:

PHPの関数

The explanation for the above program: In the above example, the employee names along with the employee id’s can be displayed by just calling the function employee () where the user wants to print the employee details. This user-defined functions can be used when the organization has a huge data and has to print all the employee details all together at a single go.

Example #4

Code:

<?php
function addNumbers(int $a, int $b) {
return $a + $b;
}
echo addNumbers(5, "13 days");
// since strict is NOT enabled "5 days" is changed to int(5), and it will return 10
?>

Output:

PHPの関数

上記のプログラムの説明: 上記の例では、ユーザー定義関数が独自のプロパティを持ち、ユーザーが独自の入力を指定して目的の出力を取得できることがわかりました。ユーザー定義関数は、プログラマまたは開発者が組み込み関数を使用するのではなく、コードに独自の変更を加えるために使用されます。この関数タイプを使用する主な目的は、開発者が円の面積の計算、身長の測定、従業員の詳細などの独自のロジックを作成できることです。PHP はデータ型が厳密な方法で設定されていない、緩やかに型付けされた言語です。 、整数と文字列のデータ型の値を追加して出力を取得できます。上の例では、整数と文字列「5 と 13」が加算され、出力は 18 としてフェッチされます。この機能はユーザーに利点をもたらします。

結論

この記事では、PHP の関数の種類とその特徴について説明しました。開発者とプログラマーは、コードを再度記述する必要がないため、これら 2 つの関数を使用してコードの開発を試みます。また、コードは、実行する必要があるタスクの種類に基づいて記述されるため、テストが簡単です。

以上がPHPの関数の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。