Search code examples
phpencryptionmcrypt

Returning 2 Values from a series of functions


Hi and good day to all members, admin and to everyone. I would like to ask a question that has a connection from my previous post which can be seen here entitled Crypto-Js different output from mcrypt Upon chage of data to encrypt. Now my question is I made another php function that will eventually call this function stated in the link. See below the basic php function I created.

function login($word,$word2)
{

$word = mcrypts_encrypt($word);
$word2 = mcrypts_encrypt($word2);

    return $word;
    return $word2;

}

Now my question is this, I have tried placing the $word and the $word 2 with real data such as CROW and Blader but It only echoes the encrypted word of CROW ($word) and not Blader ($w0rd2).

For reference purpose I will also include the script for the encrypt.

MCRYPT_ENCRYPT

function mcrypts_encrypt($encrypted)
{
//Padding 6/25/2014
    $pad = 16 - (strlen($encrypted) % 16);
    $encrypted = $encrypted . str_repeat(chr($pad), $pad);
//Encrypt//Decode
    $iv = base64_decode('AAAAAAAAAAAAAAAAAAAAAA==');
    $key = base64_decode('ITU2NjNhI0tOc2FmZExOTQ==');
    $plaintext = mcrypt_encrypt( MCRYPT_RIJNDAEL_128, $key, $encrypted, MCRYPT_MODE_CBC,  $iv );
//Return encrypted Data
    return base64_encode($plaintext);
}

Thanks for the help in advance.


Solution

  • You can only call return from a function once, at that point, the flow of code is returned back to the caller.

    To pass multiple values back to the caller, return an array containing both of the values, e.g.

    function login($word,$word2)
    {
        $word = mcrypts_encrypt($word);
        $word2 = mcrypts_encrypt($word2);
    
        return array($word, $word2);
    }
    

    and use as this;

    $encrypted = login('first-word', 'second-word');
    echo $encrypted[0]; // the first word, encrypted
    echo $encrypted[1]; // the second word, encrypted