Jump to content

Writing To File - New Question.


norNerd

Recommended Posts

Hi, I've been searching for the HTML code for the $ sign, becouse i need to write variables to a php file, but after searching for about an hour without any luck I gave up.When I write to the file this way: $var = "string"; it outputs only: = "string"; As the variable is an active variable for the php script, I also tried to write it like this: \$\var but then I got: $\var and thats not working verry good either :)..So I wanna know if anyone has a good solution for this problem I got?----------------------------------(new question is at the bottom)norNerd

Link to comment
Share on other sites

Use single quotes for the string that contains the dollar. For example,

echo '$var = "string";';

will output

$var = "string";

Link to comment
Share on other sites

Ok, now to my next question, what in the world can I do to be able to do this:

<?php$secondvariable = "my_irritating_string";$some_text = '<?phphere is some text that will go into the file i write too... and after the text we put in this:$variable="$secondvariable";?>';?>

Is that possible to do?And if so, how can I do it?norNerd

Link to comment
Share on other sites

If you write PHP files, you need to make sure you're not prematurely turning off the executing PHP script itself. To do that, simply separate the "?" and the ">", like so:

$some_text = '<?phphere is some text that will go into the file i write too... and after the text we put in this:$variable="$secondvariable";?' . '>';

Link to comment
Share on other sites

Ok thanks :) But i wonder, as the $variable does not get printed as a "real" variable but as text I have a problem understanding how I can make the $secondvariable to be a real variable? :sVerry confusing...Any clues? :)..norNerd

Link to comment
Share on other sites

Look, you need to understand the difference between a variable and a string.A string is just a piece of text. A variable is... errr.... let's say "a shortcut" to this text.Strings can be concatenated (i.e. added together into a new string) with the "." operator, an example of which is above. Whether you use a "shortcut" to a text, or the text itself, it's the same deal.Armed with that new knowledge, try to figure your question out. Try something, and if you fail, post your failing example.

Link to comment
Share on other sites

First thing, i know what a variable is and what a string is, therefor i said $var = "string";, and when thats clear i can try to format my question in a more understandable way.

if(isset($_POST['write'])){	$variable1 = $_POST['variable1']; 	$variable2 = $_POST['variable1']; 			$text = '<?php		$variable_ins1 = "$variable1";		$variable_ins2 = "$variable2";		?>';				echo file_put_contents("myfile.php","$text");		}

That code will work, but in the wrong way, it will write the $text as it is right into myfile.php,the problem there is that i want it to write it like this:

if(isset($_POST['write'])){	$variable1 = $_POST['variable1']; //Lets say he posts Hello	$variable2 = $_POST['variable1']; //And World			$text = '<?php		$variable_ins1 = "Hello";		$variable_ins2 = "World";		?>';				echo file_put_contents("myfile.php","$text");		}

And this dont work:

if(isset($_POST['write'])){	$variable1 = $_POST['variable1']; //Lets say he posts Hello	$variable2 = $_POST['variable1']; //And World			$text = '<?php		$variable_ins1 = "".$variable1."";		$variable_ins2 = "".$variable2."";		?>';				echo file_put_contents("myfile.php","$text");		}

And this dont work:

if(isset($_POST['write'])){	$variable1 = $_POST['variable1']; //Lets say he posts Hello	$variable2 = $_POST['variable1']; //And World			$text = '<?php		$variable_ins1 = "."$variable1".";		$variable_ins2 = "."$variable2".";		?>';				echo file_put_contents("myfile.php","$text");		}

So sorry for really not understanding how i can do this, and sorry for asking how to do it in a web development support forum, but i really dont have any clue how to do this, so thats why i ask.norNerd

Link to comment
Share on other sites

Did one last shot and found the solution:

		$text = '<?php		$variable_ins1 = '$variable1="'."$variable1".'";';		$variable_ins2=  '$variable2="'."$variable2".'";';		?>';

Thanks :)norNerd

Link to comment
Share on other sites

Strings beginning with single quotes are outputted "as is". Strings with double quotes have their variables resolved. You can write a single quote within a single quoted string by prepending the inner ' with "\", e.g.

'That\'s cool.'

And in the same fashion, you can put double quotes in a double quoted string, i.e.

"And he said \"because I say so\", which I thought was cool."

What you want is single quoted strings, concatenated with variable values. I'm not certain as to whether you want the output to use single or double quotes, but for simplicity, let's use double quotes. Therefore,

'$variable_ins1 = "' . $variable1 . '"'

Will output

$variable_ins1 = "Hello"

(I'm also assuming $variable1 equals "Hello")Notice how the ' after the " terminates the first string, then the . concatenates it with the value of $variable1. The result string is then further concatenated with a single quoted string that only has " in it.If you wanted to get a single quoted string as output, you'd escape the single quote in the single quoted string, i.e.

'$variable_ins1 = \'' . $variable1 . '\''

Or you can concatenate double quoted strings with single quotes inside, also escaping any dollars, i.e.

"\$variable_ins1 = '" . $variable1 . "'"

(I wouldn't use that last one, but it's still an option)[edit]Oh... you've figured it out... sorry... oh well. For future reference's sake.[/edit]

Link to comment
Share on other sites

Those examples of yours should really be out on w3schools somewhere, thanks for makeing it 100% clear to me how to use single and duble quotes :)Was a few things there i was thinkin about so thanks again for those :)norNerd

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...