PHP Tutorial Part One: Getting Started with PHP

khess -8 Tallied Votes 1K Views Share

Programming in PHP is fun and easy. It is a very powerful scripting language that takes simple HTML and turns it into a fully-interactive experience for the web user. Let's get started!

Note: These tutorials assume that you have a working web server that's capable of displaying PHP code and that you have PHP installed on that web server system or that you're using a server that is equipped and ready.
You can run PHP on Windows. You can even use PHP with IIS (The Windows Web Server) so you don't necessarily need Unix/Linux and Apache.

PHP Reference

Your best reference for all things PHP is the PHP web site: www.php.net. Go there and bookmark it--you'll refer to it often.

PHP Speaks HTML

The first thing you have to learn and always remember is that if you use even one line of PHP code in an HTML file, you must name the file with a .php extension. The .php extension tells the web server that this file is special and needs special handling by the PHP executable and related libraries.

Delimit Your PHP

Second, all PHP functions and code must be inserted between the following delimiters

<? php code ?>

--the less than question mark opens the code and the question mark greater than close. You must always use delimiters in pairs--an opening delimiter and closing delimiter. Delimiters are symbols that tell the web server what kind of content follows and how it should be handled. HTML uses delimiters too

<TAG>

.

Example 1: How About a Date?

If you want to add the current date to a web page, the code would look like:

<HTML><BODY>
<? echo date("M-d-Y"); ?>
</BODY></HTML>

Explanation: The 'echo' command tells the PHP program to echo the results that follow to the screen. Since you're using HTML and the browser is your "screen", that's where you see the results. The date function is a built-in PHP program that display the date in the format you specify in the parentheses.

Save the file with the filename, test.php.

Open the file by pointing your browser to the file on the webserver and you should see:

Jan-19-2009 (you'll see the current date)

Try changing the date format by moving things around ("d-M-Y") or just using one of the date variables ("Y"). Refer to the PHP documentation to get a full range of date variables. It's fun and useful.

The full list of PHP date-related functions are here.

Example 2: Who's Looking at You, Kid?

If you want to gather some info about the people that are looking at your site or if you just want to let them know you're watching, you can easily echo some of their information back to them by using what's known as Server Variables.

Note: This is not personal information, nor are you stealing anything from them. It is information that is transferred to your website when a visitor connects to your webpages.

You can get their IP Address and their Browser and Computer information. The IP Address is how computers communicate with each other on the Internet--think of it as kind of like a phone number or home address.

To see this information, edit your test.php file again and enter the following:

<HTML><BODY>
<? echo date("M-d-Y");
echo "<P>";
echo "Your IP Address is " . $_SERVER['REMOTE_ADDR'];
?>
</BODY></HTML>

Explanation: The line

echo "<P>";

is HTML for start a new Paragraph. The next line,

echo "Your IP Address is " . $_SERVER['REMOTE_ADDR'];

sends the text, "Your IP Address is " to your browser. The dot (.) is PHP for concatenate or connect these two things. $SERVER is a built-in Server variable that echoes the viewing browser's IP Address to their browser.

Save this file and point your browser to test.php.

You should see:

Jan-19-2009 (Current Date)

Your IP Address is 10.0.1.200 (The IP Address of Your Computer)

There are several variables from which to choose here. The complete list is located at: PHP:$_SERVER.

Change the Server variable or add new ones. For starters, try: HTTP_USER_AGENT to see your browser and computer information.

Stay tuned for PHP Tutorial Part Two: Combining HTML and PHP where you'll get a taste of how to integrate these two languages in a more elegant fashion. Don't worry, it's easy and you'll like the way it works. It's easier to read and more fun to write.

Comatose commented: Short Tags? No XHTML? Taste It! -2
John A commented: Why are you using short tags? It's bad enough that you recommend their use in a tutorial period, it's even worse that you don't bother explaining what they are. -4
Atli commented: Short tags! Why do you hate the newbies? +0
SeanOBrian commented: Bad markup, short PHP tags, this is not an appropriate beginner post. +0
OS_dev commented: There are enough bad developers out there without you trying to make more. +0
holli.ween commented: 60 +0
ShawnCplus 456 Code Monkey Team Colleague

I'm going to have to go ahead and disagree with all of the code. Never use short tags in production. You can never know if the server you're going to move code to has short_tags enabled or not. It also makes it easier to separate code from HTML in templates.

Member Avatar for diafol
diafol

Nice tutorial, however, shouldn't we be using XHTML (lowercase tags) now?

peter_budo commented: Well spotted, guess not all are up to date ;) +14
death_oclock 103 Posting Whiz

In addition to the XHTML comment, tags need to be closed properly. And is this really the place for yet another PHP tutorial? There are some really great ones out there already.

khess 95 Practically a Master Poster

And there should only be one brand of gasoline, cracker, printer, car, etc.

This is an intro tutorial that is supposed to be placed in the tutorial section and not here. Sorry for the intrusion.

Member Avatar for diafol
diafol

Well, at least there's somewhere on this site that has tutorials now. Thanks khess.

jbennet 1,618 Most Valuable Poster Team Colleague Featured Poster

Before the tutorials section had lots of poor tutorials so now we ask they are posted here first, then you PM a moderator and if they like it, they will see about putting it in the Tutorials Section.

thoughtcoder 167 Junior Poster

You should learn modern HTML before trying to teach PHP.

Comatose commented: Taste It KHess! +12
SeanOBrian commented: good thought. clearly stated +0
wdbaker54 0 Newbie Poster

I like the minimalist PHP in this tutorial and I have to somewhat disagree with the posters who commented more on the HTML syntax than the php. It is a tutorial on PHP and not HTML. Someone who is learning PHP probably shouldn't be too nit picking there and focus on the subject at hand.
On the other hand - this might be just a bit TOO minimalist and part two needs a whole lot more substance. I am a perl programmer with a minimal familiarity with PHP and it took me only a few minutes to not only read the whole thing but to understand the basic concepts of embedded scripting. I wil check back and look at part two when it comes out.

ShawnCplus 456 Code Monkey Team Colleague

I like the minimalist PHP in this tutorial and I have to somewhat disagree with the posters who commented more on the HTML syntax than the php. It is a tutorial on PHP and not HTML. Someone who is learning PHP probably shouldn't be too nit picking there and focus on the subject at hand.
On the other hand - this might be just a bit TOO minimalist and part two needs a whole lot more substance. I am a perl programmer with a minimal familiarity with PHP and it took me only a few minutes to not only read the whole thing but to understand the basic concepts of embedded scripting. I wil check back and look at part two when it comes out.

Well most of the stuff isn't nitpicking, if you're just starting PHP it is VERY important to start off with the right concepts, syntax and practices.

SeanOBrian commented: good points nicely put +0
kambohg 0 Newbie Poster

can u clear own concept and can u make own logic in php this will b best thing for us i think basic is importent ok watting make own 1st logic then teach php plz

sameeraict 3 Newbie Poster

i m a begginer of php and i like the way this tutorial presents the lesson.it gives more details at the biggining , its very interesting.i like that.

jueric 0 Newbie Poster

Am very glad with the begginners tutorial with the php program..am really looking forward to part two of it.its surely interactive..if possible can i be sent the tutorial in my mail?

sameeraict 3 Newbie Poster

ok,i have some downloaded php tutorials i 'll send u them.

guru12 -2 Light Poster

Hello All
Well.Great Post , Thanks a lot , it is very much elaborated and all can easily follow up your tips.
Thank you for your posting

asivksoft 0 Newbie Poster

Programming in PHP is fun and easy. It is a very powerful scripting language that takes simple HTML and turns it into a fully-interactive experience for the web user. Let's get started!

Note: These tutorials assume that you have a working web server that's capable of displaying PHP code and that you have PHP installed on that web server system or that you're using a server that is equipped and ready.
You can run PHP on Windows. You can even use PHP with IIS (The Windows Web Server) so you don't necessarily need Unix/Linux and Apache.

PHP Reference

Your best reference for all things PHP is the PHP web site: www.php.net. Go there and bookmark it--you'll refer to it often.

PHP Speaks HTML

The first thing you have to learn and always remember is that if you use even one line of PHP code in an HTML file, you must name the file with a .php extension. The .php extension tells the web server that this file is special and needs special handling by the PHP executable and related libraries.

Delimit Your PHP

Second, all PHP functions and code must be inserted between the following delimiters

<? php code ?>

--the less than question mark opens the code and the question mark greater than close. You must always use delimiters in pairs--an opening delimiter and closing delimiter. Delimiters are symbols that tell the web server what kind of content follows and how it should be handled. HTML uses delimiters too

<TAG>

.

Example 1: How About a Date?

If you want to add the current date to a web page, the code would look like:

<HTML><BODY>
<? echo date("M-d-Y"); ?>
</BODY></HTML>

Explanation: The 'echo' command tells the PHP program to echo the results that follow to the screen. Since you're using HTML and the browser is your "screen", that's where you see the results. The date function is a built-in PHP program that display the date in the format you specify in the parentheses.

Save the file with the filename, test.php.

Open the file by pointing your browser to the file on the webserver and you should see:

Jan-19-2009 (you'll see the current date)

Try changing the date format by moving things around ("d-M-Y") or just using one of the date variables ("Y"). Refer to the PHP documentation to get a full range of date variables. It's fun and useful.

The full list of PHP date-related functions are here.

Example 2: Who's Looking at You, Kid?

If you want to gather some info about the people that are looking at your site or if you just want to let them know you're watching, you can easily echo some of their information back to them by using what's known as Server Variables.

Note: This is not personal information, nor are you stealing anything from them. It is information that is transferred to your website when a visitor connects to your webpages.

You can get their IP Address and their Browser and Computer information. The IP Address is how computers communicate with each other on the Internet--think of it as kind of like a phone number or home address.

To see this information, edit your test.php file again and enter the following:

<HTML><BODY>
<? echo date("M-d-Y");
echo "<P>";
echo "Your IP Address is " . $_SERVER['REMOTE_ADDR'];
?>
</BODY></HTML>

Explanation: The line

echo "<P>";

is HTML for start a new Paragraph. The next line,

echo "Your IP Address is " . $_SERVER['REMOTE_ADDR'];

sends the text, "Your IP Address is " to your browser. The dot (.) is PHP for concatenate or connect these two things. $SERVER is a built-in Server variable that echoes the viewing browser's IP Address to their browser.

Save this file and point your browser to test.php.

You should see:

Jan-19-2009 (Current Date)

Your IP Address is 10.0.1.200 (The IP Address of Your Computer)

There are several variables from which to choose here. The complete list is located at: PHP:$_SERVER.

Change the Server variable or add new ones. For starters, try: HTTP_USER_AGENT to see your browser and computer information.

Stay tuned for PHP Tutorial Part Two: Combining HTML and PHP where you'll get a taste of how to integrate these two languages in a more elegant fashion. Don't worry, it's easy and you'll like the way it works. It's easier to read and more fun to write.

insert the paragraph

asivksoft 0 Newbie Poster

about IF condition

chowdary51 0 Newbie Poster

nice tutorial

SeanOBrian 0 Newbie Poster

I have to chime in with those questioning the usefulness of a tutorial by someone who is so out of touch with Web development as to present such archaic HTML. Disappointingly bad form; if the basics are shaky, why would a beginner trust such material?

OS_dev 0 Junior Poster

This is a joke right?

Wow, a tutorial with bad html, short php tags and worst of all starts with echo and then about 10 paragraphs down ends with superglobals. Umm, ya, I'd say you skipped around a little bit.

and these quotes just tickled me:
"It is a very powerful scripting language that takes simple HTML and turns it into a fully-interactive experience for the web user" - what does that mean?

"have a working web server that's capable of displaying PHP code" - Umm, ya, if your server is displaying php code, it's not a working web server

"You can get their IP Address and their Browser and Computer information." - how's that work again?

"Stay tuned for PHP Tutorial Part Two: Combining HTML and PHP" - Oh, I can't wait

Member Avatar for diafol
diafol

This guy's had his fair share of knocks. Maybe khess would like to rewrite some of the original code and re-post? The sentiment was right - he posted some well-meaning material during a time when there was very little "basic/beginner stuff" in the code repository.

How's the old saying go, "Publish and be damned"? Ouch!

Banderson 29 Junior Poster in Training

I always like beginner tutorials, a nice refresher never hurts. This tutorial is not bad, but it is rather outdated, at this point. Most of this code would work for PHP Version 4 **,
but it will not work for Versions 5 =>

You must use

<?php  ?>

for Versions 5 =>

This being said if you are still using PHP 4 **, you probably need to consider migrating to Versions 5 => for updated security on your server.

Now on a brighter note, Thank you Ken Hess for another contribution to the community, it is much appreciated!

colweb 13 Posting Whiz

If you want to learn PHP please don't read this tutorial. Buy a good book or read some good books that are published online.

Member Avatar for diafol
diafol

I think we get the message by now. Poor khess.

jhbalaji 0 Light Poster

Nice for Beginners like me :)

Member Avatar for diafol
diafol

Perhaps this thread should be closed?

jen06 0 Newbie Poster

Nice tutorial. This may help me a lot in starting PHP language. Thank you.

zimah 0 Newbie Poster

can u give me coding php about select from database..please..reply msg

Member Avatar for diafol
diafol

start a new thread zimah - or even better - use Google to search for an answer. That request is seriously poor.

SELECT field1, field2 FROM table1

RAICK 0 Newbie Poster

is there any complete step by step tuttorial of PHP or HTML ?
like this one too very helpfull for beginers Thanks for Ken Hess.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.