Quick and Dirty Intro To PHP: by David Choffnes
Quick and Dirty Intro To PHP: by David Choffnes
By David Choffnes
(content shamelessly ripped from the manual)
What is PHP?
• PHP (recursive acronym for "PHP:
Hypertext Preprocessor") is a widely-used
Open Source general-purpose scripting
language that is especially suited for Web
development and can be embedded into
HTML.
– Write an HTML script with some embedded
code to do something
– Code is executed on the server.
What is PHP?
• An example:
• Example 1-1. An introductory example
<html>
<head>
<title>Example</title>
</head>
<body>
<?php
echo "Hi, I’m a PHP script!";
?>
</body>
</html>
Ridiculous DB support (and more)
• Writing a database-enabled web page is incredibly
simple. The following databases are currentlysupported:
– Adabas D, Ingres, Oracle (OCI7 and OCI8), dBase, InterBase,
Ovrimos, Empress, FrontBase, PostgreSQL, FilePro (read-only),
mSQL, Solid Hyperwave, Direct MS-SQL, Sybase, IBM DB2,
MySQL, Velocis, Informix, ODBC, Unix dbm
• DBX database abstraction extension
– allows you to transparently use any database
• Supports ODBC, the Open Database Connection
standard,
• Support for talking to other services using protocols such
as LDAP, IMAP, SNMP, NNTP, POP3, HTTP, COM (on
Windows) and countless others.
Basic syntax
• Escaping from HTML
– Example 5-1. Ways of escaping from HTML
1. <? echo ("this is the simplest, an SGML
processing instruction\n"); ?>
<?= expression ?> This is a shortcut for "<? echo
expression ?>"
2. <?php echo("if you want to serve XHTML or XML
documents, do like this\n"); ?>
3. <script language="php">
echo ("some editors (like FrontPage) don’t
like processing instructions");
</script>
4. <% echo ("You may optionally use ASP-style
tags"); %>
<%= $variable; # This is a shortcut for "<%
echo . . ." %>
Basic Syntax
• Example 5-2. Advanced escaping
<?php
if ($expression) {
?>
<strong>This is true.</strong>
<?php
} else {
?>
<strong>This is false.</strong>
<?php
}
?>
Instruction Separation
• Instructions are separated the same as in
C or Perl - terminate each statement with
a semicolon.
• The closing tag (?>) also implies the end
of the statement, so the following are
equivalent:
<?php
echo "This is a test";
?>
<?php echo "This is a test" ?>
Comments
• PHP supports C, C++ and Unix shell style
comments
<?php
echo "This is a test"; // This is a one-
line c++ style comment
/* This is a multi line comment
yet another line of comment */
echo "This is yet another test";
echo "One Final Test"; # This is shell-
style style comment
?>
Types
• PHP supports eight primitive types.
– Four scalar types:
• boolean
• integer
• floating-point number (float)
• string
– Two compound types:
• array
• object
– And finally two special types:
• resource
• NULL
Scalars
• Very simple:
– $foo = true; (boolean)
– $foo = 20; (integer)
– $foo = 3.1415; (float)
Strings
• A string literal can be specified in three
different ways.
– single quoted
• Variables not expanded
– double quoted
• $foo = 20;
• echo “The value of foo is $foo”;
Strings
– heredoc syntax
Example 6-2. Here doc string quoting example
<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
?>
Arrays
• Specifying with array()
– An array can be created by the array() language-
construct. It takes a certain number of comma-
separated key => value pairs.
– A key is either a nonnegative integer or a string. If a
key is the standard representation of a non-negative
integer, it will be interpreted as such (i.e. ’8’ will be
interpreted as 8, while ’08’ will be interpreted as ’08’).
– A value can be anything.
– If you omit a key, the maximum of the integer-indices
is taken, and the new key will be that maximum +1. If
no integer-indices exist yet, the key will be 0 (zero). If
you specify a key that already has a value assigned to
it, that value will be overwritten.
Arrays
array( [key =>] value
, ...
)
// key is either string or nonnegative integer
// value can be anything