Advertisement:

Author Topic: PHP Lesson 02 - php basics  (Read 33641 times)

Offline Parham

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,317
  • Gender: Male
  • #!/usr/bin/perl
    • Online Node
PHP Lesson 02 - php basics
« on: August 26, 2003, 02:50:29 PM »
The greatest thing about PHP is the fact that it's a web-specific language.  The language is specifically intended to be used for internet development which means you get many features which makes it easier for you to work with the language on the internet.  One of the greatest things about PHP is how you can mix and match PHP code with HTML code.

If you're familiar with HTML, you know what all HTML tags (special words that format an HTML webpage) are encased in <s and >s.  For example, to bold a string, you would sandwich it between the <b> and </b> tags.  PHP doesn't quite work this way.  Instead, all PHP code goes right inside a huge single tag - the PHP tag.

The PHP tag can look one of two standard ways (there are other alternatives which are rarely used):
<?php ?> - the long version of the tag
or
<? ?> - the short version of the tag

Anything inside these tags will be recognized as PHP code, anything outside these PHP tags will be recognized as plain HTML.  A simple example is the following (again, you don't need to know exactly what the following does right now).  Save the following file into your "www" directory and run it by going to "http://127.0.0.1/basic.php".

basic.php
Code: [Select]
<html>
<head>
<title></title>
</head>
<body>

<? print 'php code here'; ?>

</body>
</html>

Some people don't like this method and will instead encase the entire file's contents inside PHP tags and use functions inside the PHP language to output their HTML code.

The other basic thing I want to cover in this lesson is how to "comment" in PHP.  A comment is a way for you to insert explanations of code in plain English which will in NO way affect your program.  Comments help other programmers understand code you've written.  Comments can be written in several ways (save and test the following file):

comments.php
Code: [Select]
<?
print 'hello<br>'; //this is a comment, this line prints "hello"

print 'how are you?<br>'; #this is also a comment, this line prints "how are you?"

print 'I am good thank you<br>'; /* this is a multi-line comment
                                    and can span several lines */
?>

Anything after "//" or "#" on a line is interpreted as a comment and will not be used.  Anything between the "/* */" characters will also be interpreted as comments.  Comments can be stuck anywhere in between lines of code or after code on a line, but not before except the "/* and */" comment because then all code after the comment will be interpreted as comments too.

For example:

Code: [Select]
<?
print 'hello<br>'; //this is a comment, this line prints "hello"
//here is another comment tag
print 'how are you?<br>'; #this is also a comment, this line prints "how are you?"
#more comments inbetween
/* a comment before with this type of comment tag */ print 'I am good thank you<br>';
/* this is a multi-line comment and can span several lines */
?>

As you've already guessed by now "print" outputs text to the browser, something we'll be going over in our next lesson anyway.
« Last Edit: March 29, 2004, 05:03:40 AM by [Unknown] »

Offline Joshua Dickerson

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 12,778
  • Gender: Male
    • joshuaadickerson on GitHub
    • joshuaadickerson on LinkedIn
Re:PHP Lesson 2
« Reply #1 on: August 26, 2003, 03:43:23 PM »
NOTE: Don't use print, it is useless compared to echo.
Need help? See the wiki. Want to help SMF? See the wiki!

Did you know you can help develop SMF? See us on Github.

How have you bettered the world today?

Offline Jeff Lewis

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 3,449
  • Gender: Male
Re:PHP Lesson 2
« Reply #2 on: August 26, 2003, 03:48:32 PM »
Co-Founder of SMF

Offline Parham

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,317
  • Gender: Male
  • #!/usr/bin/perl
    • Online Node
Re:PHP Lesson 2
« Reply #3 on: August 26, 2003, 04:43:16 PM »
LOL i was going to cover that in the next lesson when I went through "print" and "echo".  Anyway, reason for using "print" was that it is much more of a reasonable function name.  I went STRICTLY with name convention here.

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re:PHP Lesson 2
« Reply #4 on: August 26, 2003, 04:58:19 PM »
I'd just like to say that the difference is not subtle... using echo WITH commas is incredibily better than print with dots or double quotes.

It should also be noted that some configurations of PHP don't allow "short tags", so you should get used to using <?php ?>.

-[Unknown]

Offline Jeff Lewis

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 3,449
  • Gender: Male
Re:PHP Lesson 2
« Reply #5 on: August 26, 2003, 05:53:31 PM »
I was only quoting something Rasmus wrote :)
Co-Founder of SMF

Offline zoki

  • Semi-Newbie
  • *
  • Posts: 43
  • Gender: Male
  • Have you seen me offline?
Re:PHP Lesson 2
« Reply #6 on: November 07, 2003, 06:25:20 PM »
I'd just like to say that the difference is not subtle... using echo WITH commas is incredibily better than print with dots or double quotes.
-[Unknown]

I actally got used to echo with dots ;]