Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP Parse error: syntax error, unexpected token "use" #3017

Closed
DrWilliamHobbs opened this issue Feb 13, 2024 · 15 comments
Closed

PHP Parse error: syntax error, unexpected token "use" #3017

DrWilliamHobbs opened this issue Feb 13, 2024 · 15 comments

Comments

@DrWilliamHobbs
Copy link

DrWilliamHobbs commented Feb 13, 2024

Hi - i got these error in the php_error_log:

PHP Parse error: syntax error, unexpected token "use" in /opt/lampp/htdocs/....

The site isnt loading properly and i used only your snippet:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
    

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

I use xampp on linux to test something with php version 8.2.12

@Synchro
Copy link
Member

Synchro commented Feb 13, 2024

A syntax error on that means that you're not using PHP 8.2, but something very old that doesn't support namespaces, which were introduced in PHP 5.3. I suggest you write a script containing <?php phpinfo(); to double check your version.

@DrWilliamHobbs
Copy link
Author

Ive double checked it: Same as before....

PHP Version 8.2.12 Build Date Nov 25 2023 08:05:53

@Synchro
Copy link
Member

Synchro commented Feb 13, 2024

This isn't something specific to PHPMailer. Try seeing if PHP gives you an error if you ask it to run this:

<?php
use abc\xyz;

That's a simple use statement creating an alias for a non-existent class, but it should do nothing at all if you try to run it. If you run it on an old version of PHP, you should get the same error.

@DrWilliamHobbs
Copy link
Author

There is nothing and No errors.

@Synchro
Copy link
Member

Synchro commented Feb 13, 2024

OK, so now add the original lines back in, one at a time, until the problem starts happening.

@XL-2000
Copy link

XL-2000 commented Feb 14, 2024

My guess...

Switch the order of the "require" and "use" lines.
In your code you are using a namespace which is not available yet (since the loading of the files will happen AFTER the declaration.
I expect if you load the files before the "use", it should work (if the files are loaded properly)

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

@Synchro
Copy link
Member

Synchro commented Feb 14, 2024

No, that won't help. Use statements are just local aliases, the require statements have no effect on them. They only come into play when you instantiate a class.

@DrWilliamHobbs
Copy link
Author

OK, so now add the original lines back in, one at a time, until the problem starts happening.

theres is no change

My guess...

Switch the order of the "require" and "use" lines. In your code you are using a namespace which is not available yet (since the loading of the files will happen AFTER the declaration. I expect if you load the files before the "use", it should work (if the files are loaded properly)

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

same...

@XL-2000
Copy link

XL-2000 commented Feb 14, 2024

Two questions:

  1. Are you using <?php at the start of your PHP file, incase unsafe / shorthand tags (<?) are not allowed
  2. In case you are not providing the full code file, are you sure you are not using "use" inside a class declaration? Like:
class Foo { 
use PHPMailer\PHPMailer\PHPMailer;
}

it should be


use PHPMailer\PHPMailer\PHPMailer;
class Foo {
...
}

Maybe its good to provide you full file, since it seems to be a overall syntax error

@DrWilliamHobbs
Copy link
Author

Two questions:

1. Are you using `<?php` at the start of your PHP file, incase unsafe / shorthand tags (`<?`) are not allowed

2. In case you are not providing the full code file, are you sure you are **not** using "`use`" inside a class declaration? Like:
class Foo { 
use PHPMailer\PHPMailer\PHPMailer;
}

it should be


use PHPMailer\PHPMailer\PHPMailer;
class Foo {
...
}

Maybe its good to provide you full file, since it seems to be a overall syntax error

I'll check it on tuesday, because my files are in the office ^^

@XL-2000
Copy link

XL-2000 commented Feb 23, 2024

So, what's the verdict?

@ucsendre
Copy link

ucsendre commented Mar 1, 2024

Hello,
I have the same problem: PHP Parse error: syntax error, unexpected token "use"
The code I try to use is:

<? function cleaninput($input) { $input = trim($input); $input = stripslashes($input); $input = htmlspecialchars($input); return $input;} if (isset($_POST['name']) && isset($_POST['email'])) { if (isset($_POST['name'])) { $felado = cleaninput( $_POST['name'] ); } else { $felado = ''; }; if (isset($_POST['email'])) { $faemail = cleaninput( $_POST['email'] ); } else { $faemail = ''; }; if (isset($_POST['subject'])) { $targy = cleaninput( $_POST['subject'] ); } else { $targy = ''; }; if (isset($_POST['message'])) { $uzenet = cleaninput( $_POST['message'] ); } else { $uzenet = ''; }; require 'inc/Exception.php'; require 'inc/PHPMailer.php'; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; $mail = new PHPMailer(true); try { $mail->setLanguage('hu', 'inc/language/'); $mail->setFrom('[email protected]', $felado); $mail->addAddress('[email protected]', 'Someone'); $mail->addReplyTo($faemail, $felado); $mail->isHTML(true); $mail->Subject = 'Üzenet a weboldalról'; $mail->Body = 'Tárgy: <strong>' . $targy . '</strong><br/>Üzenet:<br/>' . $uzenet; $mail->AltBody = $uzenet; $mail->send(); $vissza = '<div id="form-message-success" class="mb-4">Üzenetét továbbítottuk.</div>'; } catch (Exception $e) { $vissza = "<div id='form-message-warning' class='mb-4'>Az üzenet küldése sikertelen. Hibaüzenet: {$mail->ErrorInfo}</div>"; }; }; header("Location: https://somedomain.hu/"); exit;

@XL-2000
Copy link

XL-2000 commented Mar 3, 2024

And what happens when you put the "use" and "require" lines on the top of your file, so as the first lines?

<?php
require ...
use...

@ucsendre
Copy link

ucsendre commented Mar 4, 2024

And what happens when you put the "use" and "require" lines on the top of your file, so as the first lines?

<?php
require ...
use...

It seems this works.
Thank you!
:)

@XL-2000
Copy link

XL-2000 commented Mar 4, 2024

And what happens when you put the "use" and "require" lines on the top of your file, so as the first lines?

<?php
require ...
use...

It seems this works. Thank you! :)

You're welcome!
That was what I was saying all along!
"Use" is handled compile-time, not run-time.
I guess TS has the same problem

@Synchro Synchro closed this as completed Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants