PHP is a scripting language which is used primarily for serving up dynamic web pages, particularly those which access a database like MySQL, although it supports many other databases as well. It is free, and platform independent.
PHP was developed by Zend Technologies
Take the W3Schools tutorial. It's a great introduction.
Solving the Hello World problem in PHP
<html> <body> <?php echo "Hello World"; ?> </body> </html>
If you looked at the source on your browser, you would see this
<html> <body> Hello World </body> </html>
Some basic rules:
The file has to have a .php suffix.
A PHP scripting block always starts with <?php and ends with
?>. A PHP scripting block can be placed anywhere in the
document.
Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another.
There are two basic statements to output text with PHP: echo and print. The dot operator is used to concatenate args to echo.
All variables in PHP start with a $ sign symbol. Variables may contain strings, numbers, or arrays (weakly typed).
Uses c or c++ style comments
It supports all your favorite arithmetic operators (+, -, *, /,
relational operators >, < == != >=
Logical operators && ||
The if syntax is exactly like C if (...) { } else { }
The for and while syntax is exactly the same, there is even a switch statement.
There is a way to interate through an array which is a little different
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "
There are more than 3000 built in functions
Here is a link to all of them
The famous function phpinfo() displays massive amounts of
info about php and the system
Run it here to
see what the output looks like
You can of course write your own functions in the usual way.
<html>
<body>
<?php
function square($n) {
$retval = $n * $n;
return $retval;
}
for ($i = 1; $i < 6; $i++) {
echo "The square of " . $i . " is " . square($i);
echo "<br>";
}
?>
</body>
</html>
You can even write recursive functions (unusual in a scripted language)
<html>
<body>
<?php
function recursion($a)
{
if ($a < 70000) {
echo $a . " ";
recursion($a * 2);
}
}
x = 2;
recursion($x);
?>
</body>
</html>
PHP can throw exceptions with the try catch syntax.
PHP Server Variables
All servers hold information such as which URL the user came from,
what's the user's browser, and other information. This information is
stored in variables.
In PHP, the $_SERVER is a reserved variable that contains all server
information. The $_SERVER is a global variable - which means that it's
available in all scopes of a PHP script. Example
The following example will output which URL the user came from, the
user's browser, and the user's IP address:
<html>
<body>
<?php
echo "Referer: " . $_SERVER["HTTP_REFERER"] . <br />";
echo "Browser: " . $_SERVER["HTTP_USER_AGENT"] . "<br />";
echo "User's IP address: " . $_SERVER["REMOTE_ADDR"];
?>
</body>
</html>
The header() function is used to send raw HTTP headers over
the HTTP protocol.
Note: This function must be called before anything is written to the page!
<?php
//Redirect browser
header("Location: http://www.w3schools.com/");
?>
Note: This function also takes a second parameter - an optional value
of true or false to determine if the header should replace the
previous header. Default is TRUE.
However, if you pass in FALSE as the second argument you can FORCE
multiple headers of the same type.
The fopen function opens a file
$f=fopen("welcome.txt","r");
<?php
if (!($f=fopen("welcome.txt","r")))
exit("Unable to open file!");
?>
there is an end of file test feof()
and the usual collection of ways to read data
fgetc reads a single char
<?php
if (!($f=fopen("welcome.txt","r")))
exit("Unable to open file.");
while (!feof($f))
{
$x=fgetc($f);
echo $x;
}
fclose($f);
?>
Reading data from the client.
$_POST["name"] is a reference to the value of
the variable name passed in with the post method.
Here is our currencyconverter program
<html>
<head>
<title>CurrencyConverter.php</title>
</head>
<body>
<big>
<?php $rate = .87;
echo "$ " . $_GET["amount"] . " = €" . $_GET["amount"] * $rate;
?>
</body>
</html>
To invoke this
<html>
<head>
<title>Currency Converter</title>
</head>
<body>
<h1>Currency Converter</h1>
<form method=GET
action="http://cgi2.cs.rpi.edu/~ingallsr/CurrencyConverter.php">
<P>
converts dollars to euros<p>
Enter the dollar amount in the box<p>
<input type=text name="amount"><p>
<input type="submit"></form>
</body>
</html>
How to Create a Cookie
The setcookie() function is used to create cookies. This must
be called before the <html> tag
setcookie(name, value, expire, path, domain);
All args except the first are optional
Example
The following example sets a cookie named "uname" - that expires after ten hours.
<?php
setcookie("uname", $name, time()+36000);
?>
<html>
<body>
How to Retrieve a Cookie Value
There are a number of predefined variables, one is $_COOKIE
When a cookie is set, PHP uses the cookie name as a variable.
To access a cookie you just refer to the cookie name as a variable.
Tip: Use the isset() function to find out if a cookie has been set.
Example
The following example tests if the uname cookie has been set, and prints an appropriate message.
<html>
<body>
<?php
if (isset($_COOKIE["uname"]))
echo "Welcome " . $_COOKIE["uname"] . "!<br />";
else
echo "You are not logged in!<br />";
?>
</body>
</html>
Server Side Includes
You can insert the content of one file into another file before the
server executes it, with the require() function. The require()
function is used to create functions, headers, footers, or elements
that will be reused on multiple pages.
<?php require("headers.html"); ?>
This would come after the html tag
XML parsing in PHP
PHP supports all of your favorite dom parsing elements
<html>
<body>
<?php
function shownode($node)
{
if ($node->node_type() == XML_ELEMENT_NODE) {
echo "Node name is " . $node->node_name() . "<br>";
if ($node->has_child_nodes()) {
$children = $node->child_nodes();
foreach($children as $child) {
shownode($child);
}
}
}
}
if (!$dom = domxml_open_file("book.xml")) {
echo "Error while parsing the document\n";
exit;
}
$root = $dom->document_element();
shownode($root);
?>
</body>
</html>
Ruby on Rails
Rails is a full-stack, open-source web framework in Ruby for writing
real-world applications with joy and less code than most frameworks
spend doing XML sit-ups