How To Create A Dynamic Website
Introduction To Creating A Dynamic Website
Up until recently, scripting on the internet was something which very few people even attempted, let alone mastered. Recently though, more and more people have been building their own websites and scripting languages have become more important. Because of this, scripting languages are becoming easier to learn and PHP is one of the easiest and most powerful yet.
Understanding PHP
Written by Prime
What is PHP
PHP stands for Hypertext Preprocessor and is a server-side language. This means that the script is run on your web server, not on the user's browser, so you do not need to worry about compatibility issues. PHP is relatively new (compared to languages such as Perl (CGI) and Java) but is quickly becoming one of the most popular scripting languages on the internet.
Why PHP
You maybe wondering why you should choose PHP over other languages such as Perl or even why you should learn a scripting language at all. Learning a scripting language, or even understanding one, can open up huge new possibilities for your website. Although you can download pre-made scripts from sites like Hotscripts, these will often contain advertising for the author or wil not do exactly what you want. With an understanding of a scripting language you can easily edit these scripts to do what you want, or even create your own scripts.
Using scripts on your website allows you to add many new 'interative' features like feedback forms, guestbooks, message boards, counters and even more advanced features like portal systems, content management, advertising managers etc. With these sort of things on your website you will find that it gives a more professional image. As well as this, anyone wanting to work in the site development industry will find that it is much easier to get a job if they know a scripting language.
What Do You Need
You need to have a web server with PHP running on it. You can either have it on your own or you will get a web hosting service that supports PHP scripting. I would recommend to check it from http://webhost.dpjonline.com
Writing PHP
Writing PHP script would need a text editor. You may use notepad from your computer but if you want to have a good presentation of your code, you may use tswebeditor or netbeans ide
Declaring PHP
PHP scripts are always enclosed in between two PHP tags. This tells your server to parse the information between them as PHP. The three different forms ar as follows:
<?
PHP Code Here
?>
<?php
PHP Code Here
?>
<script language="php">
PHP Code Here
</script>
All these work in exactly the same way but I normally use the second declaration of PHP (<?php ... ?>). There's no particular reason for this, though, and you can use either of the three different declarations. As you noticed they all starts with <? and end with ?>
Your First PHP Script
This is a basic script. All it will do is print out all the information about PHP on your server. Start your text editor and type the following PHP code:
<?php
phpinfo();
?>
As you can see this is just a single line of code. It is a standard PHP function called phpinfo which will tell the server to print out a standard table of information about PHP setup on your web server.
One more thing you should notice in this example is that the line ends in a semicolon. This is very important. As with many other scripting and programming languages nearly all lines are ended with a semicolon to inform the scripting engine that it ends our single instruction. If you miss to place a semicolon, the scripting engine will no longer understand your script and will display some errors on your screen.
Finishing And Testing Your Script
You can now save yoru script as phpinfo.php and save it to your web server's root or public directory or upload it to your web hosting service via FTP or file manager. Using your favorite web browser go to the address of your script. If you're having a server on the same computer you may have it as http://localhost/phpinfo.php . If PHP is installed and working, you should get a page full of PHP server informations on your web browser's window. If it doesnt work, you may mistyped it or saved it in the wrong directory of your server.
Control Structures
Conditional Statements
Very often when you write code, you want to perform different actions for different decisions.
You can use conditional statements in your code to do this.
In PHP we have the following conditional statements:
- if statement - use this statement to execute some code only if a specified condition is true
- if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false
- if...elseif....else statement - use this statement to select one of several blocks of code to be executed
- switch statement - use this statement to select one of many blocks of code to be executed
The if Statement
Use the if statement to execute some code only if a specified condition is true.
Syntax
| if (condition) code to be executed if condition is true; |
The following example will output "Have a nice weekend!" if the current day is Friday:
| <html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; ?> </body> </html> |
Notice that there is no ..else.. in this syntax. You tell the browser to execute some code only if the specified condition is true.
The if...else Statement
Use the if....else statement to execute some code if a condition is true and another code if a condition is false.
Syntax
| if (condition) code to be executed if condition is true; else code to be executed if condition is false; |
Example
The following example will output "Have a nice weekend!" if the current day is Friday, otherwise it will output "Have a nice day!":
| <html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?> </body> </html> |
If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces:
| <html> <body> <?php $d=date("D"); if ($d=="Fri") { echo "Hello!<br />"; echo "Have a nice weekend!"; echo "See you on Monday!"; } ?> </body> </html> |
The if...elseif....else Statement
Use the if....elseif...else statement to select one of several blocks of code to be executed.
Syntax
| if (condition) code to be executed if condition is true; elseif (condition) code to be executed if condition is true; else code to be executed if condition is false; |
Example
The following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!":
| <html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; elseif ($d=="Sun") echo "Have a nice Sunday!"; else echo "Have a nice day!"; ?> </body> </html> |
Flow Control
The while Loop
The while loop executes a block of code while a condition is true.
Syntax
| while (condition) { code to be executed; } |
Example
The example below defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:
| <html> <body> <?php $i=1; while($i<=5) { echo "The number is " . $i . "<br />"; $i++; } ?> </body> </html> |
Output:
| The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 |
The do...while Statement
The do...while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true.
Syntax
| do { code to be executed; } while (condition); |
Example
The example below defines a loop that starts with i=1. It will then increment i with 1, and write some output. Then the condition is checked, and the loop will continue to run as long as i is less than, or equal to 5:
| <html> <body> <?php $i=1; do { $i++; echo "The number is " . $i . "<br />"; } while ($i<=5); ?> </body> </html> |
Output:
| The number is 2 The number is 3 The number is 4 The number is 5 The number is 6 |
The for Loop
The for loop is used when you know in advance how many times the script should run.
Syntax
| for (init; condition; increment) { code to be executed; } |
Parameters:
- init: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop)
- condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
- increment: Mostly used to increment a counter (but can be any code to be executed at the end of the loop)
Note: Each of the parameters above can be empty, or have multiple expressions (separated by commas).
Example
The example below defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:
| <html> <body> <?php for ($i=1; $i<=5; $i++) { echo "The number is " . $i . "<br />"; } ?> </body> </html> |
Output:
| The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 |
The foreach Loop
The foreach loop is used to loop through arrays.
Syntax
| foreach ($array as $value) { code to be executed; } |
For every loop iteration, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next array value.
Example
The following example demonstrates a loop that will print the values of the given array:
| <html> <body> <?php $x=array("one","two","three"); foreach ($x as $value) { echo $value . "<br />"; } ?> </body> </html> |
Output:
| one two three |
PHP Functions
In this chapter we will show you how to create your own functions.
To keep the browser from executing a script when the page loads, you can put your script into a function.
A function will be executed by a call to the function.
You may call a function from anywhere within a page.
Create a PHP Function
A function will be executed by a call to the function.
Syntax
| function functionName() { code to be executed; } |
PHP function guidelines:
- Give the function a name that reflects what the function does
- The function name can start with a letter or underscore (not a number)
Example
A simple function that writes my name when it is called:
| <html> <body> <?php function writeName() { echo "Kai Jim Refsnes"; } echo "My name is "; writeName(); ?> </body> </html> |
Output:
| My name is Kai Jim Refsnes |
PHP Functions - Adding parameters
To add more functionality to a function, we can add parameters. A parameter is just like a variable.
Parameters are specified after the function name, inside the parentheses.
Example 1
The following example will write different first names, but equal last name:
| <html> <body> <?php function writeName($fname) { echo $fname . " Refsnes.<br />"; } echo "My name is "; writeName("Kai Jim"); echo "My sister's name is "; writeName("Hege"); echo "My brother's name is "; writeName("Stale"); ?> </body> </html> |
Output:
| My name is Kai Jim Refsnes. My sister's name is Hege Refsnes. My brother's name is Stale Refsnes. |
Example 2
The following function has two parameters:
| <html> <body> <?php function writeName($fname,$punctuation) { echo $fname . " Refsnes" . $punctuation . "<br />"; } echo "My name is "; writeName("Kai Jim","."); echo "My sister's name is "; writeName("Hege","!"); echo "My brother's name is "; writeName("Ståle","?"); ?> </body> </html> |
Output:
| My name is Kai Jim Refsnes. My sister's name is Hege Refsnes! My brother's name is Ståle Refsnes? |
PHP Functions - Return values
To let a function return a value, use the return statement.
Example
| <html> <body> <?php function add($x,$y) { $total=$x+$y; return $total; } echo "1 + 16 = " . add(1,16); ?> </body> </html> |
Output:
| 1 + 16 = 17 |
The $_GET Function
The $_GET Function
The built-in $_GET function is used to collect values from a form sent with method="get".
Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send (max. 100 characters).
Example
| <form action="welcome.php" method="get"> Name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> |
When the user clicks the "Submit" button, the URL sent to the server could look something like this:
| http://www.w3schools.com/welcome.php?fname=Peter&age=37 |
The "welcome.php" file can now use the $_GET function to collect form data (the names of the form fields will automatically be the keys in the $_GET array):
| Welcome <?php echo $_GET["fname"]; ?>.<br /> You are <?php echo $_GET["age"]; ?> years old! |
When to use method="get"?
When using method="get" in HTML forms, all variable names and values are displayed in the URL.
Note: This method should not be used when sending passwords or other sensitive information!
However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.
Note: The get method is not suitable for large variable values; the value cannot exceed 100 characters.
More Articles...
Page 1 of 2


