0

I copied some code example to new php file and got this error:

( ! ) Parse error: syntax error, unexpected '$simple_string' (T_VARIABLE) in C:\wamp3\www\my_folder\test_enco.php on line 6

Why is that?

This is the code:

    <?PHP
//    error_reporting(E_ERROR | E_WARNING | E_PARSE);
      
    // Store a string into the variable which 
    // need to be Encrypted 
    $simple_string = "Welcome to GeeksforGeeks\n"; 
      
    // Display the original string 
    echo "Original String: " . $simple_string; 
      
    // Store the cipher method 
    $ciphering = "AES-128-CTR"; 
      
    // Use OpenSSl Encryption method 
    $iv_length = openssl_cipher_iv_length($ciphering); 
    $options = 0; 
      
    // Non-NULL Initialization Vector for encryption 
    $encryption_iv = '1234567891011121'; 
      
    // Store the encryption key 
    $encryption_key = "GeeksforGeeks"; 
      
    // Use openssl_encrypt() function to encrypt the data 
    $encryption = openssl_encrypt($simple_string, $ciphering, 
                $encryption_key, $options, $encryption_iv); 
      
    // Display the encrypted string 
    echo "Encrypted String: " . $encryption . "\n"; 
      
    // Non-NULL Initialization Vector for decryption 
    $decryption_iv = '1234567891011121'; 
      
    // Store the decryption key 
    $decryption_key = "GeeksforGeeks"; 
      
    // Use openssl_decrypt() function to decrypt the data 
    $decryption=openssl_decrypt ($encryption, $ciphering,  
            $decryption_key, $options, $decryption_iv); 
      
    // Display the decrypted string 
    echo "Decrypted String: " . $decryption; 
      
    ?> 
大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
Roi
  • 21
  • 1
  • 1
    the just thing i'll seen it can be problematic it's you – Inazo Oct 09 '20 at 07:57
  • 1
    Are you sure this is the code you're running? It doesn't seem like there's anything wrong. Normally when you get: `Unexpected $`, this means something in the previous line didn't close or go right, but there is nothing wrong with your code before line 6 as your error is showing. – Loko Oct 09 '20 at 08:04
  • The code as shown does _not_ seem to throw the mentioned error: https://3v4l.org/ha6Vk – 04FS Oct 09 '20 at 08:04
  • yes, it's strange. i copied the code from here: https://www.codegrepper.com/code-examples/php/encrypted+and+decrypted+in+php – Roi Oct 09 '20 at 08:05
  • does it works on your server? @Loko – Roi Oct 09 '20 at 08:08
  • 2
    Ah, that explains it then. The code as it is shown there appears to contain non-breaking spaces, hex `C2 A0`, at the beginning of several lines, and that trips up the PHP parser. Try and copy it again _from here_, as it is posted above - that one doesn’t contain these NBSP, and works :-) – 04FS Oct 09 '20 at 08:09
  • @04FS - you are right! that was tricky one...how did you find it out? – Roi Oct 09 '20 at 08:10
  • 1
    https://3v4l.org/cpVFp highlighted those places as errors, when I tested the code copy&pasted directly from the site you mentioned, so I put it into NotePad++ and checked what byte values where in those positions using the Hex Editor ;-) – 04FS Oct 09 '20 at 08:13
  • first time i see https://3v4l.org/cpVFp . I'll keep it for future use. thanks! – Roi Oct 09 '20 at 08:15

0 Answers0