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:

<HTML>
 <HEAD>
 <TITLE>Page title</TITLE>
 </HEAD>
<BODY>
This text is written directly in html.
<?php
echo "This text is written by php\n";
?>

</HTML>

If you execute this page from an internet server and then check the page source, you will see:

<HTML>
 <HEAD>
 <TITLE>Page title</TITLE>
 </HEAD>
<BODY>
This text is written directly in html.
This text is written by php

</HTML>

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.

$abc = "Any text";
$ef = 11;
$klm = 2 + 3 * $ef;
$mn1 = "Any $abc here";
$mn2 = 'Any $abc here';

echo "Results: $klm, $mn1 and $mn2.";

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 :

for ($i=1; $i<11; $i++)
{
echo "This is a single line";
}

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:

for ($j=1; $j<11; $j++)
{
for ($i=1; $i<$j; $i++)
{
echo "***";
}
echo "<br>";
}

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.

<Form action="test.php" method="post">
<input type="text" name="abc" value="*****"><br>
<input type="text" name="ace" value="Alik"><br>
<input type="submit" value="Send it!">
</form>

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:

<?php
echo "$abc <br> $ace <br>";
?>

On the most of servers, data have to be read from associative arrays, named $_GET and $_POST:

<?php
echo "$_POST['abc'] <br> $_POST['ace'] <br>";
?>

or more safe

<?php
echo $_POST["abc"]." <br> ".$_POST["ace"]." <br>";
?>

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: