Search code examples
phpfunctionvariablesbitwise-xor

Same string in PHP giving 2 different outputs when echoed to screen when using bitwise functions


Currently, I am working with XOR bitwise function in PHP and am trying to determine the reason why I am not getting my expected output. I hard coded the values and found that when calling a function that contains the echo I get only numbers in my string. But if I echo a variable containing the same data I get the expected output.

<?php
    include "bitwise.php";
    echo $t . "<br>";
    temp("1234");   
?>

I am calling the below code from the above code.

$t = (((((("1324" ^ $h1) . "1234") ^ $h2) . ("1324" ^ $h1)) ^ $h3) . ((("1324" ^ $h1) . "1234") ^ $h2) ^ $h5);

function temp($str)
{
    $test = (((((("1324" ^ $h1) . "1234") ^ $h2) . ("1324" ^ $h1)) ^ $h3) . ((("1324" ^ $h1) . "1234") ^ $h2) ^ $h5);
    echo $test;

    return $test;
}

The $h# are strings contain a jumble of characters that remain constant for both attempts. The outputs are:

$t
Le7KXh}*J;GKUqzo9

$test
9223372036854775807

I would like to know why I am getting only numbers in the second example


Solution

  • As the variables h1 to h5 are defined out of the function scope, they are interpreted as a string and not as their values.

    Try adding the following string to your function:

    global $h1, $h2, $h3, $h4, $h5;
    

    And it should work. Look at this code for reference: http://sandbox.onlinephpfunctions.com/code/a801c15422b69cc2d7ba067e70a5077abe03c1a9