I have to pass a string to a form.
I want to use a mix of $_GET and $_POST in the following way:
<?php $string="bla bla bla".$some_other_string."bla bla".$some_other_string2."\n"; ?>
<form action="this_page.php?string=<?php echo $string?>" method="post" name="name">
</form>
Please note the \n in the string.
When I get the $_GET("string") (or $_REQUEST("string"), it happens the following: the php parser get the \n as a string. So he put it into the string as I had coded: \\\n and it’s not the result I want. I want simply a inner \n in a php string.
Please can you explain me why this behaviour happens, without complaining that it’s not the best way to pass a string (I suppose it isn’t, but today I had an issue and I wanted to manage it by passing the string this way)?
Ps: I have to use the \n and not the html <br /> because I have to let php write into a file.
Advice: You probably should think about putting this into a hidden field
Answer: But if you are going to put it into action as a GET parameter, then you need to urlencode the string:
That way – any non-alphanumeric characters will be url safe (think about what might happen if $string contained a ? or a # or an = ).
PHP automatically decodes GET parameters when loading them into the $_GET array – so your newline should be preserved at that point.