Jump to content

How do I put my PHP variable in between single quote marks?


Jenksuy

Recommended Posts

Hey, I am trying to insert my variable into single quote marks. the following code works but does not put the single quote marks around the variable as it is part of the variable...

 

    <?php

        $user_id = get_current_user_id();
        $files = glob('wp-content/uploads/'.$user_id.'/*');
        natcasesort($files);
        foreach($files as $file) {
            echo '<img src="/' . $file . '" onclick="fakeClick(event, document.getElementById(' . $file . '))" />';
        }
    ?>

 

this appears as with now single quote around the emboldened..

   <img src="/wp-content/uploads/1/portfolio-sample-main-image.png" onclick="fakeClick(event,     document.getElementById(wp-content/uploads/1/portfolio-sample-main-image.png))">

 

however how I want to appear is as follows...

 

    <img src="/wp-content/uploads/1/portfolio-sample-main-image.png" onclick="fakeClick(event, document.getElementById('wp-content/uploads/1/portfolio-sample-main-image.png'))">

 

as you can see with single quotes... how can I do this?

 

Thanks

echo "<img src='".$file."' onclick='fakeClick(event, document.getElementById(\"".$file."\")";

 

That should do the job, simply mask all output with backslashes.

 

Edit: You need to use differing apostrophes if you are using strings within attributes. Edited my post above a bit.

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.