This is default featured post 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Thursday 26 July 2012

PHP Switch Statement - Syntax - Multiple case - Default Case - Example




PHP Switch Statement



In the previous lessons we covered the various elements that make up an If Statement in PHP. However, there are times when an if statement is not the most efficient way to check for certain conditions.

For example we might have a variable that stores travel destinations and you want to pack according to this destination variable. In this example you might have 20 different locations that you would have to check with a nasty long block of If/ElseIf/ElseIf/ElseIf/... statements. This doesn't sound like much fun to code, let's see if we can do something different.

PHP Switch Statement: Speedy Checking

With the use of the switch statement you can check for all these conditions at once, and the great thing is that it is actually more efficient programming to do this. A true win-win situation!
The way the Switch statement works is it takes a single variable as input and then checks it against all the different cases you set up for that switch statement. Instead of having to check that variable one at a time, as it goes through a bunch of If Statements, the Switch statement only has to check one time.

PHP Switch Statement Example

In our example the single variable will be $destination and the cases will be: Las Vegas, Amsterdam, Egypt, Tokyo, and the Caribbean Islands.

PHP Code:

$destination = "Tokyo";
echo "Traveling to $destination<br />";
switch ($destination){
 case "Las Vegas":
  echo "Bring an extra $500";
  break;
 case "Amsterdam":
  echo "Bring an open mind";
  break; 
 case "Egypt":
  echo "Bring 15 bottles of SPF 50 Sunscreen";
  break; 
 case "Tokyo":
  echo "Bring lots of money";
  break;
 case "Caribbean Islands":
  echo "Bring a swimsuit";
  break; 
}

Display:

Traveling to Tokyo
Bring lots of money 

Default Case

You may have noticed the lack of a place for code when the variable doesn't match our condition. The if statement has the else clause and the switch statement has the default case.
It's usually a good idea to always include the default case in all your switch statements. Below is a variation of our example that will result in none of the cases being used causing our switch statement to fall back and use the default case. Note: the word case does not appear before the word default, as default is a special keyword!

PHP Code:

$destination = "New York";
echo "Traveling to $destination<br />";
switch ($destination){
 case "Las Vegas":
  echo "Bring an extra $500";
  break;
 case "Amsterdam":
  echo "Bring an open mind";
  break;
 case "Egypt":
  echo "Bring 15 bottles of SPF 50 Sunscreen";
  break; 
 case "Tokyo":
  echo "Bring lots of money";
  break; 
 case "Caribbean Islands":
  echo "Bring a swimsuit";
  break;  
 default:
  echo "Bring lots of underwear!";
  break;
}

Display:

Traveling to New York
Bring lots of underwear!




PHP If...Else Statements - PHP If Statements - Syntex - Example - Output | Conditional statements - Syntex - Code - Example.

Think about the decisions you make before you go to sleep. If you have something to do the next day, say go to work, school, or an appointment, then you will set your alarm clock to wake you up. Otherwise, you will sleep in as long as you like!
This simple kind of if/then statement is very common in every day life and also appears in programming quite often. Whenever you want to make a decision given that something is true (you have something to do tomorrow) and be sure that you take the appropriate action, you are using an if/then relationship.

The PHP If Statement

The if statement is necessary for most programming, thus it is important in PHP. Imagine that on January 1st you want to print out "Happy New Year!" at the top of your personal web page. With the use of PHP if statements you could have this process automated, months in advance, occuring every year on January 1st.
This idea of planning for future events is something you would never have had the opportunity of doing if you had just stuck with HTML.

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. The code is executed 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>


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> 

A False If Statement

Let us now see what happens when a PHP if statement is not true, in other words, false. Say that we changed the above example to:

PHP Code:

$my_name = "anotherguy";

if ( $my_name == "someguy" ) {
 echo "Your name is someguy!<br />";
}
echo "Welcome to my homepage!";

Display:

Welcome to my homepage!
Here the variable contained the value "anotherguy", which is not equal to "someguy". The if statement evaluated to false, so the code segment of the if statement was not executed. When used properly, the if statement is a powerful tool to have in your programming arsenal!

PHP Include Function - Example - Code | PHP Require Function - Example | Difference Between PHP Require Vs Include Function


PHP Include:


Without understanding much about the details of PHP, you can save yourself a great deal of time with the use of the PHP include command. include takes a file name and simply inserts that file's contents into the script that issued the include command.

An Include Example

Say we wanted to create a common menu file that all our pages will use. A common practice for naming files that are to be included is to use the ".php" extension. Since we want to create a common menu let's save it as "menu.php".

menu.php Code:

<html>
<body>
<a href="http://www.example.com/index.php">Home</a> - 
<a href="http://www.example.com/about.php">About Us</a> - 
<a href="http://www.example.com/links.php">Links</a> - 
<a href="http://www.example.com/contact.php">Contact Us</a> <br />
Save the above file as "menu.php". Now create a new file, "index.php" in the same directory as "menu.php". Here we will take advantage of the include command to add our common menu.

index.php Code:

<?php include("menu.php"); ?>
<p>This is my home page that uses a common menu to save me time when I add
new pages to my website!</p>
</body>
</html>

Display:

Home - About Us - Links - Contact Us This is my home page that uses a common menu to save me time when I add new pages to my website!
And we would do the same thing for "about.php", "links.php", and "contact.php". Just think how terrible it would be if you had 15 or more pages with a common menu and you decided to add another web page to that site. You would have to go in and manually edit every single file to add this new page, but with include files you simply have to change "menu.php" and all your problems are solved. Avoid such troublesome occasions with a simple include file.

What do Visitors See?

If we were to use the include command to insert a menu on each of our web pages, what would the visitor see if they viewed the source of "index.php"? Well, because the include command is pretty much the same as copying and pasting, the visitors would see:

View Source of index.php to a Visitor:

<html>
<body>
<a href="index.php">Home</a> - 
<a href="about.php">About Us</a> - 
<a href="links.php">Links</a> - 
<a href="contact.php">Contact Us</a> <br />
<p>This is my home page that uses a common menu to save me time when I add
new pages to my website!</p>
</body>
</html>
The visitor would actually see all the HTML code as one long line of HTML code, because we have not inserted any new line characters. We did some formatting above to make it easier to read. We will be discussing new line characters later.

Include Recap

The include command simply takes all the text that exists in the specified file and copies it into the file that uses the include command. Include is quite useful when you want to include the same PHP, HTML, or text segment on multiple pages of a website. The include command is used widely by PHP web developers. Like PHP Echo, include is not a function, but a language construct.
The next lesson will talk about a slight variation of the include command: require. It is often best to use the require command instead of the include command in your PHP Code. Read the next lesson to find out why!

______________________________________________________________________________

PHP Require

Just like the previous lesson, the require command is used to include a file into your PHP code. However there is one huge difference between the two commands, though it might not seem that big of a deal.

Require vs Include

When you include a file with the include command and PHP cannot find it you will see an error message like the following:

PHP Code:

<?php
include("noFileExistsHere.php");
echo "Hello World!";
?>

Display:

Warning: main(noFileExistsHere.php): failed to open stream: No such file or directory in /home/websiteName/FolderName/tizagScript.php on line 2 Warning: main(): Failed opening 'noFileExistsHere.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/websiteName/FolderName/tizagScript.php on line 2

Hello World!
Notice that our echo statement is still executed, this is because a Warning does not prevent our PHP script from running. On the other hand, if we did the same example but used the require statement we would get something like the following example.

PHP Code:

<?php
require("noFileExistsHere.php");
echo "Hello World!";
?>

Display:

Warning: main(noFileExistsHere.php): failed to open stream: No such file or directory in /home/websiteName/FolderName/tizagScript.php on line 2
Fatal error: main(): Failed opening required 'noFileExistsHere.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/websiteName/FolderName/tizagScript.php on line 2
The echo statement was not executed because our script execution died after the require command returned a fatal error! We recommend that you use require instead of include because your scripts should not be executing if necessary files are missing or misnamed.

PHP Operators - Examples | PHP Assignment - Arithmetic Operators list | PHP Comparison Operators - String Operators - Array Operators - Pre/Post-Increment and Pre/Post-Decrement.

n all programming languages, operators are used to manipulate or perform operations on variables and values. You have already seen the string concatenation operator "." in the Echo Lesson and the assignment operator "=" in pretty much every PHP example so far.

There are many operators used in PHP, so we have separated them into the following categories to make it easier to learn them all.


  • Assignment Operators
  • Arithmetic Operators
  • Comparison Operators
  • String Operators
  • Combination Arithmetic & Assignment Operators

Assignment Operators

Assignment operators are used to set a variable equal to a value or set a variable to another variable's value. Such an assignment of value is done with the "=", or equal character. Example:
  • $my_var = 4;
  • $another_var = $my_var;
Now both $my_var and $another_var contain the value 4. Assignments can also be used in conjunction with arithmetic operators.

Arithmetic Operators

OperatorEnglishExample
+ Addition 2 + 4
- Subtraction 6 - 2
* Multiplication 5 * 3
/ Division 15 / 3
% Modulus 43 % 10

PHP Code:

$addition = 2 + 4; 
$subtraction = 6 - 2; 
$multiplication = 5 * 3; 
$division = 15 / 3; 
$modulus = 5 % 2; 
echo "Perform addition: 2 + 4 = ".$addition."<br />"; 
echo "Perform subtraction: 6 - 2 = ".$subtraction."<br />"; 
echo "Perform multiplication:  5 * 3 = ".$multiplication."<br />"; 
echo "Perform division: 15 / 3 = ".$division."<br />"; 
echo "Perform modulus: 5 % 2 = " . $modulus 
 . ". Modulus is the remainder after the division operation has been performed.  
 In this case it was 5 / 2, which has a remainder of 1.";

Display:

Perform addition: 2 + 4 = 6
Perform subtraction: 6 - 2 = 4
Perform multiplication: 5 * 3 = 15
Perform division: 15 / 3 = 5
Perform modulus: 5 % 2 = 1. Modulus is the remainder after the division operation has been performed. In this case it was 5 / 2, which has a remainder of 1.

Comparison Operators

Comparisons are used to check the relationship between variables and/or values. If you would like to see a simple example of a comparison operator in action, check out our If Statement Lesson. Comparison operators are used inside conditional statements and evaluate to either true or false. Here are the most important comparison operators of PHP.
Assume: $x = 4 and $y = 5;
OperatorEnglish Example Result
== Equal To $x == $y false
!= Not Equal To $x != $y true
< Less Than $x < $y true
> Greater Than $x > $y false
<= Less Than or Equal To $x <= $y true
>= Greater Than or Equal To $x >= $y false

String Operators

As we have already seen in the Echo Lesson, the period "." is used to add two strings together, or more technically, the period is the concatenation operator for strings.

PHP Code:

$a_string = "Hello";
$another_string = " Billy";
$new_string = $a_string . $another_string;
echo $new_string . "!";

Display:

Hello Billy!

Combination Arithmetic & Assignment Operators

In programming it is a very common task to have to increment a variable by some fixed amount. The most common example of this is a counter. Say you want to increment a counter by 1, you would have:
  • $counter = $counter + 1;
However, there is a shorthand for doing this.
  • $counter += 1;
This combination assignment/arithmetic operator would accomplish the same task. The downside to this combination operator is that it reduces code readability to those programmers who are not used to such an operator. Here are some examples of other common shorthand operators. In general, "+=" and "-=" are the most widely used combination operators.
OperatorEnglish Example Equivalent Operation
+=Plus Equals $x += 2; $x = $x + 2;
-=Minus Equals $x -= 4; $x = $x - 4;
*=Multiply Equals $x *= 3; $x = $x * 3;
/=Divide Equals $x /= 2; $x = $x / 2;
%=Modulo Equals $x %= 5; $x = $x % 5;
.=Concatenate Equals $my_str.="hello"; $my_str = $my_str . "hello";

Pre/Post-Increment & Pre/Post-Decrement

This may seem a bit absurd, but there is even a shorter shorthand for the common task of adding 1 or subtracting 1 from a variable. To add one to a variable or "increment" use the "++" operator:
  • $x++; Which is equivalent to $x += 1; or $x = $x + 1;
To subtract 1 from a variable, or "decrement" use the "--" operator:
  • $x--; Which is equivalent to $x -= 1; or $x = $x - 1;
In addition to this "shorterhand" technique, you can specify whether you want to increment before the line of code is being executed or after the line has executed. Our PHP code below will display the difference.

PHP Code:

$x = 4;
echo "The value of x with post-plusplus = " . $x++;
echo "<br /> The value of x after the post-plusplus is " . $x;
$x = 4;
echo "<br />The value of x with with pre-plusplus = " . ++$x;
echo "<br /> The value of x after the pre-plusplus is " . $x;

Display:

The value of x with post-plusplus = 4
The value of x after the post-plusplus is = 5
The value of x with with pre-plusplus = 5
The value of x after the pre-plusplus is = 5
As you can see the value of $x++ is not reflected in the echoed text because the variable is not incremented until after the line of code is executed. However, with the pre-increment "++$x" the variable does reflect the addition immediately.

Array Operators

Operator Name Description
x + y Union Union of x and y
x == y Equality True if x and y have the same key/value pairs
x === y Identity True if x and y have the same key/value pairs in the same order and of the same types
x != y Inequality True if x is not equal to y
x <> y Inequality True if x is not equal to y
x !== y Non-identity True if x is not identical to y


PHP String Variables | String Creation | PHP String Reference | PHP - String Creation Heredoc

Before you can use a string you have to create it! A string can be used directly in a function or it can be stored in a variable. Below we create the exact same string twice: first storing it into a variable and in the second case we send the string directly to echo.

PHP Code:

$my_string = "Tizag - Unlock your potential!";
echo "Tizag - Unlock your potential!";
echo $my_string;
In the above example the first string will be stored into the variable $my_string, while the second string will be used in the echo and not be stored. Remember to save your strings into variables if you plan on using them more than once! Below is the output from our example code. They look identical just as we thought.

Display:

Elearn - Unlock your potential! Elearn - Unlock your potential! 

PHP - String Creation Single Quotes

Thus far we have created strings using double-quotes, but it is just as correct to create a string using single-quotes, otherwise known as apostrophes.

PHP Code:

$my_string = 'Elearn - Unlock your potential!';
echo 'Elearn - Unlock your potential!';
echo $my_string;
If you want to use a single-quote within the string you have to escape the single-quote with a backslash \ . Like this: \' !

echo 'Elearn - It\'s Neat!';

PHP - String Creation Double-Quotes

We have used double-quotes and will continue to use them as the primary method for forming strings. Double-quotes allow for many special escaped characters to be used that you cannot do with a single-quote string. Once again, a backslash is used to escape a character.

PHP Code:

$newline = "A newline is \n";
$return = "A carriage return is \r";
$tab = "A tab is \t";
$dollar = "A dollar sign is \$";
$doublequote = "A double-quote is \"";
Note: If you try to escape a character that doesn't need to be, such as an apostrophe, then the backslash will show up when you output the string.
These escaped characters are not very useful for outputting to a web page because HTML ignore extra white space. A tab, newline, and carriage return are all examples of extra (ignorable) white space. However, when writing to a file that may be read by human eyes these escaped characters are a valuable tool!

The Concatenation Operator

There is only one string operator in PHP.
The concatenation operator (.)  is used to put two string values together.
To concatenate two string variables together, use the concatenation operator:

<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?
The output of the code above will be:
Hello World! What a nice day!

Complete PHP String Reference

For a complete reference of all string functions, go to our complete PHP String Reference.
The reference contains a brief description, and examples of use, for each function!


PHP - String Creation Heredoc

The two methods above are the traditional way to create strings in most programming languages. PHP introduces a more robust string creation tool called heredoc that lets the programmer create multi-line strings without using quotations. However, creating a string using heredoc is more difficult and can lead to problems if you do not properly code your string! Here's how to do it:

PHP Code:

$my_string = <<<TEST
elearnsite.com
Webmaster Tutorials
Unlock your potential!
TEST;

echo $my_string;
There are a few very important things to remember when using heredoc.
  • Use <<< and some identifier that you choose to begin the heredoc. In this example we chose TEST as our identifier.
  • Repeat the identifier followed by a semicolon to end the heredoc string creation. In this example that was TEST;
  • The closing sequence TEST; must occur on a line by itself and cannot be indented!
Another thing to note is that when you output this multi-line string to a web page, it will not span multiple lines because we did not have any <br /> tags contained inside our string! Here is the output made from the code above.

Display:

elearnsite.com Webmaster Tutorials Unlock your potential!
Once again, take great care in following the heredoc creation guidelines to avoid any headaches.

PHP - Echo | How to print in PHP | PHP echo new line | php Echo Syntax - Function - Code - Example.

As you saw in the previous lesson, the PHP command echo is a means of outputting text to the web browser. Throughout your PHP career you will be using the echo command more than any other. So let's give it a solid perusal!

Outputting a String

To output a string, like we have done in previous lessons, use PHP echo. You can place either a string variable or you can use quotes, like we do below, to create a string that the echo function will output.

PHP Code:

<?php
$myString = "Hello!";
echo $myString;
echo "<h5>I love using PHP!</h5>";
?>

Display:

Hello!
I love using PHP!
 

Tips and Notes

Note: The echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will generate a parse error.
Tip: The echo() function is slightly faster than print().
Tip: The echo() function has the following shortcut syntax. See example 5.

Example 1

<?php
$str = "Who's Kai Jim?";
echo $str;
echo "<br />";
echo $str."<br />I don't know!";
?>
The output of the code above will be:
Who's Kai Jim?
Who's Kai Jim?
I don't know!


Example 2

<?php
echo "This text
spans multiple
lines.";
?>
The output of the code above will be:
This text spans multiple lines.


Example 3

<?php
echo 'This ','string ','was ','made ','with multiple parameters';
?>
The output of the code above will be:
This string was made with multiple parameters


Example 4

Difference of single and double quotes. Single quotes will print the variable name, not the value:
<?php
$color = "red";
echo "Roses are $color";
echo "<br />";
echo 'Roses are $color';
?>
The output of the code above will be:
Roses are red
Roses are $color


Example 5

Shortcut syntax:
<html>
<body>

<?php
$color = "red";
?>

<p>Roses are <?=$color?></p>

</body>
</html>
 

PHP Variables | Creating (Declaring) PHP Variables | Variable Example | Variable Scope | Variable Naming Conventions |

If you have never had any programming, Algebra, or scripting experience, then the concept of variables might be a new concept to you. A detailed explanation of variables is beyond the scope of this tutorial, but we've included a refresher crash course to guide you.

A variable is a means of storing a value, such as text string "Hello World!" or the integer value 4. A variable can then be reused throughout your code, instead of having to type out the actual value over and over again. In PHP you define a variable with the following form:
  • $variable_name = Value;
If you forget that dollar sign at the beginning, it will not work. This is a common mistake for new PHP programmers!
Note: Also, variable names are case-sensitive, so use the exact same capitalization when using a variable. The variables $a_number and $A_number are different variables in PHP's eyes.

PHP Variables

As with algebra, PHP variables are used to hold values or expressions.
A variable can have a short name, like x, or a more descriptive name, like carName.
Rules for PHP variable names:
  • Variables in PHP starts with a $ sign, followed by the name of the variable
  • The variable name must begin with a letter or the underscore character
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • A variable name should not contain spaces
  • Variable names are case sensitive (y and Y are two different variables)

PHP has no command for declaring a variable.
A variable is created the moment you first assign a value to it:
$myCar="Volvo";
After the execution of the statement above, the variable myCar will hold the value Volvo.
Tip: If you want to create a variable without assigning it a value, then you assign it the value of null.
Let's create a variable containing a string, and a variable containing a number:
<?php
$txt="Hello World!";
$x=16;
?>
Note: When you assign a text value to a variable, put quotes around the value.

A Quick Variable Example

Say that we wanted to store the values that we talked about in the above paragraph. How would we go about doing this? We would first want to make a variable name and then set that equal to the value we want. See our example below for the correct way to do this.

PHP Code:

<?php
$hello = "Hello World!";
$a_number = 4;
$anotherNumber = 8;
?>

Note for programmers: PHP does not require variables to be declared before being initialized.

PHP Variable Scope

The scope of a variable is the portion of the script in which the variable can be referenced.
PHP has four different variable scopes:
  • local
  • global
  • static
  • parameter

PHP Variable Naming Conventions

There are a few rules that you need to follow when choosing a name for your PHP variables.
  • PHP variables must start with a letter or underscore "_".
  • PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
  • Variables with more than one word should be separated with underscores. $my_variable
  • Variables with more than one word can also be distinguished with capitalization. $myVariable

Static Scope

When a function is completed, all of its variables are normally deleted. However, sometimes you want a local variable to not be deleted.
To do this, use the static keyword when you first declare the variable:
static $rememberMe;
Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.
Note: The variable is still local to the function.

PHP Installation | How to Install and Configure PHP 5 | PHP: Manual Installation Steps

 PHP Installation :

Many web developers want to run Apache and PHP on their own computer since it allows them to easily test their scripts and programs before they put them "live" on the Internet. This article gives a step by step guide on how you can install and configure PHP5 to work together with the Apache HTTP Server on Windows. The procedure has been tested to work on both Windows XP and Vista.
If you have not already installed Apache on your machine, check out one of the guides listed below. This "How To" guide assumes that you have already completed installing Apache.
  • If you are using Apache 1.3.x, see the guide How to Install the Apache Web Server 1.x on Windows.
  • If you plan to use one of the Apache 2 or 2.2 web servers on Windows XP, see the tutorial How to Install and Configure Apache 2 on Windows instead.
  • If you are using Apache 2.2 on Windows Vista, please read How to Install Apache 2.2 on Windows Vista.
Note: those planning to install PHP 4 on Apache 1.x should read my article How to Install and Configure PHP4 to Run with Apache on Windows instead.

Steps to Setting Up PHP 5 :

  1. Download PHP 5

    Before you begin, get a copy of PHP 5 from the PHP download page. In particular, download the VC6 thread-safe zip package from the "Windows Binaries" section — that is, don't get the installer. For example, select the package labelled "PHP 5.2.5 zip package" if 5.2.5 is the current version.
    [Update: note that I have not tested the procedure below with any of the PHP 5.3 versions, only with 5.2.5, which was the latest version at the time I originally wrote this. In theory, the procedure should work with later 5.2 versions as well. I'm not sure about 5.3 though. A version jump from 5.2 to 5.3 usually means bigger changes than simple bug fixes. If you want to be sure the procedure below will work, just get the latest of the 5.2 series.]
  2. Install PHP 5

    Create a folder on your hard disk for PHP. I suggest "c:\php" although you can use other names if you wish. Personally though, I prefer to avoid names with spaces in it, like "c:\Program Files\php" to avoid potential problems with programs that cannot handle such things. I will assume that you used c:\php in this tutorial.
    Extract all the files from the zip package into that folder. To do that simply double-click the zip file to open it, and drag all the files and folders to c:\php.
  3. Upgraders: Remove the Old PHP.INI File from Your Windows Directory

    If you are upgrading to PHP 5 from an older version, go to your windows directory, typically c:\windows, and delete any php.ini file that you have previously placed there.
  4. Configuring PHP

    Go to the c:\php folder and make a copy of the file "php.ini-recommended". Name the new file "php.ini". That is, you should now have a file "c:\php\php.ini", identical in content with "c:\php\php.ini-recommended".
    Note: if you are using Apache 1, you should either move the php.ini file to your windows directory, "C:\Windows" on most systems, or configure your PATH environment variable to include "c:\php". If you don't know how to do the latter, just move the php.ini file to the "c:\windows" folder. You do not have to do this if you are using Apache 2, since we will include a directive later in the Apache 2 configuration file to specify the location of the php.ini file.
    Use an ASCII text editor (such as Notepad, which can be found in the Accessories folder of your Start menu) to open "php.ini". You may need to make the following changes to the file, depending on your requirements:

php introduction | php tutorial | php introduction tutorial


PHP Tutorial - Learn PHP :

        If you want to learn the basics of PHP, then you've come to the right place. The goal of this tutorial is to teach you the basics of PHP so that you can:
  • Customize PHP scripts that you download, so that they better fit your needs.
  • Begin to understand the working model of PHP, so you may begin to design your own PHP projects.
  • Give you a solid base in PHP, so as to make you more valuable in the eyes of future employers.
PHP stands for PHP Hypertext Preprocessor.

What You Should Already Know:

Before you continue you should have a basic understanding of the following:
  • HTML/XHTML
  • JavaScript

What is PHP?

PHP was originally an acronym for Personal Home Pages, but is now a recursive acronym for PHP: Hypertext Preprocessor.
PHP was originally developed by the Danish Greenlander Rasmus Lerdorf, and was subsequently developed as open source. PHP is not a proper web standard - but an open-source technology. PHP is neither real programming language - but PHP lets you use so-called scripting in your documents.
To describe what a PHP page is, you could say that it is a file with the extension .php that contains a combination of HTML tags and scripts that run on a web server.

How does PHP work?

The best way to explain how PHP works is by comparing it with standard HTML. Imagine you type the address of an HTML document (e.g. http://www.mysite.com/page.htm) in the address line of the browser. This way you request an HTML page. It could be illustrated like this:
The figure shows a client that requests an HTML file from a server
As you can see, the server simply sends an HTML file to the client. But if you instead type  and thus request an PHP page - the server is put to work:

What is MySQL?

  • MySQL is a database server
  • MySQL is ideal for both small and large applications
  • MySQL supports standard SQL
  • MySQL compiles on a number of platforms
  • MySQL is free to download and use

PHP + MySQL:

  • PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)

Why PHP?

  • PHP runs on different platforms (Windows, Linux, Unix, etc.)
  • PHP is compatible with almost all servers used today (Apache, IIS, etc.)
  • PHP is FREE to download from the official PHP resource: www.php.net
  • PHP is easy to learn and runs efficiently on the server side

Where to Start?

To get access to a web server with PHP support, you can:
  • Install Apache (or IIS) on your own server, install PHP, and MySQL
  • Or find a web hosting plan with PHP and MySQL support




Twitter Delicious Facebook Digg Stumbleupon Favorites More