在 PHP 中,使用了许多函数,例如内置函数和用户定义函数。每个函数都有自己的功能和属性。函数是在程序中编写的一组语句,可以在代码中任何需要的地方多次使用。需要调用函数来执行函数内编写的语句。它是一段代码,将一个或多个输入作为参数并对其进行处理并返回一个值。程序员只需创建一个函数,然后在程序中需要的地方调用该函数。
广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
在PHP中,程序员主要使用两个函数。他们是:
当开发人员或程序员必须执行自己的代码逻辑时,将使用这些函数。这些函数是使用关键字 function 定义的,在函数内部,将编写一组语句以在发生函数调用时执行它。只需简单地调用函数(如 functionname())即可进行函数调用,并且该函数将被执行。
这些函数为我们提供了内置的库函数。 PHP 在安装包本身中提供了这些功能,这使得该语言更加强大和有用。要使用该函数的属性,我们只需在需要时调用该函数即可获取所需的结果。
PHP 中使用了很多内置函数,例如 Date、Numeric、String 等
以下几点解释了为什么我们应该在 php 中使用函数:
正如我们之前讨论的,在 PHP 中我们有两个函数,即内置函数和用户定义函数。让我们更多地了解这些功能:
对于字符串函数
代码:
<!DOCTYPE html> <html> <body> <?php print_r(str_split("Hi This is a test sample")); ?> </body> </html>
输出:
上述程序的说明:在上面的示例中,我们在函数 str_split() 中传递的字符串将字符串拆分为单个字符并生成输出。
代码:
<!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>
输出:
上面程序的解释:在上面的例子中,函数 strcmp() 会比较字符串,如果字符串相同则返回零,如果字符串不相等则返回零。将返回一些其他数字。
代码:
<!DOCTYPE html> <html> <body> <?php echo strpos("I love coding, I love php too!","coding"); ?> </body> </html>
输出:
The explanation for the above program: This function strpos() will check the position of the string that is passed as a parameter.
Code:
<!DOCTYPE html> <html> <body> <?php echo strrev("Hi world!"); ?> </body> </html>
Output:
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.
Code:
<!DOCTYPE html> <html> <body> <?php echo str_word_count("Hello this is the new world!"); ?> </body> </html>
Output:
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.
Code:
<!DOCTYPE html> <html> <body> <?php echo strlen("Hello this is the test sample!"); ?> </body> </html>
Output:
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.
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:
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.
Code:
<!DOCTYPE html> <html> <body> <?php echo(round(0.65) . "<br>"); echo(round(0.75) . "<br>"); echo(round(0.30) . "<br>"); ?> </body> </html>
Output:
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:
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.
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:
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.
Code:
<!DOCTYPE html> <html> <body> <?php // Invalid calculation will return a NaN value $x = acos(10); var_dump($x); ?> </body> </html>
Output:
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.
Code:
<!DOCTYPE html> <html> <body> <?php $x = 11.35; var_dump(is_float($x)); ?> </body> </html>
Output:
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.
For User-defined functions
Code:
<!DOCTYPE html> <html> <body> <?php function Writefunction() { echo "Hello world!"; } Writefunction(); ?> </body> </html>
Output:
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:
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:
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.
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 是松散类型语言,数据类型没有严格设置,我们可以添加整数和字符串数据类型值来获取输出。在上面的示例中,整数和字符串“5 和 13”相加,输出结果为 18。此功能为用户带来了优势。
在本文中,我们讨论了 PHP 中的函数类型及其特征。开发人员和程序员尝试使用这两个函数来开发代码,因为他们不必再次编写代码,而且代码很容易测试,因为它是根据必须执行的任务类型编写的。
以上是PHP 中的函数的详细内容。更多信息请关注PHP中文网其他相关文章!