PHP Lesson 02 - php basics

Started by Parham, August 26, 2003, 02:50:29 PM

Previous topic - Next topic

Parham

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

<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

<?
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:


<?
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.

Joshua Dickerson

NOTE: Don't use print, it is useless compared to echo.
Come work with me at Promenade Group



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?

Jeff Lewis

Co-Founder of SMF

Parham

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.

[Unknown]

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]

Jeff Lewis

I was only quoting something Rasmus wrote :)
Co-Founder of SMF

zoki

Quote from: [Unknown] 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.
-[Unknown]

I actally got used to echo with dots ;]

Advertisement: