将一个函数与另一个函数进行比较
Compare a function with another function
我制作了一个包含包含文件的函数,
我可以将此功能与其他功能进行比较吗?
例如
在a.php
<?php
$a = "this is file";
?>
在b.php
<?php
$b = "this is file";
?>
在function.php
<?php
function a(){
include("a.php");
}
function b(){
include ("b.php");
}
/*
my question is can I compare between function a() and function b() ?
like this
*/
if(a()==b()){
echo "it's same words";
}
else
{echo "not same words";}
?>
我知道有一种简单的方法可以解决我的问题,但这只是一个例子,
我想用这种方式来做我复杂的算法。
此致。
努尔哈里亚迪
您需要在函数中加入 return
语句。
function a() {
include("a.php");
return $a;
}
function b() {
include("b.php");
return $b;
}
那你就可以使用
if (a() == b())
看看他们是否返回相同的东西。
这样想:两个函数什么时候相等?当它们的结果具有相同的参数 returns 相同的值时。这意味着您的函数需要 return 东西。
function a() {
ob_start();
include("a.php");
return ob_get_clean();
}
function b() {
ob_start();
include("b.php");
return ob_get_clean();
}
if (strcmp(a(), b()) == 0) {
echo "it's same words";
}
我制作了一个包含包含文件的函数, 我可以将此功能与其他功能进行比较吗?
例如
在a.php
<?php
$a = "this is file";
?>
在b.php
<?php
$b = "this is file";
?>
在function.php
<?php
function a(){
include("a.php");
}
function b(){
include ("b.php");
}
/*
my question is can I compare between function a() and function b() ?
like this
*/
if(a()==b()){
echo "it's same words";
}
else
{echo "not same words";}
?>
我知道有一种简单的方法可以解决我的问题,但这只是一个例子, 我想用这种方式来做我复杂的算法。
此致。
努尔哈里亚迪
您需要在函数中加入 return
语句。
function a() {
include("a.php");
return $a;
}
function b() {
include("b.php");
return $b;
}
那你就可以使用
if (a() == b())
看看他们是否返回相同的东西。
这样想:两个函数什么时候相等?当它们的结果具有相同的参数 returns 相同的值时。这意味着您的函数需要 return 东西。
function a() {
ob_start();
include("a.php");
return ob_get_clean();
}
function b() {
ob_start();
include("b.php");
return ob_get_clean();
}
if (strcmp(a(), b()) == 0) {
echo "it's same words";
}