PHPMan
New Member
:: PHP Tutorial ::
Introduction
Let us begin our journey into the simple yet powerful world of PHP by understanding what the term "Hypertext Pre-processor" means. Hypertext is nothing but what we commonly call "link", those strange words that take you to different places when the mouse is clicked on them. In other words Hypertext stands simply for a plain web page. Yes, I know that techi people have this queer habit of making simple things high-sounding. The term pre-processor means exactly what it says, it means that which processes before. Thus PHP processes web pages before they are sent to the browser. So a PHP page is processed first on the web-server that creates an HTML which is sent to the client, a web-browser in most cases.
I think that's already quite heavy on the newbies, let's now see who fit the eligibility criterion for this tutorial. The only basic requirement is the elementary knowledge of HTML. If you are among those who is not familiar with HTML you can read the HTML tutorial before venturing into this one. An introduction to any kind of programming would certainly help. If you are a seasoned web developer using other technologies then just read through this tutorial and with the help of the PHP manual you can add PHP into your resume.
For trying out the examples and code snippets in the following pages you will need PHP and MySQL.
In the next section we shall look a little at the history of PHP and how it compares to the other solutions out there.
And The Rest is History...
Haven't we all heard that phrase so often, well its true for anything great and PHP is no exception to that rule. PHP was created in 1994 by Rasmus Lerdorf to keep track of who was looking at his online resume. In 1995 the first version of PHP was released and was known as "Personal Home Page Tools" From then on PHP has grown at an enormous pace, various people have contributed to it. You can read a more detailed history of PHP in the manual or here.In a very short span of 2 years PHP became among the most widely used languages for web development. And yes, its usage is still growing at a staggering rate. There is an active community that discusses the possibilities and problems with PHP and a group of core developers that works on improving PHP and fixing bugs. You can join the PHP newsgroups at www.php.net.
Php Vs Other Technologies
There are numerous technologies available for the aspiring developer other than PHP that serve the same function as PHP, for example ASP, JSP, Perl, ColdFusion to name a few. Let me tell you that they are all good and usually its a matter of preference and the cost involved that urges the developer to pick one over the other. Opinions regarding this may vary but most PHP developers believe that PHP is easier to learn and has a superior syntax than the other languages. Additionally PHP is fast and safe. PHP is cross platform, it runs on almost anything that exists out there, naming all that PHP runs on would probably take a whole paragraph, that's why I'm omitting it. It accesses almost every existing database. And on top of all this it's FREE!! Do I need to give you more reasons why PHP has grown and has been used the way it has?
Now that we have seen what the options are and have decided to go the PHP way, let the games begin...
Let The Games Begin...
I know that you are itching to get started with hands on PHP, it has been a rather long wait but now get ready to launch yourself in the world of dynamic web-pages. When I say dynamic I mean dynamic as opposed to static; don't know the difference, well it will become clear in the next five minutes. Let us begin with two simple examples that will show us how PHP is embedded into HTML and how easy it is to create simple PHP pages. Type the following code in your favorite text editor and name the file "first.php" (remember to put quotes while naming the file otherwise it may become first.php.txt)
<html>
<head>
<title>My first PHP page</title>
</head>
<body>
<?php
$var = "This is my first PHP page";
echo $var;
?>
</body>
</html>
Here is what first.php should look like
As you can see above PHP code is put between the opening tag "<?php" and the closing tag "?>" All thats in between these two is PHP and is processed by the web-server. You will also notice that all PHP statements end with a semi-colon. Here, I should mention that in PHP we comment single line text with a double backslash "//" and multiline comment using "/*" and "*/". This will be demonstrated in the next example. Variables in PHP are always prefixed with a "$". We shall look at variables in more detail later. Now let us move on to our first page that has true dynamic content. Type in the following and name the file "greeting.php".
<html>
<head>
<title>good ......</title>
</head>
<body>
<?php
$hour = date("H");
if ($hour <= 11) // check if the time is before 11:00 hrs
{
echo "good morning my friend";
}
elseif ($hour > 11 && $hour < 18)
{
echo "good afternoon my friend";
}
else
{
echo "good evening my friend";
}
/* This is a multi-line comment,
it can span over more than one line,
and go on and
on till we come to -> */
?>
</body>
</html>
This page is truly dynamic because its results will change with the time of the day on the web-server. If the source of the output page is viewed, one sees very simple HTML code. This is because the web-server processes the PHP code before creating and sending a simple output HTML file to the browser (remember "hypertext pre-processor" well, after all its not all that stupid!!). The above script uses the PHP function date() to get the hour of the day in a 24 hour format then checks what time of the day it is and then outputs the relevant comment. It uses two of the most important concepts in programming that of variables and that of control structures. In the following page we shall have a look at these.
Variables are forever...
Maybe you are not of that opinion, but the moment you get into any kind of programming you'll see what I mean. A variable is an area of memory that is set aside to store data. In PHP you can recognize variables easily because they are prefixed with the dollar "$" sign. Variables are assigned using the assignment operator "=". Here is how it is done :
$name = "Harry Potter";
$pagenumber = 4;
Here we see that we have a variable identifier called $name and the string value "Harry Potter" has been assigned to it. Then we assign the value 4 to the variable $pagenumber. Note that we do not surround the value 4 within quotes so that PHP treats $pagenumber as a numeric value instead of a string which happens when we put quotes. Now that we have seen how variables are assigned values, let us look at the different types of variable that PHP allows :
String
Integer
Floating-point numbers
Array
Object
A discussion about these different types is beyond the scope of this tutorial. A detailed description and usage of each can be found in the PHP manual. Take this form me, if you want to be a good programmer then you must learn to read manuals. We shall be using variables all through this tutorial and looking at the following examples I'm sure you will get a hang of how variables are used, their function and various other important things that come by hands on coding. Now let use move on to control structures, after you have read this little bit, we shall study how PHP interacts with HTML forms and then you will have enough knowledge to create a simple game. Doesn't that sound like FUN!! Want to get there, just read how logic is implemented in programming.
NOTE : variables are case sensitive in PHP so $name is not the same as $NAME or $Name.
More..........
------------------------------------------------------
Credit - Techi WareHouse
Introduction
Let us begin our journey into the simple yet powerful world of PHP by understanding what the term "Hypertext Pre-processor" means. Hypertext is nothing but what we commonly call "link", those strange words that take you to different places when the mouse is clicked on them. In other words Hypertext stands simply for a plain web page. Yes, I know that techi people have this queer habit of making simple things high-sounding. The term pre-processor means exactly what it says, it means that which processes before. Thus PHP processes web pages before they are sent to the browser. So a PHP page is processed first on the web-server that creates an HTML which is sent to the client, a web-browser in most cases.
I think that's already quite heavy on the newbies, let's now see who fit the eligibility criterion for this tutorial. The only basic requirement is the elementary knowledge of HTML. If you are among those who is not familiar with HTML you can read the HTML tutorial before venturing into this one. An introduction to any kind of programming would certainly help. If you are a seasoned web developer using other technologies then just read through this tutorial and with the help of the PHP manual you can add PHP into your resume.
For trying out the examples and code snippets in the following pages you will need PHP and MySQL.
In the next section we shall look a little at the history of PHP and how it compares to the other solutions out there.
And The Rest is History...
Haven't we all heard that phrase so often, well its true for anything great and PHP is no exception to that rule. PHP was created in 1994 by Rasmus Lerdorf to keep track of who was looking at his online resume. In 1995 the first version of PHP was released and was known as "Personal Home Page Tools" From then on PHP has grown at an enormous pace, various people have contributed to it. You can read a more detailed history of PHP in the manual or here.In a very short span of 2 years PHP became among the most widely used languages for web development. And yes, its usage is still growing at a staggering rate. There is an active community that discusses the possibilities and problems with PHP and a group of core developers that works on improving PHP and fixing bugs. You can join the PHP newsgroups at www.php.net.
Php Vs Other Technologies
There are numerous technologies available for the aspiring developer other than PHP that serve the same function as PHP, for example ASP, JSP, Perl, ColdFusion to name a few. Let me tell you that they are all good and usually its a matter of preference and the cost involved that urges the developer to pick one over the other. Opinions regarding this may vary but most PHP developers believe that PHP is easier to learn and has a superior syntax than the other languages. Additionally PHP is fast and safe. PHP is cross platform, it runs on almost anything that exists out there, naming all that PHP runs on would probably take a whole paragraph, that's why I'm omitting it. It accesses almost every existing database. And on top of all this it's FREE!! Do I need to give you more reasons why PHP has grown and has been used the way it has?
Now that we have seen what the options are and have decided to go the PHP way, let the games begin...
Let The Games Begin...
I know that you are itching to get started with hands on PHP, it has been a rather long wait but now get ready to launch yourself in the world of dynamic web-pages. When I say dynamic I mean dynamic as opposed to static; don't know the difference, well it will become clear in the next five minutes. Let us begin with two simple examples that will show us how PHP is embedded into HTML and how easy it is to create simple PHP pages. Type the following code in your favorite text editor and name the file "first.php" (remember to put quotes while naming the file otherwise it may become first.php.txt)
<html>
<head>
<title>My first PHP page</title>
</head>
<body>
<?php
$var = "This is my first PHP page";
echo $var;
?>
</body>
</html>
Here is what first.php should look like
As you can see above PHP code is put between the opening tag "<?php" and the closing tag "?>" All thats in between these two is PHP and is processed by the web-server. You will also notice that all PHP statements end with a semi-colon. Here, I should mention that in PHP we comment single line text with a double backslash "//" and multiline comment using "/*" and "*/". This will be demonstrated in the next example. Variables in PHP are always prefixed with a "$". We shall look at variables in more detail later. Now let us move on to our first page that has true dynamic content. Type in the following and name the file "greeting.php".
<html>
<head>
<title>good ......</title>
</head>
<body>
<?php
$hour = date("H");
if ($hour <= 11) // check if the time is before 11:00 hrs
{
echo "good morning my friend";
}
elseif ($hour > 11 && $hour < 18)
{
echo "good afternoon my friend";
}
else
{
echo "good evening my friend";
}
/* This is a multi-line comment,
it can span over more than one line,
and go on and
on till we come to -> */
?>
</body>
</html>
This page is truly dynamic because its results will change with the time of the day on the web-server. If the source of the output page is viewed, one sees very simple HTML code. This is because the web-server processes the PHP code before creating and sending a simple output HTML file to the browser (remember "hypertext pre-processor" well, after all its not all that stupid!!). The above script uses the PHP function date() to get the hour of the day in a 24 hour format then checks what time of the day it is and then outputs the relevant comment. It uses two of the most important concepts in programming that of variables and that of control structures. In the following page we shall have a look at these.
Variables are forever...
Maybe you are not of that opinion, but the moment you get into any kind of programming you'll see what I mean. A variable is an area of memory that is set aside to store data. In PHP you can recognize variables easily because they are prefixed with the dollar "$" sign. Variables are assigned using the assignment operator "=". Here is how it is done :
$name = "Harry Potter";
$pagenumber = 4;
Here we see that we have a variable identifier called $name and the string value "Harry Potter" has been assigned to it. Then we assign the value 4 to the variable $pagenumber. Note that we do not surround the value 4 within quotes so that PHP treats $pagenumber as a numeric value instead of a string which happens when we put quotes. Now that we have seen how variables are assigned values, let us look at the different types of variable that PHP allows :
String
Integer
Floating-point numbers
Array
Object
A discussion about these different types is beyond the scope of this tutorial. A detailed description and usage of each can be found in the PHP manual. Take this form me, if you want to be a good programmer then you must learn to read manuals. We shall be using variables all through this tutorial and looking at the following examples I'm sure you will get a hang of how variables are used, their function and various other important things that come by hands on coding. Now let use move on to control structures, after you have read this little bit, we shall study how PHP interacts with HTML forms and then you will have enough knowledge to create a simple game. Doesn't that sound like FUN!! Want to get there, just read how logic is implemented in programming.
NOTE : variables are case sensitive in PHP so $name is not the same as $NAME or $Name.
More..........
------------------------------------------------------
Credit - Techi WareHouse