« How to Cache results from a php script | Main | How to hide a toolbar in Firefox »
start programming with PHP
By maurizio | May 14, 2007
If you are new here, you may want to subscribe to my RSS feed. Feel free to leave comments and questions too.Thanks for visiting!
Many of you (1) asked me what is PHP. From Wikipedia:
PHP (PHP:Hypertext Preprocessor) is a [...] programming language originally designed for producing dynamic web pages.
Php is mostly used as a server-side language (which means it runs on a web server).It’s primary use is to generate Html pages that can be seen with browsers (Firefox, Internet Explorer, etc). You can, for example, sum 2 numbers and print out the result in an html page. The Php file has to be put on a web server in order to let the web server execute the code and generate the results (usually on a html page).
Like with every computer language, you can do a lot of stuff, given you have the information you need.
<?php
$result = 1 + 3;
echo $result;
?>
With this simple piece of code you sum 1 to 3 and then print the result.
What I would like you to know is not the code rules or the structure of the language. What I would like you to know, my fellow reader(s), is the power you can have with a server-side language. You just have to think about some idea you would like to create. For example, I wanted to read automatically my Favorites from Technorati and then check who Favorited my Blog (I will show the code sooner or later). This is the idea; the most important part of a program.
To create a program, you should be able to dumb down whatever you think. In my example, I just have to think it this way:
Read my Favorites –> read the Html page containing my favorites and then find out how to recognize single favorites –> Favorites are recognizable because they are all html links inside a specific div (< div class=”photos”>)
As you can see, I’m not specifying the code itself. It is not important (yet). Now that you understood the concept, you simply have to write in code. You don’t need to know php yet. What you need to know is that programming languages give you the power of the loop :-). A loop is simply a piece of code that is repeated several times.
If your head isn’t exploded yet, maybe it’s time to take a break. Go read Paula’s blog or just take a nap. Then come back and read on. :-)
Ok, now that your mind is fresh, let’s go.
As you probably imagine now, the basic operation I described above should be “executed” several times, for every Favorite I find on my page. So a loop is handy
find every (< div class="photos") and look for a link after each of them
becomes
do:
If you find (< div class="photos")
then find a link,
otherwise
stop.
Do it again until I stop you.
If you understand this last piece you are starting to understand how to program. What it’s doing is simple; you continue to do (or read) that code until you stop. Given the following piece of html page:
< div class="photos">
<a href="a.html">
</div>
< div class="photos">
<a href="b.html">
</div>
Now, if you read this html with my code, you should do the following:
[start program, start reading html from the beginning]
If I find (<div class=”photos”) ..let me see, yes! found it! what shall I do next? Find a link Found. Then? Do it again until I stop you. Ok, go back to the beginning.Now I go back to the beginning of the loop, but I still go on reading the html page from where I finished, so I do again the same things and I find the second <div and the second link. When I go back again I don’t find the <div so “otherwise stop“.
Are you starting to understand?
$IhaveTo = true;
while ($IhaveTo) {
$position = positionOfString($htmlPage,'<div class="photos"',$position);
if ($position === false) {
$IhaveTo = false;
} else {
findTheLink();
}
}
Now it’s getting more complicated..I think I’ll stop here for now. Just some quick notes:
- {} are mandatory. The help the computer understand where it should stop to read. With those symbols you (and your computer) understand where the while loop starts and ends. If you undestand this basic rule and you speak english, it shouldn’t be difficult to understand every piece of php code.
- $IhaveTo, like every other words starting with a $ is a variable. A variable is a place where you store information. $IhaveTo stores the information true/false, so the whole piece of code inside while{ } is “read” only if it’s set to true.
- every words followed by () or (sometext) is a function. A function is simply another piece of code that you separated from your main piece of code. You should do that if you use some piece of code and use it several times. On myTechnorati Favorites Tool explanation I show a function called getPage($url). You can put this function on the code above so it gets called every time the computer “read” the IF statement.
- Note the ; at the end of every line. Well, not really every line, because lines that end with { don’t need that.
- As I said before, if you want to do some test, you have to create a text file that end .php instead of .txt, save it on your webspace and then open it with your preferred browser (which should be Firefox)
Topics: Programming |
Read other related posts:


May 14th, 2007 at 6:45 am
Hi Maurizio -
Please forgive me for taking so long to respond.
I kinda took the weekend off to spend with my lovely loved ones and now I’m back, responding to all my lovely emails and comments.
I’ll be back,
Paula
May 14th, 2007 at 6:57 am
Don’t worry Paula, I will always forgive my dearest reader. As long as you read me.. :-)
Do you like this post? I hope to get more readers like you with it.
May 14th, 2007 at 8:43 am
[...] Maruzio of Nafurai [...]
June 11th, 2007 at 8:09 am
[...] start programming with PHP [...]
August 23rd, 2007 at 2:50 am
[...] his programming skills, but surely reading the blog will improve your ones. You can find posts like start programming with PHP (the language WP is written in) that will introduce you into programming, or Busy Day.. aka get [...]