将c ++函数转换为PHP?

I would like to convert my xor function to PHP. Can it be done? It needs to work as it`s working now...

string encryptDecrypt(string toEncrypt) {
    char key[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    string output = toEncrypt;
    for (int i = 0; i < toEncrypt.size(); i++)
        output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))];

    return output;
}

You can use almost the same syntax in PHP as your C++ function does:

function encryptDecrypt($toEncrypt)
{
    $key= array( '1', '2', '3', '4', '5', '6', '7', '8', '9' );
    $key_len = count($key);
    $output = $toEncrypt;
    for ($i = 0; $i < strlen($toEncrypt); $i++)
    {
       $output[$i] = $toEncrypt[$i] ^ $key[$i % $key_len];
    }

    return $output;
}

Online demo for C++ function: https://ideone.com/g9cpHJ

Online demo for PHP function: https://ideone.com/3prgd0

In PHP it's something like this:

function encryptDecrypt($toEncrypt){
    $key = array( '1', '2', '3', '4', '5', '6', '7', '8', '9' );
    $output = $toEncrypt;
    for ($i = 0; $i < strlen($toEncrypt); $i++)
        $output = pow( $toEncrypt[$i], $key[$i % count($key)]; );

    return $output; }

I hope to be fine.