PDA

View Full Version : PHP's equivialant to document.write


HZR
09-27-2002, 10:59 AM
Don't laugh at me, this is probably a very easy question, but I started with PHP two days ago so...
I want to know PHP's equivialant to document.write. I know about echo and print but they're not what I search for. Lets say I would do something like this in JavaScript; loop through all headings. I would do something like:
for(i = 1; i < 7; i++)
document.write('<h' + i + '>heading' + i + '<h' + i + '>')

If I use echo or print it would write out all the "'<' +" and so on

Hope you understood that.

BTW, maybe it's echo or print (thet're the same thing right?) but I shall do it in another way.

kenrbnsn
09-27-2002, 11:22 AM
It's very similar in PHP...


<?php
$text = "testing123";
for ($i=1;$i<8;$i++) echo "<H$i>$text</h$i><br>\n";
?>


In PHP you can write strings using single quotes (') or double quotes ("). Variable between double quotes get expanded.

You can also concatenate strings using a period (.).

for example another why of doing the above would be:


<?php
$text = "Another test";
for ($i=1;$i<8;$i++)
{
$test_header = "<h" . $i . ">" . $text . "</h" . $i . "><br>\n";
}
?>


Have fun learning PHP. It can be a very powerful and useful tool.

I have been using it for about 3 years.

Ken

HZR
09-27-2002, 01:39 PM
Thanks for tha answer. The first one works good but the second one doesn't. Maybe you forgot something? Or maybe I did anything wrong.
Anyway, thanks :)

kenrbnsn
09-27-2002, 04:49 PM
Yes, I forgot to echo the string in the second example... :(

Ken