首页  >  文章  >  后端开发  >  PHP调用函数

PHP调用函数

WBOY
WBOY原创
2024-08-29 12:47:42641浏览

在 PHP 中,函数有两种类型,即内置函数和用户定义函数。有很多内置函数可以由程序员或开发人员直接从程序中调用。这些内置函数对于要执行的任务具有特定的含义。用户定义函数是由包含要针对应用程序执行的代码的程序专门开发的。开发人员编写一组必须根据应用程序中的要求执行任务的代码。

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

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

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

在 PHP 中定义和调用函数

PHP 中有两种类型的函数,即内置函数和用户定义函数。这些函数在程序中用于执行各种任务。 PHP 中定义了大量的函数,可以在程序的任何地方使用。虽然它有很多不需要一遍又一遍地编写代码的优点,而且功能都是自带的,但开发者可以很容易地使用内置的功能。但所有内置函数都有预定义的含义,并且本身执行特定的任务。因此,开发人员开始开发用户定义的函数,开发人员可以在任何函数内编写特定的任务代码,并且可以在程序中的任何位置使用。用户定义的函数在程序中的类或程序的方法中调用,以根据应用程序的要求执行任务。

如何创建用户定义函数?

在 PHP 中,用户定义的函数必须使用关键字“function”声明。如果我们声明了函数名并且将要执行的代码写在里面,那么它就可以在程序中的任何地方调用。

语法:

function function_name()
{
//statements
}

在上面的语法中,function_name是程序中要调用的函数的名称,function是用于声明函数的关键字。在 PHP 中,函数是用 function 关键字声明的,前缀是函数名,程序中的函数调用只需在需要的地方调用函数名即可。

PHP调用功能实现示例

让我们通过例子来了解一下 PHP 中调用函数的概念。

示例#1

在下面的例子中,函数Write_Output是函数的名称,并且在下面的同名程序中调用该函数。此行将调用定义的函数并执行函数内编写的语句,并在代码的最后一条语句执行后退出。

代码:

<?php
function Write_Output()
{
echo "This is the sample example of a function!";
}
Write_Output();
?>

输出:

PHP调用函数

示例#2

1.在 PHP 中,函数可以是参数化函数,即将参数传递给函数。参数就像我们在程序中定义的变量一样。我们可以简单地在括号内的函数名称后面传递参数,并且可以通过在参数之间添加逗号来添加任意数量的参数。这些参数将在调用函数时进行映射。如果调用函数时没有传递正确数量的参数,它将抛出错误。

代码:

<?php
function Employee($ename) {
echo "$ename Patil \n";
}
Employee("Akash");
Employee("Prakash");
?>

输出:

PHP调用函数

在上面的示例中,Employee 是函数的名称,ename 是在声明中传递给函数的参数。 Employee 函数在函数声明下方调用,并且值在调用时传递给函数。程序的输出将是调用函数时传入函数的员工姓名。

2.在下面的示例中,打印了员工姓名,并且还打印了员工 ID。当一个组织有大量数据要存储在数据库中时,我们只需编写此类代码即可轻松处理数据。

代码:

<?php
function Employee($ename, $id) {
echo "Employee name is $ename and Employee id is $id \n";
}
Employee("Lakshmi","780540");
Employee("Rohit","780541");
Employee("Jenny","780542");
?>

输出:

PHP调用函数

在 PHP 中,变量并不严格基于数据类型,具体取决于其值或数据。数据类型是松散耦合的,不遵循任何严格的规则。因此我们也可以对具有不同数据类型的变量进行加法、减法和多种运算。

Example #3

As we have seen all the examples, we can clearly see that the user-defined functions are more beneficial to the developer as it helps a lot from an application perspective and also is used to get the desired output. The goal of using functions in a particular program is that it can create his own code and develop the application based on requirements.

Code:

<?php
function mulNumbers(int $x, int $y)
{
return $x * $y;
}
echo mulNumbers(5, 13);
?>

Output:

PHP调用函数

As we have seen in the above example that the integer can be multiplied with a string and a string can be used along with float etc. So PHP is a loosely typed language. In the above example, we could clearly see that the variable x and y are declared as integer and while calling they just use one variable as integer and another one as a string and the desired output also gets fetched.

Conclusion

In this article, we discussed how to call functions in PHP i.e. user-defined function. Then we discussed different forms of user-defined functions and syntax with example. The functions can be written for a specific application and to perform a specific task i.e. calculation of payroll, adding new entries, etc. So the developer can easily modify the code for the flexibility and can call it anywhere in the program.

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

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