Jump to content

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

Go to solution Solved by 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...



        $user_id = get_current_user_id();
        $files = glob('wp-content/uploads/'.$user_id.'/*');
        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?



Link to comment
Share on other sites

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.

Edited by Irate
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.