首页  >  文章  >  后端开发  >  PHP 中的函数

PHP 中的函数

王林
王林原创
2024-08-29 12:46:08366浏览

在 PHP 中,使用了许多函数,例如内置函数和用户定义函数。每个函数都有自己的功能和属性。函数是在程序中编写的一组语句,可以在代码中任何需要的地方多次使用。需要调用函数来执行函数内编写的语句。它是一段代码,将一个或多个输入作为参数并对其进行处理并返回一个值。程序员只需创建一个函数,然后在程序中需要的地方调用该函数。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

PHP 中的函数类型

在PHP中,程序员主要使用两个函数。他们是:

1.用户定义

当开发人员或程序员必须执行自己的代码逻辑时,将使用这些函数。这些函数是使用关键字 function 定义的,在函数内部,将编写一组语句以在发生函数调用时执行它。只需简单地调用函数(如 functionname())即可进行函数调用,并且该函数将被执行。

2.内置

这些函数为我们提供了内置的库函数。 PHP 在安装包本身中提供了这些功能,这使得该语言更加强大和有用。要使用该函数的属性,我们只需在需要时调用该函数即可获取所需的结果。

PHP 中使用了很多内置函数,例如 Date、Numeric、String 等

  • 字符串函数:这些函数在 PHP 中具有预定义的功能来处理字符串。 PHP 有多种字符串函数,例如 strpos()、strncmp()、strrev()、strlen()、
  • 日期函数:这些函数是 PHP 中的预定义功能,其格式是 UNIX 日期和时间,这是人类可读的格式。
  • 数值函数:这些函数有 PHP 提供的自己的预定义逻辑,用于数值运算。它将以布尔形式或数字形式返回结果。一些数字函数包括 is_number()、number_format()、round() 等

为什么我们应该在 PHP 中使用函数?

以下几点解释了为什么我们应该在 php 中使用函数:

  • 可重用性:在任何编程语言中,函数都用于减少多次编写的代码行。这将减少开发人员或程序员的时间和精力。如果必须在多个区域使用公共代码,那么我们可以简单地将其包含在一个函数中,并在需要时随时随地调用它。这可以通过在同一程序中调用函数或在某些不同程序中使用函数来实现。
  • 更容易的错误检测:由于代码不是批量编写的,而是拆分或划分为函数,因此可以轻松检测到发生的错误,并且可以快速轻松地修复错误。
  • 易于维护:由于程序中使用了函数,因此如果需要更改任何函数或任何代码行,我们可以轻松地在函数中更改它,并且更改将得到反映。因此,无论在哪里都易于维护。

PHP 中如何使用函数?

正如我们之前讨论的,在 PHP 中我们有两个函数,即内置函数和用户定义函数。让我们更多地了解这些功能:

示例#1

对于字符串函数

代码:

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

输出:

PHP 中的函数

上述程序的说明:在上面的示例中,我们在函数 str_split() 中传递的字符串将字符串拆分为单个字符并生成输出。

示例#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 中的函数类型及其特征。开发人员和程序员尝试使用这两个函数来开发代码,因为他们不必再次编写代码,而且代码很容易测试,因为它是根据必须执行的任务类型编写的。

以上是PHP 中的函数的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn