PhP Tutorial
Hypertext Pre-processor: What is PHP?
When you type a URL in the Address box or click a link on a Web page, you're asking a Web server on a computer somewhere to send a file to the Web browser (sometimes called a "client") on your computer. If that file is a normal HTML file, it looks exactly the same when your Web browser receives it as it did before the Web server sent it. After receiving the file, your Web browser displays its contents as a combination of text, images, and sounds. In the case of an PHP page, the process is similar, except there's an extra processing step that takes place just before the Web server sends the file. Before the Web server sends the HTML file to the Web browser, it runs all server-side scripts contained in the page. Some of these scripts display the current date, time, and other information. Others process information the user has just typed into a form, such as a page in the Web site's guestbook To distinguish them from normal HTML pages, PHP files are usually given the ".php" extension.
Creating my first PHP file
In this page we will add PHP scripting languaje codes to our regular
HTML
page, we will learn how to write texts and how to concatenete
them and write some special characters, and
we
will start using variables.
In our fisrt step to learn PHP scripting languaje we will modify a regular HTML page and we will add to it PHP commads.
Bellow we will find a regular HTML page:
To create a PHP page using the page above as our model, the first important action is to rename our file. In our case we will name the file "mypage.php". 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. We may just change the name of our file as propoused and save it to our server (without any additional changes), and when visiting the corresponding URL the result will be exactly the same (because there is nothing to be processed in the page) .
To this renamed page ("mypage.php") we will add a small PHP command within <?php opening tag and ?> closing tag.
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 brakets ("Do you like it?"). Often, instead of print command, echo command is used to output string. At the end of the line we will add ";" (otherway we will get an error). We may use "echo" command instead of "print", and the result will be identical.
We may also include all the php commands in one line by placing the code bellow in our page.
Between opening and closing tags we may include several lines with executable commands as shown bellow (we must write ";" at the end of each line):
Additionaly we may add several opening and closing tags within our page with executable commands.
As sown in the example above we may add PHP scripts within HTML tags.
Concatenation of strings
Check the codes bellow:
The pages containing any of the codes above will be exactly the same. We have just put both lines in one, and to do that we have use a point between the two texts we want to show in our pages. The point used in Code 2 will concatenated both strings.
Writing some special characters
Check the codes bellow:
Using variables: first step
With PHP we may also use variables. The variables used in PHP will always start with "$", as for example $i, $a, $mydata, $my_name etc. There are some words which we are not allow to use as a name for a variable, but as we probably do not know which ones are they, our best decission will be to use very descriptive variable names:
In the example bellow we have write a PHP page using some variables:
In this example, we have defined three variables (line 2, 3 and 4) and we have included the information in the variable latter within the HTML code.
Check those variables carefully: $my_name, $my_hello_text and $year_create has not been defined in advance (we have add a value to the variables the first time they have used them in the file, without letting know the program we will used it in advance), and the values do not contain the same kind of content:
We may also use variables in the following ways:
Creating my first PHP file
In this page we will add PHP scripting languaje codes to our regular
HTML
page, we will learn how to write texts and how to concatenete
them and write some special characters, and
we
will start using variables.
In our fisrt step to learn PHP scripting languaje we will modify a regular HTML page and we will add to it PHP commads.
Bellow we will find a regular HTML page:
To create a PHP page using the page above as our model, the first important action is to rename our file. In our case we will name the file "mypage.php". 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. We may just change the name of our file as propoused and save it to our server (without any additional changes), and when visiting the corresponding URL the result will be exactly the same (because there is nothing to be processed in the page) .
To this renamed page ("mypage.php") we will add a small PHP command within <?php opening tag and ?> closing tag.
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 brakets ("Do you like it?"). Often, instead of print command, echo command is used to output string. At the end of the line we will add ";" (otherway we will get an error). We may use "echo" command instead of "print", and the result will be identical.
We may also include all the php commands in one line by placing the code bellow in our page.
Between opening and closing tags we may include several lines with executable commands as shown bellow (we must write ";" at the end of each line):
Additionaly we may add several opening and closing tags within our page with executable commands.
As sown in the example above we may add PHP scripts within HTML tags.
Concatenation of strings
Check the codes bellow:
The pages containing any of the codes above will be exactly the same. We have just put both lines in one, and to do that we have use a point between the two texts we want to show in our pages. The point used in Code 2 will concatenated both strings.
Writing some special characters
Check the codes bellow:
Using variables: first step
With PHP we may also use variables. The variables used in PHP will always start with "$", as for example $i, $a, $mydata, $my_name etc. There are some words which we are not allow to use as a name for a variable, but as we probably do not know which ones are they, our best decission will be to use very descriptive variable names:
In the example bellow we have write a PHP page using some variables:
In this example, we have defined three variables (line 2, 3 and 4) and we have included the information in the variable latter within the HTML code.
Check those variables carefully: $my_name, $my_hello_text and $year_create has not been defined in advance (we have add a value to the variables the first time they have used them in the file, without letting know the program we will used it in advance), and the values do not contain the same kind of content:
We may also use variables in the following ways:
What is PHP?
Hypertext Pre-processor (PHPs) is a server-side scripting language, and server-sidescripts are special commands you must place in Web pages. Those commands are processed before the pages are sent from your Server to the Web browser of your visitor. A typical PHP files will content commads to be executed in the server in addition to the usual mixture of text and HTML (Hypertext Markup Language) tags.When you type a URL in the Address box or click a link on a Web page, you're asking a Web server on a computer somewhere to send a file to the Web browser (sometimes called a "client") on your computer. If that file is a normal HTML file, it looks exactly the same when your Web browser receives it as it did before the Web server sent it. After receiving the file, your Web browser displays its contents as a combination of text, images, and sounds. In the case of an PHP page, the process is similar, except there's an extra processing step that takes place just before the Web server sends the file. Before the Web server sends the HTML file to the Web browser, it runs all server-side scripts contained in the page. Some of these scripts display the current date, time, and other information. Others process information the user has just typed into a form, such as a page in the Web site's guestbook To distinguish them from normal HTML pages, PHP files are usually given the ".php" extension.
What Can You Do with PHP?
There are many things you can do with PHP.- You can display date, time, and other information in different ways.
- You can make a survey form and ask people who visit your site to fill it out, send emails, save the information to a file, etc
What Do PHP pages Look Like?
The appearance of an PHP page depends on who or what is viewing it. To the Web browser that receives it, an Active Server Page looks just like a normal HTML page. If a visitor to your Web site views the source code of an PHP page, that's what they see: a normal HTML page. However, the file located in the server looks very different. In addition to text and HTML tags, you also see server-side scripts. This is what the PHP page looks like to the Web server before it is processed and sent in response to a request.What Do PHP pages Look Like?
Server-side scripts look a lot like HTML tags. However, instead of starting and ending with lesser-than ( < ) and greater-than ( > ) brackets, they typically start with <?php or <? and will typically end with ?>. The <?php or <? are called anopening tags, and the ?> is called a closing tag. In between these tags are the server-side scripts. You can insert server-side scripts anywhere in your Web page--even inside HTML tags.Do You Have to Be a Programmer to Understand Server-Side Scripting?
There's a lot you can do with server-side scripts without learning how to program. For this reason, much of the online Help for PHP is written for people who are familiar with HTML but aren't computer programmers.Creating my first PHP file
In our fisrt step to learn PHP scripting languaje we will modify a regular HTML page and we will add to it PHP commads.
Bellow we will find a regular HTML page:
| mypage.html |
| <html> <head> <title>This is my page</title> </head> <body> This is the content of my page. </body> </html> |
To create a PHP page using the page above as our model, the first important action is to rename our file. In our case we will name the file "mypage.php". 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. We may just change the name of our file as propoused and save it to our server (without any additional changes), and when visiting the corresponding URL the result will be exactly the same (because there is nothing to be processed in the page) .
To this renamed page ("mypage.php") we will add a small PHP command within <?php opening tag and ?> closing tag.
| mypage.php |
| <html> <head> <title>This is my page</title> </head> <body> This is the content of my page. <?php print "Do you like it?"; ?> </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 brakets ("Do you like it?"). Often, instead of print command, echo command is used to output string. At the end of the line we will add ";" (otherway we will get an error). We may use "echo" command instead of "print", and the result will be identical.
We may also include all the php commands in one line by placing the code bellow in our page.
<?php print "Do
you like it?"; ?>
Between opening and closing tags we may include several lines with executable commands as shown bellow (we must write ";" at the end of each line):
| mypage.php |
| <html> <head> <title>This is my page</title> </head> <body> This is the content of my page. <?php print "Do you like it?"; print "<br>"; print "<b>If not, please visit a different page.</b>"; print "<br>"; print "<h1>Goodbay</h1>"; ?> </body> </html> |
Additionaly we may add several opening and closing tags within our page with executable commands.
| mypage.php |
| <html> <head> <title>This is my page</title> </head> <body <?php print "bgcolor=#FFFFFF"; ?>> <?php print "Hello!<br>"; ?> This is the content of my page. <?php print "Do you like it?"; print "<br>"; print "<b>If not, please visit a different page.</b>"; print "<br>"; print "<h1>Goodbay</h1>"; ?> </body> </html> |
As sown in the example above we may add PHP scripts within HTML tags.
Concatenation of strings
Check the codes bellow:
| Code 1 |
| <?php print "Hello!<br>"; print "I am Joe"; ?> |
| Code 2 |
| <?php print "Hello!<br>"."I am Joe"; ?> |
The pages containing any of the codes above will be exactly the same. We have just put both lines in one, and to do that we have use a point between the two texts we want to show in our pages. The point used in Code 2 will concatenated both strings.
Writing some special characters
Check the codes bellow:
| The code |
The output |
| Use "\n" to add a line break <pre> <?php print "AA\nBB\nCC\nDD\nEE"; ?> </pre> |
Use "\n" to add a line break
AA BB CC DD EE |
| Use "\"" to add brakets<br> <?php print "He said \"Hello\" to John"; ?> |
Use "\"" to add brakets He said "Hello" to John |
Using variables: first step
With PHP we may also use variables. The variables used in PHP will always start with "$", as for example $i, $a, $mydata, $my_name etc. There are some words which we are not allow to use as a name for a variable, but as we probably do not know which ones are they, our best decission will be to use very descriptive variable names:
| $my_favorite_song $my_counter $username etc |
In the example bellow we have write a PHP page using some variables:
| mypage.php |
| <?php $my_name="Joe"; $my_hello_text="Hello everybody!"; $year_create=2002; ?> <html> <head> <title>This is <?php print $my_name; ?>´spage</title> </head> <body> <?php print $my_hello_text; ?> This page was written in <?php print $year_create; ?>. </body> </html> |
In this example, we have defined three variables (line 2, 3 and 4) and we have included the information in the variable latter within the HTML code.
Check those variables carefully: $my_name, $my_hello_text and $year_create has not been defined in advance (we have add a value to the variables the first time they have used them in the file, without letting know the program we will used it in advance), and the values do not contain the same kind of content:
| $my_name, $my_hello_text
are strings $year_create is a number |
We may also use variables in the following ways:
| The code |
The output |
| <?php $year_create=2002; print "This page was written in ".$year_create; ?> |
This page was written in 2002 |
| <?php $year_create=2002; print "This page was written in $year_create"; ?> |
This page was written in 2002 |
| <?php $the_text="This page was written in "; $year_create=2002; print $the_text.$year_create; ?> |
This page was written in 2002 |
| <?php $the_text="This page was written in "; $year_create=2002; print "$the_text$year_create"; ?> |
This page was written in 2002 |
Creating my first PHP file
In our fisrt step to learn PHP scripting languaje we will modify a regular HTML page and we will add to it PHP commads.
Bellow we will find a regular HTML page:
| mypage.html |
| <html> <head> <title>This is my page</title> </head> <body> This is the content of my page. </body> </html> |
To create a PHP page using the page above as our model, the first important action is to rename our file. In our case we will name the file "mypage.php". 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. We may just change the name of our file as propoused and save it to our server (without any additional changes), and when visiting the corresponding URL the result will be exactly the same (because there is nothing to be processed in the page) .
To this renamed page ("mypage.php") we will add a small PHP command within <?php opening tag and ?> closing tag.
| mypage.php |
| <html> <head> <title>This is my page</title> </head> <body> This is the content of my page. <?php print "Do you like it?"; ?> </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 brakets ("Do you like it?"). Often, instead of print command, echo command is used to output string. At the end of the line we will add ";" (otherway we will get an error). We may use "echo" command instead of "print", and the result will be identical.
We may also include all the php commands in one line by placing the code bellow in our page.
<?php print "Do
you like it?"; ?>
Between opening and closing tags we may include several lines with executable commands as shown bellow (we must write ";" at the end of each line):
| mypage.php |
| <html> <head> <title>This is my page</title> </head> <body> This is the content of my page. <?php print "Do you like it?"; print "<br>"; print "<b>If not, please visit a different page.</b>"; print "<br>"; print "<h1>Goodbay</h1>"; ?> </body> </html> |
Additionaly we may add several opening and closing tags within our page with executable commands.
| mypage.php |
| <html> <head> <title>This is my page</title> </head> <body <?php print "bgcolor=#FFFFFF"; ?>> <?php print "Hello!<br>"; ?> This is the content of my page. <?php print "Do you like it?"; print "<br>"; print "<b>If not, please visit a different page.</b>"; print "<br>"; print "<h1>Goodbay</h1>"; ?> </body> </html> |
As sown in the example above we may add PHP scripts within HTML tags.
Concatenation of strings
Check the codes bellow:
| Code 1 |
| <?php print "Hello!<br>"; print "I am Joe"; ?> |
| Code 2 |
| <?php print "Hello!<br>"."I am Joe"; ?> |
The pages containing any of the codes above will be exactly the same. We have just put both lines in one, and to do that we have use a point between the two texts we want to show in our pages. The point used in Code 2 will concatenated both strings.
Writing some special characters
Check the codes bellow:
| The code |
The output |
| Use "\n" to add a line break <pre> <?php print "AA\nBB\nCC\nDD\nEE"; ?> </pre> |
Use "\n" to add a line break
AA BB CC DD EE |
| Use "\"" to add brakets<br> <?php print "He said \"Hello\" to John"; ?> |
Use "\"" to add brakets He said "Hello" to John |
Using variables: first step
With PHP we may also use variables. The variables used in PHP will always start with "$", as for example $i, $a, $mydata, $my_name etc. There are some words which we are not allow to use as a name for a variable, but as we probably do not know which ones are they, our best decission will be to use very descriptive variable names:
| $my_favorite_song $my_counter $username etc |
In the example bellow we have write a PHP page using some variables:
| mypage.php |
| <?php $my_name="Joe"; $my_hello_text="Hello everybody!"; $year_create=2002; ?> <html> <head> <title>This is <?php print $my_name; ?>´spage</title> </head> <body> <?php print $my_hello_text; ?> This page was written in <?php print $year_create; ?>. </body> </html> |
In this example, we have defined three variables (line 2, 3 and 4) and we have included the information in the variable latter within the HTML code.
Check those variables carefully: $my_name, $my_hello_text and $year_create has not been defined in advance (we have add a value to the variables the first time they have used them in the file, without letting know the program we will used it in advance), and the values do not contain the same kind of content:
| $my_name, $my_hello_text
are strings $year_create is a number |
We may also use variables in the following ways:
| The code |
The output |
| <?php $year_create=2002; print "This page was written in ".$year_create; ?> |
This page was written in 2002 |
| <?php $year_create=2002; print "This page was written in $year_create"; ?> |
This page was written in 2002 |
| <?php $the_text="This page was written in "; $year_create=2002; print $the_text.$year_create; ?> |
This page was written in 2002 |
| <?php $the_text="This page was written in "; $year_create=2002; print "$the_text$year_create"; ?> |
This page was written in 2002 |
