Current location: Hot Scripts Forums » Programming Languages » PHP » [Tutorial] Hello World


[Tutorial] Hello World

Reply
  #1 (permalink)  
Old 09-28-03, 04: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 04:16 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 09-28-03, 04:11 PM
Mud Mud is offline
Newbie Coder
 
Join Date: Sep 2003
Location: Southern California
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Wheres the Hellow World? jk Thanks i look forward to more tutorials along the way. Maybe you can make a php tutorial section on your site. And just paste the tuts here?
__________________
http://websoftblog.com/ - Web Software Blog
http://www.seekpire.com/ Make money searching!
http://www.gamingwise.com/ flash games!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 09-28-03, 04:12 PM
Antitrust Antitrust is offline
Newbie Coder
 
Join Date: Sep 2003
Location: Canada
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Mud
Wheres the Hellow World? jk Thanks i look forward to more tutorials along the way. Maybe you can make a php tutorial section on your site. And just paste the tuts here?
LOL, shows where my head was when I wrote that. Let me add that section
__________________
Brock Ferguson
Silverlime Communications
http://www.silverlime.ca

Programming, hosting, and more @ Silverlime.ca!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 09-29-03, 07:06 AM
The Wolf's Avatar
The Wolf The Wolf is offline
CMS Developer
 
Join Date: Jun 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Code:
echo 'This variable is ' . $variable . '.';
Thats a bad thing to teach someone, this code is much better and faster:

Code:
echo 'This variable is ', $variable, '.';
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 09-29-03, 07:38 AM
Antitrust Antitrust is offline
Newbie Coder
 
Join Date: Sep 2003
Location: Canada
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by The Wolf
Code:
echo 'This variable is ' . $variable . '.';
Thats a bad thing to teach someone, this code is much better and faster:

Code:
echo 'This variable is ', $variable, '.';
Really? I have always felt the opposite. This is a good note to have for beginners, though, because it does work both ways.
__________________
Brock Ferguson
Silverlime Communications
http://www.silverlime.ca

Programming, hosting, and more @ Silverlime.ca!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 10-12-03, 03:58 PM
Neo Neo is offline
New Member
 
Join Date: Oct 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
www.n-prime.com/testing/variables.php

i used this code

<?php

print "Please Work\n";

$text = 'Didnt think it would work';

echo $text;

?>

and as you can see it didnt work fully, can some one tell me why?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 10-12-03, 05:47 PM
Archbob Archbob is offline
Newbie Coder
 
Join Date: Jul 2003
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Try putting the text in double quotes.
__________________
Master Chipmunk and programmer of cheap scripts.
Chipmunk Scripts -- Free GPL scripts
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 10-12-03, 06:34 PM
Chroder Chroder is offline
Newbie Coder
 
Join Date: Sep 2003
Location: Toronto, Ontario
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by The Wolf
Code:
echo 'This variable is ' . $variable . '.';
Thats a bad thing to teach someone, this code is much better and faster:

Code:
echo 'This variable is ', $variable, '.';
I think its better to introduce the dot operator to a beginner.
__________________
DevBox.net | DevBoxForums.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 10-13-03, 05:13 AM
Snake Shift Snake Shift is offline
New Member
 
Join Date: Oct 2003
Location: dunno
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
I think the concatenation operator is better, because when you start to deal with real functions the comma is what seperates variables.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 10-14-03, 08:24 AM
Neo Neo is offline
New Member
 
Join Date: Oct 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Well i managed to figure out what was wrong with it, since then i've just kept learning, i now know about arrays, If statements and Else statements, oh yeah and i just made a simple multiplying thing using php and HTML http://www.n-prime.com/testing/cool.html i know its nothing special but im proud of it, hopefully i will be learning more skills soon, do you guys have any more tutorials?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the best cities in the world for learning php united-hosts PHP 6 10-14-03 05:04 AM
The World Is Breathing Java! fouad Everything Java 16 08-19-03 06:53 PM
Let's code the world prettypretty The Lounge 6 06-08-03 01:30 AM


All times are GMT -5. The time now is 08:49 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.