Navigation
Poll
Would you be an active poster if Ron's Guide had a message board?


Total Votes: 62
Comments: 18 — View
Past pollsPoll idea?
Rate Ron's Guide
Rate our resource at Bigwebmaster.com
PHP Basics
This tutorial will cover some basic concepts of PHP, great if you're new to PHP.
PHP Tags

To start your PHP scripts you need to use the PHP tags, these tags will tell the server that what is inside the tags needs to be parsed by the PHP engine before it's output is displayed. The opening and closing tags are shown below.

<?php // opening PHP tag

/*
  everthing between the two tags (parts in blue)
  will be parsed by PHP and then the output will be
  displayed in the browser.
*/

// closing PHP tag -> 
?>
Comments

See the orange text in the example above? Those are comments. Comments allow you to leave yourself notes or reminders while you're coding. They're also useful in situations where you are working with multiple programmers. You can create either single line comments or multiple line comments.

To make a single line comment just type "//", all of text after that, on the same line be commented out. You can also use the "#" character to create single line comments.

To create multiple line comments type "/*" followed by your comments, then type "*/" when you want to end your commenting.

Basic Echo

One of the most basic things you should know how to do with PHP is a simple echo. by 'an echo' I mean printing out text to the page. To do this, we'll use the built in PHP function, echo. Here's an example:

<?php

// Make sure to end every line with a semicolon!(;)
echo "Text in these quotes will show up on the page. ";
echo 
'You can also use single quotes to echo text. ';

// You can also use the print function
print "print and echo do the same thing. ";
print 
"It's up to you which one you want to use.";

?>

If you are using double quotes to write your text and you want print out double quotes, you need to escape them. To do this just put a "\" before your quotes. Do the same if you want to print out a single quote when you are using single quotes for the echo. I know that sounds a bit confusing, so here's an example:

<?php

// escape the double quotes
echo "The name of this tutorial is \"PHP Basics\".";

// escape the single quote
echo 'Hopefully it\'s been good so far.';

?>

It's important to remember to escape your quotes when nessecary, something as small as that can somtimes be the cause of PHP parse errors that make you want to just pull out all of your hair. Tongue

Variables

If you've studied algebra, then you've come across variables before. Variables are used in PHP too, they're used to hold values. To create a variable, just type a dollar sign($) followed by your variable name, an equal sign(=) and the value you want it to hold.
Here's an example:

<?php

// Let's stick some text in a variable.
$myText "This text is now held in a variable.";

// That's easy, how about a number?
$myNumber 87;

?>

Notice, I didn't use quotes when declaring the $myNumber variable. When assigning an integer, quotes may be used, but aren't required.

Now that these variables are defined, you can use them later in your scripts, simply by calling upon the variables, here's an example:

<?php

// output the contents of the variable $myText
echo $myText.'<br />';
echo 
'19'.$myNumber.'<br />'// will output '1987'

?>

The period after '19' is used in PHP as a concatenation operator, which means that the '87', held in $myNumber will be combined, or connected, with the '19', then the result (1987) is echoed. For more information on this, see the PHP manual on String Operators.

One important thing to remember about variables is that they are case-sensitive, which means $myvar and $myVar are two completely different variables. It's also important to remember that variable names cannot start with numbers. They may only start with underscores(_) and letters. However, they may contain any combination of letters, numbers, and underscores after that.

That's it for the PHP basics tutorial, I hope it helped you.

Discuss Tutorial: PHP Basics 18 Comments
Comment by Ron on Jun 8, 2004, 5:01 am
Please post questions or comments about this tutorial below. Smile
Comment by Haldred on Jun 8, 2004, 7:08 pm
I loved that Tutorial. But what would the Variable look like?
Oh, and congratz on becoming Green on SSD. Wink
Comment by Masked on Jun 9, 2004, 5:31 pm
Very nice. Helped me out quite a bit. Grin
Comment by Ron on Jun 10, 2004, 3:02 am
Thanks Haldred Smile
Great, glad to hear it Masked Grin
Comment by tortured-x on Aug 26, 2004, 5:44 am
great tutorial, and haldred as for variables they are usually defined by a $ prefix, it's be something like $1 = test

Wink
Comment by Kaos on Sep 10, 2004, 1:57 am
Nice tut but im just beginig in this so what is the best program to handle php?
Comment by [dmsuperman] on Oct 14, 2004, 1:58 am
Notepad. Tongue It's so awesome.

I already knew this stuff, but good tut for those who didn't.
Comment by Ultra_Immortal on Jan 12, 2005, 12:29 pm
That guide was too hard for me. I am just begining.
Comment by mejee on Feb 22, 2005, 11:05 pm
Great stuff Ron now how about some more? Rolleyes
Comment by Me on Mar 31, 2005, 1:19 pm
Thats tutorial was great but I'm still a little confused, thats just me all over!!!

« Previous [ 1 2 ] Next »
Post a comment
Sorry, you must be a registered member to post comments.

If you would like to register, you can do so here.
If you already have an account, please login.