Backslash nightmares - help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ldexterldesign
Forum Newbie
Posts: 15
Joined: Fri Apr 17, 2009 6:14 am
Location: Bristol, UK

Backslash nightmares - help!

Post by ldexterldesign »

Hi guys,

Working on a script here:

Code: Select all

<?php
// get current directory
getcwd();
// change to directory we want
chdir('wp-content\themes\ldexterldesign\images\logos\accreditors\hoverOff');
// add a backslash onto the end of the URL
$cwd = getcwd();
$cwd = $cwd.'\';
// get all the filenames
$files = scandir($cwd);
// count total files in directory
$totalFiles = count($files);
// spit out the filenames with img tag HTML
for($i = 1; $i <= $totalFiles-1; $i++){
    echo '<img src="';
    echo $cwd;
    echo $files[$i];
    echo '"/>';
    }
?>
Basically wanna know why I can't to this:

Code: Select all

// add a backslash onto the end of the URL
$cwd = getcwd();
$cwd = $cwd.'\';
Even trying to add the backslash I need in the echo's won't work:
(It won't even show below in my code below, so I must be doing something wrong)

Code: Select all

    echo '<img src="';
    echo $cwd;
    echo '\';
    echo $files[$i];
    echo '"/>';
A forward slash is OK (I don't want that though), but a backslash throws a parse error. Any thoughts?

Thanks,
L
ldexterldesign
Forum Newbie
Posts: 15
Joined: Fri Apr 17, 2009 6:14 am
Location: Bristol, UK

Re: Backslash nightmares - help!

Post by ldexterldesign »

Solved: I needed to escape the backslash I.E. Add two backslashes '\\':

Code: Select all

    echo '\\';
 
L
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Backslash nightmares - help!

Post by McInfo »

PHP Manual: Strings
PHP Manual wrote:To specify a literal backslash before a single quote, or at the end of the string, double it (\\).
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Mon Jun 14, 2010 4:00 pm, edited 1 time in total.
ldexterldesign
Forum Newbie
Posts: 15
Joined: Fri Apr 17, 2009 6:14 am
Location: Bristol, UK

Re: Backslash nightmares - help!

Post by ldexterldesign »

Cheers mate. I'll remember that one. School boy error I guess, but aren't they all when you know better.

L
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Backslash nightmares - help!

Post by McInfo »

Edit: The following quirk has been fixed with the latest update to the forum software.

To show double backslashes in a post:

Code: Select all

echo '\\';
Use triple backslashes:

Code: Select all

[code=text]echo '\\\';[/code]
Edit: This post was recovered from search engine cache.
Post Reply