There are many students and professionals in the natural sciences and engineering, apart from the specifically interested fields such as computer science or computer engineering, who need to know how to solve computational problems on computers. My motivation for writing this book and its predecessors was to learn how to use HTML and PHP to create my own online applications, and I now use this environment for many tasks that I would have previously undertaken in Fortran or C.
Introducing the Tools
- What Is an HTML Document?
- How Do You Create HTML Documents?
- Some Typographic Conventions Used in This Book
- Finding More Information About HTML
Microsoft Word and other modern word processing programs can also format any document as an HTML document. To see this for yourself, save a Word document as an HTML document and then view the result in a text editor such as Notepad!).
Your First HTML Document
Many examples presented in this text will include a browser's rendering of screen output produced by the document. When a border appears around the output, as it does for the output of Document 1.1, the purpose is to distinguish the output from the rest of the text - the document does not generate this border.
Accessing HTML Documents on the Web
The HTML documents discussed in this book were neither on the Internet nor on an intranet when they were developed. You can make documents you create yourself available on the Internet or intranet by hosting them on a web server.
Another Example
Given the fact that high-resolution images can produce very large files (>10 Mb), it is still important to consider the appropriate resolution and size for images included in HTML documents, even in an age of links of high speed broadband internet and large amounts of online storage space. The size of the printed image grayscalehouse.jpg printed here is about 93 Kb.). The image shown in Document 1.2 was taken by the author of this book, of his house under construction.
Documents, Elements, Attributes, and Values
Documents and Their Essential Elements
Below is a brief description of the four elements that will be part of any HTML document. Every HTML document must have a title, included as the first element within the head element.
Some Other Important Elements
The displayed height overrides the actual height of the image file - the number of rows in the image. The displayed width overrides the actual width of the image file - the number of columns in the image.
HTML Syntax and Style
Fortunately, market forces—the desire to get as many people as possible to accept your browser's interpretation of HTML documents—have forced uniformity on a large subset of HTML. This text will adopt some HTML style conventions and syntax that will be as cross-platform as possible.
Creating and Organizing a Website
Here is my résumé. lt;title>Homework. If the site is moved, only a single reference, to the index2.htm file, needs to be changed.
Selecting and Using Colors
If you're not sure if a color name is supported and how it will look on your monitor, you have nothing to lose by trying it. If a color name is not recognized by your browser, the result is unpredictable, but not catastrophic.
Using Cascading Style Sheets
CSS can be used to change the appearance of any HTML element that contains content. For other background properties that can be used to control the appearance of a background image, see the HTML reference text.
Another Example
If you keep your style sheets in a separate folder, you'll obviously need to reference that folder. For the types of applications discussed in this book, stair-style blades can sometimes be convenient, but they are not required.
The table Element
Table Formatting
When align is specified in a tr element, its value will be overridden if it is specified again within a td element on that line. When valign is specified in a tr element, its value will be overridden if it is specified again within a td element on that line.
Subdividing Tables into Sections
There is no provision for changing the radon values, and even if you could, you cannot use HTML to change the background colors in response to the radon values. However, if the radon values and perhaps the site names are entered in input elements, PHP can be used to generate a table with background colors calculated based on the radon values entered.
Merging Cells Across Rows and Columns
Merging cells across rows in columns in an HTML table is more tedious than it is in a word processor.
The form and input Elements
Text can be used to explain to the user of the form what type of input is expected. When the allowed input choices can be limited ahead of time by the document creator, it is appropriate to use.
Creating Pull-Down Lists
It is important to specify a value for each button because the value of the highlighted button will be captured when the content of the form is submitted to a PHP application. When you access the document, the button with the highlighted attribute will be "on". You can change it by clicking on another of the buttons in the group.
Combining Tables and Forms
In this case, the month values are numbers between 1 and 12, rather than the names of the months. The user of the page can change the default values of all input text boxes.
HTML List Elements
Although creating a 3.6 document may seem like a lot of work, the task is greatly simplified by copying and pasting information for rows. For example, in document 3.7, the items in the definition list use the b element to display the item name in bold.
HTML Frames
This is done by using thetargetattribute, which links to homeFrame, the name value specified in Document 3.9a:. Clicking on the image opens a link to the descriptive file, frameDescription.htm (see document 3.9e), which can be provided by.
More Examples
Selecting Cloud Types from a List of Possibilities
Accessing the "view source" option in your browser will only display the HTML code for frameMain.htm. Note that the names given to each checkbox in Document 3.10 are the same as the text entered in the corresponding cell.
A “ Split Window ” Application
In general, the text in the cell does not have to be the same as, or even related to, the value of the checkbox's thename attribute. the escape sequence allows inserting "white space" between the text to align the heads.
A Simple HTML/PHP Application
For values greater than or equal to 4 pCi/L,. action must be taken
to reduce the concentration of radon gas. Choose colors for a color-coded table. echo"The table below shows some radon levels measured in apartments.
";. echo" to reduce the concentration of radon gas.
Setting Up an HTML/PHP Environment
- Install or Gain Access to a PHP-Enabled Server
- Use an Editor to Create PHP Scripts
- Pass Information from HTML to a PHP Application
- Access Information Stored in Server-Based Files
When passing information from an HTML document to a PHP application, it's always a good idea to show the values being passed to the PHP application before writing more PHP code. While the (!feof($in)) statement is interpreted as "As long as I don't see an end-of-file mark, I will continue to read and process data from the specified file."(! is it" not" operator, as in "not at end-of-file tag.")5.
More Examples
Solving the Quadratic Equation
Note that the PHP variable names in this example are the same as the form field names in the corresponding HTML document. For the kinds of problems presented in this book, in which an HTML document is typically paired with a PHP application, it is often a reasonable stylistic choice to use the same names for variables in the PHP code as in the HTML document was used. .
Future Value with Compounded Interest
What Should a Programming Language Do?
Much of the work that computers are required to do involves manipulating characters and character strings instead of numbers. PHP supports manipulation of characters and character strings, including interpreting character strings as numbers and vice versa.
Some Essential Terminology
Usually, a program will compare values and then execute instructions based on the results of those comparisons. Token An indivisible lexical unit that defines all variables, keywords, operators, and literal words within a programming language.
Structure of PHP Scripts
- Statements and Statement Blocks
- Comments
- Data Types, Variables, and Literals
- Arithmetic Operators
- The Assignment Operator
- Relational and Logical Operators
The meaning of the assignment operator is: "Evaluate the expression on the right side of the assignment operator and assign the result to the identifier on the left side of the assignment operator.". When it sees an assignment operator, it just knows how to evaluate the expression on the right side of the operator and assign that result to the identifier on the left side of the expression.
Conditional Execution
In cases like this, the author believes that using the "extra" parentheses in the first example above is a better programming style. 1 However, it is true that many programmers regularly take advantage of precedence rules in their code.
Loop Structures
Count-controlled Loops
The second expression specifies the conditions under which the loop should continue executing, or, looking at it another way, it sets the final condition; the loop continues as long as the value of the second expression is strue. It is incremented in increments of 1 and the loop is executed until $k is less than 10.
Conditional Loops
If they can get into the elevator without going over the load limit, they're allowed in. If not, the elevator will leave without trying to find someone who weighs less than the person who is currently first in line.
More Examples
The Quadratic Equation Revisited
Combining HTML documents and PHP scripts literally involves combining an HTML document (all inside the HTML element) with a PHP script inside. Instead of referencing a separate PHP application, the action attribute references itself – that is, the PHP code inside the same document – through the action attribute of the form element.
Newton ’ s Square Root Algorithm
This means that the loop counter should not stop at the size of the array (4 in the case of $A, but 3—the number of elements (sizeof()) minus 1. However, if only one name is needed—the name of that array—this approach might provide some code-writing economy for a longer list of inputs.
Array De fi nition and Properties
Array Sorting
Sorting data stored in an array is a common computing task and PHP supports several functions for this task. A user-supplied comparison function must return an integer value less than 0 if the first argument is to be treated as less than the second, 0 if they are equal, and greater than 0.
Stacks, Queues, and Line Crashers
For an array of strings containing uppercase and lowercase letters, use the very simple function shown in Document 6.8 strcasecmp() to perform a case-insensitive comparison of two strings, producing the elements in the order you probably want to see: apple, David, Sue, Xena. PHP provides a very friendly environment for implementing stacks and queues because arrays can be changed dynamically while a script is running.
More Examples
The Quadratic Equation Revisited
The real root calculations in Document 6.10b are the same as those in Document 4.5b, but this code assumes that the PHP script does not automatically "know" the names of the quadratic coefficients and that the string containing these coefficients can use keys other than consecutive integers, which start at 0. Note that PHP assumes these are integers starting at 0 because no index values are given for the coeff [] array in Document 6.11a.
Reading HTML checkbox Values
You can conclude that the code presented in these examples is not overly simplified and is not worth the extra effort, because the PHP application still needs to "know" the name of the array of coefficients entered in the form fields of the HTML document. This data is very easy to process with PHP if the HTML document is written correctly, as shown in Document 6.13a.
Building a Histogram Array
This code in document 6.1.34 assumes that a certain number of histogram arrays divide the data range evenly. It would also make sense to use this element to store only the number of data values that fall within the specified data range.
The Purpose of Functions
This space and the operations performed in it are not visible to the rest of the script, including other functions within that script, except along two special paths.1 The large arrow represents the path to the function through its parameter list. The third file is a Windows file saved without pressing Enter at the end of the line.
User-De fi ned Functions
Recursive Functions
There can also be problems with large values of n due to the limited range of integers; a. For more complicated recursive algorithms, it can be difficult to follow the progress of the calculations.
Built-In Math Constants and Functions
The smallest integer (still a typefloat) greater than or equal to x. float)cos((float)x) Cosine of x,±1 (float)cosh((float)x) Hyperbolic cosine of x (float)deg2rad((float)x) Convert x in degrees to radians (float)exp( (float) x) here is the power of x (ex). float)floor((float)x). The smallest of two or more values or the smallest value in an array. float)pi() Returns the value of p, identical to M_PI (number)pow((number)x,.
More Examples
Legendre Polynomials
A series of functions called Legendre polynomials are sometimes used in scientific and engineering applications.3 Here is a table of the first 8 Legendre polynomials. Write an application that generates the value of the first n Legendre polynomials for each value of x and n0.
Kepler ’ s Equation
This is another example of a computation that is very easy to implement as a recursive algorithm because the function is defined that way. Write a script to calculate the true anomaly of an orbiting object as a function of time from perigee for a specified semimajor axis and eccentricity.
File Handling and Input/Output Functions
- Opening, Closing, and Moving Files
- Format Speci fi ers
- Reading Files
- Creating and Writing Files
- Examining Variables and Displaying Output
New data is added to the end of the file without changing the data previously present in the file. When reading a file of unknown length, it is often useful (for example, in conditional loops) to know when you have reached the end of a file.
String Handling Functions
Performs a case-sensitive comparison on the first $n_char characters of $s1 and $s2.strncmp() returns 0 if $s1 and $s2 are identical, an integer less than 0 if $s1 is less than $s2 (in the lexical meaning), and an integer greater than 0 if $s1 is greater than $s2. int)strpos((string) $s,(mixed) $lookFor. If $length is greater than or equal to the length of $s1, a warning is displayed.
Array-Related Functions
Literal keys are unchanged. array_shift() is used to remove the oldest element from the array, which is treated as a queue. You can use this to sort an array in descending order instead of the default ascending order.
Some Miscellaneous Functions and Constructs
With four parameters, $character will be used before significant figures and $separator sets the character used to separate thousands groups. The custom in many other countries is to specify dates as dd/mm/yy or dd/mm/yyyy.) strtotime() can be used to determine whether a date comes before or after another date.
More Examples
- Processing Wind Speed Data
- Calculating the Mass of Solid Objects
- Processing .bmp Image Files
- Converting Strings Containing Dates and Times to
Because of the structure of .bmp files, it is easy to insert hidden text within an image. This information may also be included in the padding bytes for the first (bottom) row of the image, for example.
Introduction
Creating a Space for Graphics Applications
These lines define the title of a graph and provide values that define the width and height of the image space in pixels, the starting coordinates for the title, the starting point (0,0) for the axes of an x-y graph, and the length of the graph . x and y axes. An important point about the image space is that the coordinates (0,0) are in the upper left corner and the pixels (300,200) are in the lower right corner; that is, the x-axis plots from left to right, as you would expect, but the y-axis plots from top to bottom, "upside down" from how you would normally think of graphs in (x,y) space .
Pie Charts
Horizontal Bar Charts
Vertical Bar Charts
Line Graphs
Summary of Some GD Graphics Functions
Create and Save GD Image Space, Display Images
Draw Lines and Shapes
Display Text