PHP is designed for developing web applications, and generally has little use in other scenarios. It is easy to integrate with a wide range of other technologies.
Why would I learn this language?
PHP as an environment is very easy to deploy. It is generally combined with Linux, Apache and MySQL (a LAMP setup). This makes it ideal for simple web applications and projects.
One great feature of PHP as a community is the documentation. There are fantastic resources available on the main PHP website, and there are many external resources discussing PHP programming and related topics.
PHP provides integration with many other libraries, such as databases, image manipulation and network communication. However, each external library tends to have its own syntax and semantic behavior. This can make larger programs unmanageable without careful planning.
Starting Points
- PHP Programming Wikibook
- A comprehensive guide to learning to program using PHP.
Example Code
The following is an example of the Fizz Buzz problem. You can run and edit this program here.
<?php
for ($i = 1; $i <= 100; $i++)
{
if ($i % 3 == 0 && $i % 5 ==0) {
echo "FizzBuzz \n";
}
else if($i % 3 == 0){
echo "Fizz \n";
}
else if($i % 5 == 0){
echo "Buzz \n";
}
else {
echo $i." \n";
}
}
?>
The following is an example of the 100 doors problem. You can run and edit this program here.
<?php
function doors ($n) {
// Create a new array with n entries starting from 0, all set to CLOSED)
$doors = array_fill(0, $n, false);
// Now, start opening and closing
for ($step = 0; $step < $n; $step++) {
for ($idx = $step; $idx < $n; $idx += $step+1) {
// Toggle state of door
$doors[$idx] = !$doors[$idx];
}
}
// Find out which doors are open
foreach($doors as $i => $door) {
if ($door)
echo "Door #" .($i+1) . " is open.\n";
else
echo "Door #" .($i+1) . " is closed.\n";
}
}
doors(100);
?>
Further Reading
- Wikipedia: PHP
- PHP: Hypertext Preprocessor (Main Website)