Jump to content

Help with writing "$" with fopen


mellis95

Recommended Posts

I just can't get my syntax correct. Could someone help me please? I am trying to write a connect file for inclusion as part of the installation routine for an app I am working on. I need this line to be:

 

$hostname="whatever the use typed on the previous page";

 

Here is the code I am attempting to use:

 

$dir="../includes2/";
$file="connect.inc";
$handle=fopen($dir.$file,'w');
$data="<?php\n";
fwrite($handle,$data);
$data="$hostName ='" . $loc . "'; ";
fwrite($handle,$data);
fclose($handle); 
echo "Check and see if it worked."
?>

The line in question is :

$data="$hostName ='" . $loc . "'; ";

So far, I have only been successful at getting the following end result:

 

<?php
='localhost'; 

What escape characters do I need to use to "$" without php looking for a variable value?

 

Thanks for the help.

Link to comment
https://forums.phpfreaks.com/topic/183171-help-with-writing-with-fopen/
Share on other sites

<?php
$str = '$var';

echo $str; //prints $var
?>

 

alternatively:

 

<?php
$str = "\$var"; //the backslash \ escapes the $ character and is used for escaping in PHP

echo $str; //prints $var

$str2 = "<a href=\"http://www.example.com\">website</a>";

echo $str2; //will output <a href="http://www.example.com">website</a> to the browser

//while...

$str2 = "<a href="http://www.example.com">website</a>";

echo $str2; //will produce a parse error since the double-quotes in the string were not escaped
?>

 

EDIT:

 

<?php
$var = 'variable';

$str = "$var";

echo $str; //prints variable
?>

 

anything within double-quotes " will be parsed by PHP .. anything within single-quotes ', will not (will be displayed as written)

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.