Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By essellar

Thread: Cannot print variable name in PHP file

  1. #1
    gdebojyoti.mail96 is offline x10Hosting Member
    Join Date
    Mar 2011
    Posts
    71

    Exclamation Cannot print variable name in PHP file

    I want to save the following line in a file in PHP:
    Code:
    <?php $value=15; ?>
    For this, I am using the following code:
    Code:
    $myFileHandle=fopen($myurl,'w');   // $myurl = URL of the file
    $myData="<?php $value=".$value."; ?>"; // $value = 15
    fwrite($myFileHandle,$myData);
    fclose($myFileHandle);
    But when I open the file, I find the following statement:
    Code:
    <?php 15=15; ?>
    What am I doing wrong?

    By the way, I am using WAMP server.
    Last edited by gdebojyoti.mail96; 07-08-2012 at 02:12 PM.

  2. #2
    essellar's Avatar
    essellar is online now Community Advocate
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,705

    Re: Cannot print variable name in PHP file

    When you use double quotes as a string delimiter, variables within the string are evaluated. With single quotes, you'd get a literal "$value" in $myData instead of the evaluated version. In double quotes, you need to escape the sigil:

    PHP Code:
    $myData="<?php \$value=".$value."?>"; // $value = 15
    gdebojyoti.mail96 likes this.
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

  3. #3
    gdebojyoti.mail96 is offline x10Hosting Member
    Join Date
    Mar 2011
    Posts
    71

    Smile Re: Cannot print variable name in PHP file

    Thank you.

  4. #4
    misson is offline x10 Spammer
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,573

    Re: Cannot print variable name in PHP file

    Note that if you're using double-quotes, you don't need to use the concatenation operator when combining data from a variable:
    PHP Code:
    $myData="<?php \$value=$value?>"; // $value = 15
    Alternatively, if you use single quotes, you don't need to escape the dollar sign:
    PHP Code:
    $myData='<?php $value=' . (int) $value. '?>'; // $value = 15
    All in all, you'd do well to read up on PHP string syntax.

    Also, be wary of injection attacks. If the data comes from user input, filter it appropriately.
    Last edited by misson; 07-09-2012 at 02:21 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Jon Skeet's and Eric Raymond's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

Similar Threads

  1. Print CSS Errors
    By learning_brain in forum Scripts, 3rd Party Apps, and Programming
    Replies: 2
    Last Post: 06-06-2012, 11:31 AM
  2. [Php] Image return /print
    By beandab in forum Scripts, 3rd Party Apps, and Programming
    Replies: 2
    Last Post: 06-11-2008, 05:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
dedicated servers