The Brave Programmer - Blogging and coding
Not for the faint hearted
 

Blog Posts From The Brave Programmer

Minimize

PHP for beginners – your first PHP program

Mar 29

Written by:
2010/03/29 08:55 PM  RssIcon

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



Tags:
Categories:
blog comments powered by Disqus
 
Blog Updates Via E-mail
  Blog Updates Via E-mail
Minimize

Do you want to receive blog updates via e-mail. Then just click on the link below. You will be redirected to Google's feed burner, where you can fill out a form. Supplying your e-mail address.

The subscription is managed entirely by Google's Feedburner. We cannot and do not collect your email address.

Subscribe to The Brave Programmer by Email

Print  
 

 

Latest Comments
  Latest Comments
Minimize
Powered by Disqus

Sign up with Disqus to enjoy a  surprise box of features

Print  
 
Blog Roll
  Blog Roll
Minimize
Print  
 
Categories
  Categories
Minimize
Print  
 
<h1>Search Blogs From The Brave Programmer</h1>
 

Search Blogs From The Brave Programmer

Minimize
Print  
 
Archive
  Archive
Minimize
Archive
<April 2024>
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
Monthly
Go
Print  
 
<h1>News Feeds (RSS)</h1>
 

News Feeds (RSS)

Minimize
Print  
 

Follow robertbravery on Twitter

Blog Engage Blog Forum and Blogging Community, Free Blog Submissions and Blog Traffic, Blog Directory, Article Submissions, Blog Traffic

View Robert Bravery's profile on LinkedIn

Mybyte

 

Robert - Find me on Bloggers.com

Tags
  Tags
Minimize
Print  
 
Contact Us Now
  Contact Us Now
Minimize
 

Email  us now or call us on 082-413-1420,  to host your website.

We design and develop websites. We develop websites that make a difference. We do Dotnetnuke Module development.

Web Masters Around The World
Power By Ringsurf
Print