View Single Post
  #1 (permalink)  
Old 09-28-03, 03:06 PM
Antitrust Antitrust is offline
Newbie Coder
 
Join Date: Sep 2003
Location: Canada
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Post [Tutorial] Hello World

I figure we are going to have many PHP rookies looking for some basic help so I decided to get this on tutorial out of the way.

Hello World PHP Tutorial brought you by Silverlime Communications

PHP is a server-side programming language popular for it's rapid development speed and power (along with the fact that it's free and widely distributed).

Today, we'll learn some basics of a PHP file and learn the ability of printing some text which can be seen in a web browser!

File Structure

Basically, this is the structure of a PHP file:

Code:
<?php

// This is commented out - but this is where the code would be if we wanted this script to do anything

?>
We opened the PHP parsing with the <?php followed by a newline. When we didn't want PHP to parse any more code, we closed it with the ?> tag. Straight HTML can be placed outside of these tags if you want that HTML to be sent straight to the browser as a normal .html file would.

Printing

PHP has a few different methods to output text to the screen. The most common, echo, is a language construct and a function (there's no need to say echo("Text"); because echo "Text"; does the same thing). Echo and it's counterpart, print, are almost identical with both printing and both being language constructs. For more information on the differences, visit PHP.FAQTs. Here's a printing demonstration using both:

Code:
<?php

// printing via print()
print("text");
print "text";

// printing via echo()
echo("text");
echo "text";

?>
Single vs. Double Quotes

Most PHP programmers find single quotes, `'`, to be faster than double quotes, `"`. You might find evidence of this when outputting large pieces of text or when parsing variables with double quotes (something single quotes don't do). Single quotes will output whatever is within the quotes without parsing for newlines, \n, carriage returns, \r, variables. $variablename, etc. Here's an example:

Code:
<?php

// set the value of $variable to 'cool'
$variable = 'cool';

// echo with single quotes
echo 'This variable is $variable';

?>
would print
This variable is $variable
while...
Code:
<?php

// set the value of $variable to 'cool'
$variable = 'cool';

// echo with double quotes
echo "This variable is $variable";

?>
would print
This variable is cool
How can we use single quotes to print variables? We add variables to the strings like so:

Code:
<?php

// set the value of $variable to 'cool'
$variable = 'cool';

// echo with single quotes and attach variable
echo 'This variable is ' . $variable . '.';

?>
I even attached a period after the variable is printed. So, here's what we would get:
This variable is cool.
That system provides very clean code and looks much better in syntax highlighting PHP code-editing software. The same can be done with double quotes.

Hello World

Using all of our knowledge, we are now going to print hello world twice using linebreaks, echo(), and print().

Code:
<?php

// output with print() with a linebreak at the end

print "Hello world!\n";

// output with echo using a variable for the text
$text = 'Hello world!';

echo $text;

?>
would print
Hello world!

Hello world!
Recapping

We learned...
  • the basic structure of a PHP file.
  • how to print using echo() and print() as functions and language constructs.
  • how to use single and double quotes properly.
  • how to set the value of variables
  • how to properly include variables while outputting basic text

Questions? Comments? Please post below or contact me.
__________________
Brock Ferguson
Silverlime Communications
http://www.silverlime.ca

Programming, hosting, and more @ Silverlime.ca!

Last edited by Antitrust; 09-28-03 at 03:16 PM.
Reply With Quote