Jump to content

PHP doesn't like "\" in string


murdocsvan

Recommended Posts

Here is my code:

 

$site_image = "D:\Hosting\html\" . $_POST['image'];

 

I get the following error:

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

 

The problem:

 

For some reason, PHP doesn't likle me putting "\" at the end of the string, but doesn't mind if it's in the middle. Is there a way around this?

Link to comment
https://forums.phpfreaks.com/topic/205954-php-doesnt-like-in-string/
Share on other sites

The "\" character is the escape character. To use one "\" in a string you need two "\\".

 

Use a forward slash in the file spec. PHP know enough to translate to the Windows form when you're running on Windows. This way you don't have to change your code when you move it to a Linux server.

 

Ken

You could also probably (not knowing how your server is setup) get away with using:

 

$_SERVER['DOCUMENT_ROOT'] to get to your server document root instead of having to manually define it. This will also make the code a bit more portable.

 

Thanks for that, very helpful :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.