【问题标题】:Compare a function with another function比较一个函数和另一个函数
【发布时间】:2015-04-25 12:06:00
【问题描述】:

我创建了一个包含包含文件的函数, 我可以将此功能与其他功能进行比较吗?

例如

在一个.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";}

?>

我知道有一种简单的方法来处理我的情况,但这只是一个示例, 我想用这种方式来做我复杂的算法。

问候。

努尔哈里亚迪

【问题讨论】:

标签: php function compare


【解决方案1】:

您需要将return 语句放入函数中。

function a() {
    include("a.php");
    return $a;
}

function b() {
    include("b.php");
    return $b;
}

然后就可以使用了

if (a() == b())

看看他们是否返回了相同的东西。

【讨论】:

  • 我有使用 return,但它不能在 if 语句中。但是,我再试一次。感谢 Barmar 先生的帮助,问候...
【解决方案2】:

这样想:两个函数何时相等?当它们具有相同参数的结果返回相同的值时。这意味着你的函数需要返回一些东西。

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.phpb.php 不回显任何内容,因此输出缓冲区中没有任何内容。他们只是设置了一个变量。
  • 感谢您的回复 Justinas,但是我有两个以上的功能怎么样?像 c(), d(), e(), ... 等等?,我的情况是,如果有很多相同的单词,它会打印一个单词,但我像这样使用我的 Source。非常感谢和最好的问候......
  • Barmar : 是的,这是真的,我不好的是,我回显了一些 ini a.php 和 b.php
  • @nurharyadi 也许尝试使用不同的逻辑来查看变量是否已设置并包含一些值?
  • @Justinas,我的问题是解决这个问题,我可以问一个与这个问题相关的答案吗?我忘了说,我的函数在循环算法中。非常感谢...
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 2022-08-04
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多