Guide to HTML, JavaScript and PHP for Scientists and Engineers [Brooks 2011-05-19].pdf

(10246 KB) Pobierz
David R. Brooks
Guide to HTML, JavaScript
and PHP
For Scientists and Engineers
777149906.005.png
David R. Brooks
Institute for Earth Science Research and Education
2686 Overhill Drive
Eagleville, PA 19403
USA
brooksdr@drexel.edu
ISBN 978-0-85729-448-7
e-ISBN 978-0-85729-449-4
DOI 10.1007/978-0-85729-449-4
Springer London Dordrecht Heidelberg New York
British Library Cataloguing in Publication Data
A catalogue record for this book is available from the British Library
Library of Congress Control Number: 2011926229
© Springer-Verlag London Limited 2011
Apart from any fair dealing for the purposes of research or private study, or criticism or review, as
permitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced,
stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers,
or in the case of reprographic reproduction in accordance with the terms of licenses issued by the
Copyright Licensing Agency. Enquiries concerning reproduction outside those terms should be sent to
the publishers.
The use of registered names, trademarks, etc., in this publication does not imply, even in the absence of
a speciic statement, that such names are exempt from the relevant laws and regulations and therefore
free for general use.
The publisher makes no representation, express or implied, with regard to the accuracy of the information
contained in this book and cannot accept any legal responsibility or liability for any errors or omissions
that may be made.
Printed on acid-free paper
Springer is part of Springer Science+Business Media (www.springer.com)
Preface
“The best way to become acquainted with a subject is to write
a book about it.”
—Benjamin Disraeli
“Each problem that I solved became a rule, which served
afterwards to solve other problems.”
—René Descartes
i
What Is the Purpose of This Book?
There are many students and professionals in science and engineering, other than those spe-
ciically interested in ields such as computer science or computer engineering, who need to
know how to solve computational problems on computers. There are basically two approaches
to meeting the needs of such people. One is to rely on software applications such as spread-
sheets, using built-in functions and perhaps user-deined macros, without requiring any
explicit understanding of the principles on which programming languages are based.
A second approach is to learn a programming language, previously Fortran or Pascal,
and more recently C, C++, or Java. These languages are important for certain kinds of
work, such as computer science or scientiic computing, but they may be viewed, possibly
with good reason, as irrelevant by many students and professionals.
From a student’s point of view, there is no painless solution to this dilemma, but in this
book I assume that learning to solve computational problems in an online environment
using HTML, 1 JavaScript , and PHP will at least appear to be a more relevant approach.
HTML and JavaScript are universally used for developing self-contained online applica-
tions. The use of PHP for accessing externally stored data iles, a capability that is not
available directly through JavaScript, greatly extends the range of science and engineering
problems that can be addressed. A working knowledge of these programming languages is
a valuable skill for any scientist or engineer. The fact that these are Web-based languages
may make such skills more immediately marketable than comparable skills developed
with older text-based languages.
1 See Glossary for definitions of terms appearing in bold font.
v
777149906.006.png 777149906.007.png
 
vi
Preface
In some ways, the HTML/JavaScript/PHP environment is more dificult to learn than a
traditional text-based programming language such as C. C is a mature (some might prefer
“obsolete”), fairly small language with an unambiguous set of syntax rules and a primitive
text-based input/output interface. You can view the limitations of C as either a blessing or
a curse, depending on your needs. A major advantage of C is that programs written in
ANSI Standard C should work equally well on any computer that has a C compiler, making
the language inherently platform-independent .
HTML, JavaScript, and PHP, on the other hand, are immature and unstable program-
ming languages (if we can agree to call HTML a “language”) that function within a con-
stantly changing Web environment. There are dialects of HTML and JavaScript that will
work only on particular computing platforms and the possibility exists for language “exten-
sions” that may be even more platform-dependent. PHP is more platform-independent, but
it is still an evolving language whose standards are set and maintained by a user group –
essentially by volunteers. While it is true that there are extensions to languages such as C
and other older languages that are platform-dependent, the platform dependence of lan-
guages used in the online environment is a major implementation issue rather than an
occasional minor inconvenience.
As one indication of the teaching and learning challenges these environments provide,
just three popular paperback HTML and JavaScript reference books occupy nearly 6 in. of
space (15 cm in deference to a metric audience) on my ofice bookshelf! A great deal of
the material in those books is devoted to explaining the often subtle differences among
various versions of HTML and JavaScript.
Fortunately, it is possible to work with some core subsets of HTML and JavaScript
which, with PHP, can be used to solve some of the same kinds of computational problems
that would be appropriate for a more traditional language such as C or C++. My initial
motivation for writing this book was to learn how to use HTML, JavaScript, and PHP to
create my own online applications, and I now use this environment for many tasks that I
previously would have undertaken in C. Based on this experience, I have concluded that,
despite the fact that these languages cannot fairly be deined as “scientiic computing”
languages, it is nonetheless entirely reasonable to use them to learn basic programming
skills, and to create useful and robust science and engineering applications.
Although this book is intended for “scientists and engineers,” as suggested by its title,
the content is not technically complex. The examples and exercises do not require exten-
sive science, engineering, or mathematics background and only rarely is mathematics
beyond basic algebra needed. So, I believe this book could serve as a beginning program-
ming text for undergraduates and even for high school students.
ii
Learning by Example
It is well known that people learn new skills in different ways. Personally, I learn best by
having a speciic goal and then studying examples that are related to that goal. Once I
understand those examples, I can incorporate them into my own work. I have used that
777149906.001.png 777149906.002.png
Preface
vii
learning model in this book, which contains many complete examples that can serve as
starting points for your work. (See the second quotation at the beginning of this preface.)
This model works particularly well in an online environment. The amount of online
information about HTML, JavaScript, and PHP, including code samples, is so vast that it
is only a slight exaggeration to state that nobody writes original code anymore. If you have
trouble “learning by example,” you will have trouble learning these languages, not just
from this book, but in general because that is how most of the available information is
presented.
It is an inescapable fact that a great deal of the source code behind Web pages involves
nothing more (or less) than creative cutting, pasting, and tweaking of existing code. Aside
from the issues of plagiarism and intellectual dishonesty that must be dealt with in an aca-
demic environment, there is also the practical matter of an effective learning strategy. You
cannot learn to solve your own computational problems just by trying to paste together
someone else’s work. (Believe me, I’ve tried!) Until you develop your own independent
skills, you will constantly be frustrated because you will never ind exactly what you need
to copy and you will be unable to synthesize what you need from what is available.
So, while you should expect to ind yourself constantly recycling your own code based
on what you learn from this book, you need to make sure that you really learn how to use
these languages and don’t just learn to copy !
If you are reading this book, you almost certainly are not and do not aspire to be a pro-
fessional programmer. For a casual programmer from a scientiic or technical background,
it can be very time consuming to cut through the clutter of online information about these
languages when the applications are not directly applicable to the needs of scientists and
engineers. In my own work, what I need over and over again is some sample code that will
jog my memory about how to approach recurring programming problems – how to select
items from a pull-down list, how to extract information from a data ile, how to pass infor-
mation from an HTML document to a PHP application, how to display data-based graph-
ics, etc. Throughout the book, I have tried to give examples that serve this need, including
an entire chapter devoted to PHP graphics.
iii
The Origin and Uses of This Book
In 2007, Springer published An Introduction to HTML and JavaScript for Scientists
and Engineers . This was followed in 2008 by An Introduction to PHP for Scientists and
Engineers: Beyond JavaScript . Those two books followed the sequence in which I learned
to use HTML, JavaScript, and PHP in my own work. (See the irst quotation at the begin-
ning of this preface.) When the time came to consider a second edition of the HTML and
JavaScript book, it seemed a better idea to undertake a rewrite that would combine both
books into a single volume. This book is the result. I have, hopefully, clariied some of the
explanations. There are more examples and exercises and I have added some new material
that my students and I have found useful, including a brief introduction to using “pseudo-
code” as an approach to organizing solutions to computing problems (see Appendix 4).
777149906.003.png 777149906.004.png
Zgłoś jeśli naruszono regulamin