How do I echo the original HTML in PHP? - php

I have some HTML. In my HTML, there is a form that uses a PHP script. When I submit this form, it sends me to a blank page. I have tried this, as suggested in a similar question, but it doesn't work because I have HTML events that call functions with arguments, so both " and ' are already used... I am new to PHP, so, is a there a solution to this that I do not know of?

Use the backslash character \ to escape your quotes like so \". The backslash causes PHP to treat the quote as a regular character. :]
Example:
<?php echo "Hello World"; ?>
outputs Hello World
<?php echo "\"Hello World\""; ?> outputs "Hello World"

Escape your quotes.
echo "<h3>First</h3>"
\" is an escaped quote. When a quote is escaped, the environment treats it as an ordinary character, not as a quote. \' Single quotes can also be escaped if need be.
(Not the best example since I'm not also using singles, but you get the point.)

Related

using html and java script tags in php

I am confused about using single and double quotes and back slash while using java script and html tags in php can any one please clarify i googled it but still not clear about it. i am confused for this small thing.i am new to programming
- here is my code
<?php
if(isset($_GET['id'])) {
echo '<div id="d2">';
include "test2.php";
echo '</div>'; }
else
{
echo '<div id="d1">';
include "insert.php";
print "<script type=javascript>"
print "document.getEelementById('alertdiv1').innerHTML='hi' ;"
print "</script>"
echo '</div>';
}
?>
In PHP, you can enclose a string in either single quotes or double quotes. Both are valid:
$var = "this is a string";
$var2 = 'this is also a string';
The main difference is that if your string contains a variable, and you want the variable content to be treated as part of the string, you need to use double quotes:
echo "$var which I made";
will return:
this is a string which I made
When you are manipulating html, css and JavaScript strings, you need to make sure that you don't accidentally close your PHP string. For example:
echo "<h1 class='myheading'>Heading Text</h1>";
Notice how I used double quotes to enclose my string? Because I did that, I was able to use single quotes in the html, without escaping them.
If I'd wanted to use double quotes in my string, I would have had to escape them, like this:
echo "<h1 class=\"myheading\">Heading Text</h1>";
The \ tells PHP that the double quote which immediately follows is to be treated as a literal, and not used to terminate the string.
I can't see any problems relating to quotes in your code.
<script type=javascript> — That is not a valid value of the type attribute (which is optional anyway now). Get rid of the type attribute.
document.getEelementById — Element only has 3 es in it, not 4.
alertdiv1 — There is no element with that id in your code
hi as far as concerned single quotes and double quotes doesnt matters when its a string.but when you use any variable inside
$a = 'hi';
echo '$a' ;
will output
$a
but when you use " "
$a = 'hi';
echo "$a" ;
it will print
hi
Basically, if you're using "" (quotation marks) as your delimiter and you then use a quotation mark as part of the string you must escape it by putting a backslash in front of it.
For example:
$string = "This is my string"; // This is fine as the value of the string doesn't contain any quotation marks (")
$string = "I am including a quote in my string \"To Be Or Not To Be\"."; // This is also fine, as I have escaped the quotation marks inside the string
The same applies if you're using '' (apostrophes) as your delimiter and you then want to use them as part of the string, you must escape them using back slash ().
Hope that helps.
$var = "AAA";
echo 'This costs a lot of $var.'; // This costs a lot of $s.
echo "This costs a lot of $var."; // This costs a lot of AAA.

PHP variables and strings

Getting very confused with echoing an HTML anchor with a variable inside.
<?php
echo ' Next';
?>
I've tried so many variations of lost which ones I've tried. One of the attempts was with curly brackets { but still nothing. I know I'm getting my single and double quotes muddled up!
Could somebody please put me straight on this one. Also, what is the rules for apostrophes and quotes in PHP. If I want to echo something, what shall I start it with, an apostrophe or a quote.
<?php
echo ' Next';
?>
If you want to do some math of other trickery inside an echo, you will need to surround it in brackets.
Edit: #DaveRandom points out that the exception to the trickery clause is $var++ and ++$var.
If you use ' when printing string, everything inside is treated as a text.
If you use ", variables passed inside are converted to the their values.
However it's impossible to do a math operations inside ". You have to escape it and do it in 'PHP way'.
<?php
echo ' Next';
?>
Use double quotes "something" and surround the variables with curly brackets when they are inside the quotes.
echo " <a href='?p={$current_page+1}'>Next</a>";
You can also use string concatenation, which basically means joining a few strings together:
echo 'something' . 'something else' . $my_variable;
As for escaping, if anywhere inside some quotes you want to insert a quote of the same type (e.g. if you surround your script with double quotes and you want to insert a double quote), you need to escape these quotes by prepending them with a backslash - \.
For example you want to output Text and you have surrounded it in double quotes, you need to escape these double quotes in the HREF attribute by prepending them with a backslash \, so the result should be Text.
The following are valid ways of escaping and displaying characters:
echo "it\" so nice to be here";
echo 'it\'s so nice to be here';
echo "it's so nice to be here"; // Different quotes, no need to escape
echo 'it"s so nice to be here'; // Different quotes, no need to escape
The following will result in an error:
echo 'it's so nice the be here';
Because the PHP interpreter will assume the expression to be ended with the quote found in it's, resulting in the rest of the line being treated is invalid code.
For more information you can read the PHP documentation on the echo() function and this wonderful article on Quotes and Strings as well.
I assume you want to do this:
echo ' Next';
You can try This
$link = ' %s';
printf($link, $current_page - 1, "Prev");
printf($link, $current_page + 1, "Next");

How to use single quote inside an echo which is using single quote

First of all, i have gone through the related questions.. haven't found any answer..
I m using this code to display a message
echo 'Here goes your message with an apostrophe S like thi's ';
How can i make this work, as any quote inside this echo will break the statement...
Either escape the quote with a backslash, or use double quotes to designate the string.
echo 'Here goes your message with an apostrophe S like thi\'s';
echo "Here goes your message with an apostrophe S like thi's";
Escape the quote using a backslash.
'hello\'s'
The single quote that appears after the backslash will appear on screen.
Have you tried the function addslashes()? It even uses your example.
I personally prefer the function htmlspecialchars()
which does the same thing but has flags which let you specify its behavior.
like so:
echo htmlspecialchars("O'Rielly", ENT_QUOTES);
This shows the string properly on an HTML webpage.
echo <<<EOT
You can put what ever you want here.. HTML, " ' ` anyting will go
Here goes your message with an apostrophe S like thi's
EOT;
Be sure to read this before using such kind of strings.
In PHP, the escape sequence character is the backslash (\). You can add this before special characters to ensure that those characters are displayed as literals. For example:
echo 'Here goes your message with an apostrophe S like thi\'s ';
Or you can also write like this:
echo "Here goes your message with an apostrophe S like thi's ";
Since the methods from the answers didn't work for my situation I ended up just calling a new echo everytime the type of quote changed through the code and swapped the type of quote to start the echo, it's 2019 now and I don't know about any other solution since I am really new to programming but this worked fine for me, example:
else {
echo '<a onclick="document.getElementById(';
echo "'open_login').style.display='block'";
echo '" class="branding w3-bar-item w3-button w3-mobile w3-light-blue w3-hover-white w3-right"href="#login"><span class="fa fa-user"></span> Login do Aluno</a>';
}
The problem may be that you are passing the variable through HTML in single quotes ''. If it is the case, try using double quotes: "".

How do I let PHP echo "\n" as plain-text for javascript and not have the "\n" create a new line?

PHP is echoing JavaScript (I'm using the jQuery library) something like this:
echo 'var users = $("#add").val().split("\n");';
However, the \n is creating a line break in what the echoed script looks like, and therefore breaking the JavaScript. Is there a way to circumvent this?
Many thanks!
The \n is an escape sequence meaning newline. Backslashes are the beginning of escape sequences, to output a backslash then write \\. So you want \\n. Other useful escape sequences include the quote: use \" to put a quote into the string instead of ending the string.
echo "var users = $(\"#add\").val().split(\"\\n\");";
Not sure If you looking for this
echo "<script>alert('Line1\\\\nThis still in Line1')</script>";

Can't see new lines on textarea - what could the problem be?

I have a php string with a lot of information to be displayed inside a textarea html element.
I don't have access to that textarea nor to the script (if any) that generates it.
$somestring = 'first line \nSecond line \nThird line.';
$somestring as NOT been "worked" with trim or filter_var. Nothing.
On the textfield, I get the \n printed on the textarea hence, not interpreted.
What can I try in order to have those new lines applied?
Thanks in advance.
Try wrapping $somestring with " (double quotes) instead of ' (single quotes)
\n, \r and other backslash escape characters only works in double quotes and heredoc. In single quotes and nowdoc (the single quote version of heredoc), they are read as literal \n and \r.
Example:
<?php
echo "Hello\nWorld"; // Two lines: 'Hello' and 'World'
echo 'Hello\nWorld'; // One line: literally 'Hello\nWorld'
echo <<<HEREDOC
Hello\nWorld
HEREDOC; // Same as "Hello\nWorld"
echo <<<'NOWDOC'
Hello\nWorld
NOWDOC; // Same as 'Hello\nWorld' - only works in PHP 5.3.0+
Read more about this behaviour in the PHP manual
EDIT:
The reason single and double quotes behave differently is because they are both needed in different situations.
For instance, if you would have a string with a lot of new lines, you would use double quotes:
echo "This\nstring\nhas\na\nlot\nof\nlines\n";
But if you would use a string with a lot of backslashes, such as a file name (on Windows) or a regular expression, you would use single quotes to simplify it and avoid having unexpected problems by forgetting to escape a backslash:
echo "C:\this\will\not\work"; // Prints a tab instead of \t and a newline instead of \n
echo 'C:\this\would\work'; // Prints the expected string
echo '/regular expression/'; // Best way to write a regular expression
$somestring = "first line \nSecond line \nThird line.";
http://php.net/types.string <-- extremely useful reading
this article is a cornerstone of PHP knowledge and it's just impossible to use PHP without it.
unlike most of manual pages which are are just for quick reference, this very page is one which every developer should learn by heart.

Categories