Results 1 to 5 of 5

Thread: fwrite help

  1. #1
    Join Date
    Mar 2009
    Location
    Chennai, India
    Posts
    77
    Thanks
    16
    Thanked 7 Times in 6 Posts

    Default fwrite help

    Hi I am trying to write to a file using fwrite()...

    Can I use a variable inside fwrite? If yes then how to do that?

    I have tried to use two variables $surl and $lurl but they don't work... Instead of creating a file with the name as the value of the variable, it actually creates a file $surl.php

    Code:
     $fp = fopen('$surl.php','w' );
    fwrite($fp, '<?php header("Location: $lurl"); exit; ?>');
    fclose($fp);

  2. #2
    Join Date
    Mar 2009
    Location
    Chennai, India
    Posts
    77
    Thanks
    16
    Thanked 7 Times in 6 Posts

    Default

    Hi, I got the first line working by changing it to the following:
    Code:
    $fp = fopen($surl . '.php','w' );
    But the second line doesn't work.. It writes the variable itself instead of writing its value.. Pls help

  3. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    You cannot enclose variables in single quotes.

    PHP Code:
    <?php

    $lurl 
    'http://www.some.com';

    $write '<?php header("Location: ' $lurl '"); exit; ?>';

    echo 
    $write// view page source

    //fwrite($fp, $write);

    exit;

  4. The Following User Says Thank You to JasonDFR For This Useful Post:

    borris83 (03-31-2009)

  5. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    If you want to you can use curly braces around variables so you don't have to add concatenating operators to the string, but as Jason says, if you put a variable inside single quotes it will end up writing it out literally.

    PHP Code:
    "Hello my name is {$name}"
    is the same as:

    PHP Code:
    "Hello my name is " $name
    Whatever floats your boat.
    Last edited by Schmoopy; 03-30-2009 at 05:48 PM.

  6. The Following User Says Thank You to Schmoopy For This Useful Post:

    borris83 (03-31-2009)

  7. #5
    Join Date
    Mar 2009
    Location
    Chennai, India
    Posts
    77
    Thanks
    16
    Thanked 7 Times in 6 Posts

    Default

    Thank you,

    This works:
    Code:
    fwrite($fp, '<?php header("Location: ' . $lurl . '"); exit; ?>');

Bookmarks

Posting Permissions

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