Hardware Reference
In-Depth Information
Here's a slightly more complex PHP script. Save it to your
server in the public_html directory as time.php:
In PHP, there are three important built-in variables, called
environment variables , with which you should be familiar:
$_REQUEST , $_GET , and $_POST . These give you the
results of an HTTP request. Whether your PHP script was
called by a HTML form or by a user entering a URL with a
string of variables afterwards, these variables will give you
the results. $_GET gives you the results if the PHP script
was called using an HTTP GET request, $_POST gives the
results of an HTTP POST request, and $_REQUEST gives
you the results regardless of what type of request was
made.
<?php
/*
Date printer
Context: PHP
Prints the date and time in an HTML page.
*/
// Get the date, and format it:
$date = date("Y-m-d h:i:s\t");
Since HTTP requests might contain a number of
different pieces of information (think of all the fields
you might fill out in a typical web form), these are all
array variables. To get at a particular element, you can
generally ask for it by name. For example, if the form
you filled out had a field called Name, the name you
fill in would end up in the $_REQUEST variable in an
element called $_REQUEST['Name'] . If the form made an
HTTP POST request, you could also get the name from
$_POST['Name'] . There are other environment variables
you'll learn about as well, but these three are the most
useful for getting information from a client—whether it's a
web browser or a microcontroller. You'll learn more about
these, and see them in action, later in the topic.
// print the beginning of an HTML page:
echo "<html><head></head><body>\n";
echo "hello world!<br>\n";
// Include the date:
echo "Today's date: $date<br>\n";
// finish the HTML:
echo "</body></html>\n";
?>
To see it in action, type http://www.example.com/
time.php into your browser (replacing example.com as
before). You should get the date and time. You can see
this program uses a variable, $date , and calls a built-in
PHP function, date() , to fill the variable. You don't have to
declare the types of your variables in PHP. Any simple, or
scalar , variable begins with a $ and can contain an integer,
a floating-point number, or a string. PHP uses the same
C-style syntax as Processing, so you'll see that if-then
statements, repeat loops, and comments all look familiar.
For more on PHP, check out www.php.net , the main source
for PHP, where you'll find some good tutorials on how to
use it. You can also read Learning PHP 5 by David Sklar
(O'Reilly) for a more in-depth treatment.
Serial Communication Tools
The remote-access programs in the earlier section were
terminal emulation programs that gave you access to
remote computers through the Internet, but that's not all
a terminal emulation program can do. Before TCP/IP was
ubiquitous as a way for computers to connect to networks,
connectivity was handled through modems attached to
the serial ports of computers. Back then, many users con-
nected to bulletin boards (BBSes) and used menu-based
systems to post messages on discussion boards,
down-load files, and send mail to other users of the same
BBS.
Variables in PHP
PHP handles variables a little differently than Process-
ing and Arduino. In the latter two, you give variables any
name you like, as long as you don't use words that are
commands in the language. You declare variables by
putting the variable type before the name the first time
you use it. In PHP, you don't need to declare a variable's
type, but you do need to put a $ at the beginning of the
name. You can see it in the PHP script above. $date is a
variable, and you're putting a string into it using the date()
command.
There are a number of commands for checking variables
that you'll see in PHP. For example, isset() checks whether
the variable's been given a value yet, or is_bool() , is_int() ,
and is_string() check to see whether the variable contains
those particular data types (boolean, integer, and string,
respectively).
Nowadays, serial ports are used mainly to connect to
some of your computer's peripheral devices. In micro-
controller programming, they're used to exchange data
between the computer and the microcontroller. For the
projects in this topic, you'll find that using a terminal
 
Search WWH ::




Custom Search