-1

I made a function which is contain an include file, Can I compare this function with other function ?

e.g.

in a.php

<?php
$a = "this is file";
?>

in b.php

<?php
$b = "this is file";
?>

in 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";}

?>

I know there's simple way to do my case, but it's just an example, and I want use this way to do my complicated algorithm.

Regards.

Nur Haryadi

Barmar
  • 741,623
  • 53
  • 500
  • 612

2 Answers2

1

You need to put return statements in the functions.

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

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

Then you can use

if (a() == b())

to see if they returned the same thing.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I have use return, but it can't altough it's in if statement. but, I try this one again. Thanks Mr. Barmar for your help, regards... – nur haryadi Apr 25 '15 at 12:42
0

Think this way: When does two functions are equal? When their results with same parameters returns same values. It means your functions needs to return something.

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";
}
Justinas
  • 41,402
  • 5
  • 66
  • 96
  • `a.php` and `b.php` don't echo anything, so there's nothing in the output buffer. They just set a variable. – Barmar Apr 25 '15 at 12:45
  • Thanks for your response Justinas, but how about I have more than 2 functions ? like c(), d(), e(), ... etc ?, my case is if there's many same word it will print one word, but I use my Source like this. Many Thanks and Best regards ... – nur haryadi Apr 25 '15 at 12:53
  • Barmar : yes, it's true, my bad is, I echo something ini a.php and b.php – nur haryadi Apr 25 '15 at 12:56
  • @nurharyadi Maybe try using different logic to see if variable is set and contains some value? – Justinas Apr 25 '15 at 15:21
  • @Justinas, My problem was solve for this one , can I ask an answer which is be related with this one ? I forgot to said that, my function in the loop algorithm. many thanks... – nur haryadi Apr 26 '15 at 11:12