fantasticode - coding tips, tutorials and forums
Welcome Guest! | Sign Up? | Login |
Total Members: 8
Members Online: 0
Guests Online: 1
Total Online: 1
Total Articles: 4
Total Tutorials: 4
XHTML/HTML Tutorials: 2
CSS Tutorials: 1
PHP Tutorials: 1
Javascript Tutorials: 0
External Tutorials: 0
Total Posts: 2
Total Threads: 2
find us on facebook

Outputting From PHP

Tutorial by fantasticode

This tutorial will show you how to output some text to the user's browser by using PHP.

This should be a really quick tutorial as it's very simple to output text from PHP to the browser. Let's take a look at the code below:

<?php
$myName="Barry";
$yourName="Ed";
echo("Hi $yourName, my name is $myName!");
?>

Pretty easy hey? The above code will output "Hi Ed, my name is Barry!" into the browser window (obviously without the quote marks! Let's take a closer look at what I did...

<?php
.
.
.
?>

That's me opening and closing the PHP tags so that the server knows what it needs to do before the browser gets a hold of the page (servers execute PHP, browsers execute CSS and HTML). It doesn't actually need to be <?php, just <? will suffice as an opening tag, and the closing tag is ALWAYS ?>, and NEVER php?>!

$myName="Barry";
$yourName="Ed";

This part of the code is defining two variables, $myName and $yourName, you can call them whatever you want, but a variable ALWAYS starts with a $ sign, and they ARE case sensitive so be careful! (Also, just so you know, the character immediately after the $ sign cannot be a number, though you can have numbers elsewhere in the variable's name.

echo("Hi $yourName, my name is $myName!");

Here's the important bit - the echo("") function tells the server that the text within it should be outputted to the browser. You can also have HTML code in here, so you could have something like...

echo("
<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\">
<tr><th>Person</th><th>Name</th></tr>
<tr><td>Me</td><td>$myName</td></tr>
<tr><td>You</td><td>$yourName</td></tr>
</table>");

... which outputs a simple table showing the two variables. Note that if you want to have quote marks (") within the echo("") tag, they need to be preceded by a backslash (\) so that the server does not think that they are the closing quote of the echo("") tag.

Rate This Tutorial
1 2 3 4 5 6 7 8 9 10
Google
member avatar forumadmin
member since
26th Feb 2008
view profile

Latest topics:

  • how session are created a …
  • how Dropdown use is HTML?
  • See what else people are chatting about; go to the forums

    Valid XHTML 1.0 Valid CSS