Help using echo

A

Anonymous

Guest
Hi,

echo displays a variable as it is. If the variable itself does not have any line breaks, you will not see any when displaying it with echo. In your program, if you see any line break, i guess they should be because they come from the file you are reading.

Regards.
 
hi from a long-time programmer but PHP newbie :)

:help:

I've been going through the PHP tutorial and manual on php.net.

It says the following should display multiple lines, but it doesn't. What am I doing wrong please?

<?php
echo "Hello World";

echo "This spans
multiple lines. The newlines will be
output as well";

echo "This spans\nmultiple lines. The newlines will be\noutput as well.";

echo "Escaping characters is done \"Like this\".";

?>

It comes out like this:

Hello WorldThis spans multiple lines. The newlines will be output as wellThis spans multiple lines. The newlines will be output as well.Escaping characters is done "Like this".

Also, what are escaping characters, please?
 
Yep. The final result will be HTML, so should use br tags:
Code:
<?php 
echo "Hello World<br />"; 

// This is the same as in one line
echo "This spans 
multiple lines. The newlines will be 
output as well<br />"; 

echo "This spans<br />multiple lines. The newlines will be<br />output as well."; 

// Now see the diference between <br /> and \n in a HTML document
echo "Escaping characters is done \"Like this\".\n\n\<br />n\n\n\"; 
echo 'Hello';

?>
 
Thank you for your reply.

I put in your code exactly as it was, and I got:

"Parse error: parse error, unexpected $ in /home/.../public_html/hallo.php on line 18"

What does this mean, please? There is no "$" in the code.
 
Thank you. The whole code is:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN">
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>

<?php
echo "Hello World<br />";


// This is the same as in one line
echo "This spans
multiple lines. The newlines will be
output as well<br />";


echo "This spans<br />multiple lines. The newlines will be<br />output as well.";

// Now see the diference between <br /> and \n in a HTML document
echo "Escaping characters is done \"Like this\".\n\n\<br />n\n\n\";
echo 'Hello';
?>
</body>
</html>
 
I'm afraid it made no difference. I don't understand this at all. I wil pass on my query to my webspace provider as well, perhaps they know the answer.
 
the error is in the following string:

Code:
echo "Escaping characters is done \"Like this\".\n\n\<br />n\n\n\";
trying yo see the error..
 
found it!

the last \ is doing that.. :)

Code:
echo "Escaping characters is done \"Like this\".\n\n<br />\n\n\n";

thats how its supposed to be
 
Thank you very much. Now it parses okay.

It now gives this:

Hello World
This spans multiple lines. The newlines will be output as well
This spans
multiple lines. The newlines will be
output as well.Escaping characters is done "Like this". \
n Hello

Should it not be like this:

Hello World
This spans
multiple lines. The newlines will be
output as well
This spans
multiple lines. The newlines will be
output as well.Escaping characters is done "Like this". \
n Hello

??

I still don't really understand what the escaping characters are for, but as the code is now parsing, I can experiment with them.

Thank you gesf and Alexei :)
 
well you are wrong as it will be correct if you take a look at the source, but html files need a <br /> tag to output a new line

escaping characters is needed in some cases when you are using php to output html containing javascript method called on event and you cant go around with using only " and ', you also need to have the 3d quotation symbol: thats where escaping comes in..

escaping is also needed when dealing with SQL queries :)
 
Thank you. I think I'm beginning to understand :)

But...

<?php
echo "This spans\nmultiple lines. The newlines will be\noutput as well.";
echo "This spans\rmultiple lines. The newlines will be\routput as well.";
echo "Escaping characters is done \"Like this\".";
?>

leads to ...

This spans multiple lines. The newlines will be output as well.This spans multiple lines. The newlines will be output as well.Escaping characters is done "Like this".

Neither the '/n' or the '/r' seem to make any difference, the line feed and carriage return doesn't happen. Is it not supposed to?
 
Okay, I see it makes the carriage returns and line feeds in the html, but not in the browser output.

Seems a bit pointless to me!

As long as it is doing what it is supposed to do, I am content to leave it for now, as I'm sure I will learn about escaping when I need to.

Thanks.
 
Back
Top