PHP for beginners – your first PHP program
Mar29Written by:
2010/03/29 08:55 PM
We’ve started this tutorial series for beginners who want to be programmers. We all have to start someplace. Why not start at the beginning then?
There are a lot of tutorials out there on the internet, why start another one? Well why not? But I want to help all and everyone who is struggling with programming. Whether your a beginner programmer, or just a blogger keen on doing something special with your Wordpress blog. Then it’s good to know the basics.
If you have followed the two introductory series, then you would have had PHP and a web server set up. As well as chosen your editor or PHP IDE of choice. So lets get started
Test if PHP is installed correctly
If you have installed PHP, there is an easy way to test. Using your favourite editor or IDE create a program called phptest.php. Then enter the following into the new file:
<?php phpinfo(); ?>
Save then run from your browser by entering either http://127.0.0.1/phptest.php, or http://localhost/phptest.php.
You should then see a whole lot of PHP related configuration settings in a nice table. You now know your php and web server have been installed and working correctly.
If not re-read your installation instructions and install again.
Your First Program.
PHP is really simple. As a scripting language, it can be embedded into any HTML page. Just as JavaScript can. With a special tag, you can embed any PHP code into your HTML page.
PHP also runs independently, but we will cover that at a later stage.
First create a file named MyFirst.php, or what ever you want to call it, and put it in your web server's root directory (DOCUMENT_ROOT or WWWRoot), depending on which web server you have set up.
When a file with ".php" extension is placed in the server and the corresponding url is visited, the server will process the file before sending it to the client, so that all PHP commands will be processed.
Now a regular html page might look like this:
<html>
<head>
<title>This is my page</title>
</head>
<body>
This is my first PHP app.
</body>
</html>
Not much happening. But lets change this and insert a php tag with some PHP code.
<html>
<head>
<title>This is my page</title>
</head>
<body>
<?php
print "This is my first php app. Really it is.";
?>
</body>
</html>
When visiting the page, the server will process the page to execute all PHP commands in the page, which will be located within <?php opening tag and ?> closing tags. In our case "print" command will write to the resulting page the text contained between the quotes. Often, instead of “print” command, “echo” command is used to output string. Using the “echo” command results in identical output
You can print out any legit HTML code. Ever wonder how Wordpress is done. How they are able to construct pages with PHP. Simply put, developers just output HTML code. Well, Wordpress is a little more complicated than that.
So, lets add some HTML to our php application above. In your editor change your php file to include h3 tags:
<html>
<head>
<title>
This is my page</title>
</head>
<body>
<?php
print "<h3>My PHP App</h3>";
print "This is my first php app. Really it is.";
?>
</body>
</html>
Save and run the php file through your browser. Works great doesn’t it?
So theoretically you could embed a whole HTML page around php tags and it will effectively be a dynamic php application. But who wants static php pages.
Later on in the tutorial we will learn how to use variables and make decisions. Making our application truly dynamic!
Remember you can win a weeks worth of free ad space just for commenting. The best comments during the week will get a weeks free advertising space. Check out: Win free advertising for your blog
New here, or perhaps you've been here a few times? Like this post? Why not subscribe to this blog and get the most up to date posts as soon as they are published.
Get involved in our community. Help promote other bloggers. List your blog in our directory for bloggers. Blog Directory for Bloggers
blog comments powered by