A tiny php dcript beyond “Hello World”
A simple problem that can be presented to us and that cannot be solved using only HTML is that a page is available only the first 10 days of the month.
We will display a sign saying that the site is available if the date is less than or equal to 10, otherwise we will display a site out of order message.
To obtain the date from the web server we must call the date function and require only the day:
$ dia = date (“d”);
Variables in PHP are preceded by the $ character. If we pass the string “d” to the date function it will return only the day (if we want the complete date: $ date = date (“Y: m: d”)
To verify if the variable $ dia is less than or equal to 10, we must use the if statement, similar to other languages.
Then the page with the program is as follows:
$day = date (“d”);
if ($day <= 10)
echo “active site”;
echo “site out of order”;
Variable names are case sensitive, so if we write it in lowercase initially we must respect it in the rest of the program. In contrast, the PHP language instructions are not sensitive, so if we want to write IF or if, the two forms will be fine. Those of us who come from other languages such as C, C ++, Java, have the habit of writing the keywords in lowercase, but this is only out of habit.
The if condition must necessarily be in parentheses. The available relational operators are:
> = Greater than or equal
<= Less than or equal
! = Other
If the condition is verified true, the first block enclosed in curly braces is executed; if the condition is verified false, the block is executed in curly braces that follows the else.