php basics
php code is included in a standard html page (there could be some exceptions). This is a standard programming language (but interpreted). It starts with <?php mark and is on-fly translated up to ?> sign. For example, command "echo" is the main command to output any data to the html page text:
|
If you execute this page from an internet server and then check the page source, you will see:
|
php command is translated on a server, so the client cannot see the source. It is important, if you do not like to share your program with the rest of the Internet community, but the main advantage is, that if you debug your program, it will work independently on a client software.
Variables
The name of a variable has to begin with a "$" sign, and continue with only letters and numbers (no spaces). Variables don't need to be declared. The server will create them when first used.
|
Cycles
Most important for us now is the "for" cycle. The
declaration is the same as in the C language. On the first line
of the next example there are three commands - the cycle variable
initialization, the comparison and the increment of the cycle
variable - separated by ";" (semicolon).
On the other lines, the body of the cycle is written; it is
separated from the rest of the page by "curly
brackets", i.e. { and }, which means "a block of a
program". In the cycle, the first line of the cycle is
executed - the variable is inicialized, then its value is
compared with the condition. If the condition is true, the body
of the cycle is executed. Then the cycle variable is increased
(the third command in cycle header) and the cycle will be
repeated :
|
This example will write ten-times the same parameter. Try it, and then check the page source (in Firefox or MS Explorer).
* There is something missing - what? (try to execute this script, or compare the result with the next example).
Typical school example is a star triangle:
|
Inside of the body of the cycle is the second cycle.
Forms support
The heading of a form must contain the "action="
parameter, if you like to transfer the data to the server.
Another important parameter is, how to transmit data to the
server: you can use either the method="get" , or
method="post" .
For executing a form, there should be a "submit" button
(another possibility is to use a Javascript function for page
submitting).
Most important are the input objects; each of them has to have a
(unique) "name" parameter, to be send to server.
|
On the first version of php, each of the form object was passed to php as a variable, with the same name, as input button. Now, we have to allow the "register_global" condition, to get them - if it is our case, we can write:
|
On the most of servers, data have to be read from associative arrays, named $_GET and $_POST:
|
or more safe
|
In the previous example, the dot (".") operator connects strings to one (the variable name is outside the quotation marks).
In some cases, there could be useful to list everything, what was sent by a form. It could be useful for debugging; see the "universal form response" example for my solution.
Notes: