...

View Full Version : Resolved Quick Syntax Question



tmarch291
05-30-2011, 02:10 AM
Hi all,

I have a problem that I've spent a bit of time trying to figure out, but to no avail.

I'm trying to create a script which basically takes a selection from a drop down box and an image and re-names the image as the selection from the drop down box. I have everything done except for the last line of code which saves the image under the new name.

Currently, I have:


$dest='images\$Order1.jpg';

(Order1 is the name variable)

This doesn't work, however. It simply saves the image as '$Order1.jpg'.

I have tried messing around with the brackets, but I still havent found something that works.

Thanks for any advice.
Tim

tmarch291
05-30-2011, 02:20 AM
EDIT: Found the answer. I just changed the direction of the slash. The working code is:


$dest="images/" . $Order1 .".jpg";

Inigoesdr
05-30-2011, 04:43 AM
The backslash in your original code is used to escape -- in this case escape the variable. In general you should always use forwardslashes in your paths as you figured out to prevent problems like these.

Fou-Lu
05-30-2011, 11:58 AM
The backslash in your original code is used to escape -- in this case escape the variable. In general you should always use forwardslashes in your paths as you figured out to prevent problems like these.

I agree, nowadays there will be no problem using the / in place of \ when accessing the filesystem.
That said, if you really want to use them you would escape the escape. As mentioned, using 'images\$Order1.jpg' would actually create the literal string 'images$Order1.jpg'. The escape tells PHP to not parse that variable as a variable. For the backslash, you would instead use 'images\\$Order1.jpg', which will properly identify the \ instead of the escape.
Most languages handle this better than PHP. Java won't compile for example when you try to give it a path like that. Its much smarter since it will actually evaluate the \x character to see if its an escape sequence before attempting to compile it. PHP on the other hand as a string based language doesn't really care if its in there as its not really a problem to have it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum