First thing about PHP, its too much stuff to have it all in your brain!
So the best thing is to have the manual always handy. The best manual is the official one, that you can find in php.net . There you can find 2 kinds of tutorial, one you can check on-line, the other one you download and see it off-line. My advice: download the CHM version of the manual, its easy to use and you don’t have to wait for the pages to download, besides, this way you can use it even if you don’t have internet.
In order for PHP to work, all the files with the code must have the PHP extension (i.e. “xpto.php“).
Inside each file, whenever you want to write PHP code, you have to enclose it with <? and ?> .
When you want it to output something, use the keyword echo.
First example
Now lets try the first example, create a file (using notepad or dreamweaver) called “index.php” and write the following inside:
<?
echo “Hello world!”;
?>
now go to a web browser and surf to your webserver. (http://localhost)
I hope that worked. ( if it didn’t, keep trying
)
Variables
Now let’s see about variables…
In matematics, you can do this: X=5 , and the X now has the value of 5. In programming its pretty much the same, only a variable can have numbers, letters, or whatever! For example, lets try this:
<?
$x= “Hello world!”;
echo $x;
?>
It should have the same result as the previous example. Now lets do something with numbers complete the code below, so that it will output the sum, the subtraction, the division and multiplication of those 2 variables:
<?
$x= 5;
$y=10;
?>
Now, do it again, adding only one line of code, but making the results come in different lines.
Flow control structures
until here everything was executed from top to bottom, however there are commands to change the way the code flows, executing only some code, going back, repeating or whatever… that’s flow control instructions (or commands, or structures).
The most important ones are: if…else…elseif ; while ; do while ; for ; switch
Check them out in the manual and do the following tasks:
task 1:
create a variable that will contain a number representing an hour of the day. Depending of the number in that variable, the script will return “good morning”, “good afternoon” or “good evening”.
task 2:
using only 3 lines of code, the script must repeat 20 times “Do files get embarrassed when they get unzipped?” (or whatever you want it to repeat). Create a variable containing the number of times the sentence must be repeated. If you change the number to zero, it should return nothing.
task 3:
the same as the one before, but if you put the number to zero, it will still print the sentence at least once.
task 4:
the same as task 2 but using a different control structure and only one line of code.
task 5:
create a variable that will contain a number, that number will represent a software error. Create 5 error codes and message. When you change the code, the message must change. Don’t use the IF statement.
Functions
Suppose you have to cover a wall with small papers that have the same drawing. Lets say you would need 1000 papers like those. Would you draw all the 1000 papers?!
the same happens with programming, if you write code that you might need later, you put it in a function. Besides, if you need to change it, you only change one piece of code and not 1000 pieces.
task 6:
- make a function that returns the contents of a text file
- make another function that, given some text, encloses it in html tags to change the text colour to yellow.
- make a function that saves some text into a file.
- in the main script use the functions in that order and in the end use the first function again to show the file contents in the screen.
Sessions
Check out this tutorial, its quite good and simple.
task 7:
Now I want you to add in the web page you previously made, links to each of the CSS files you have made. Use the PHP sessions to change the CSS file being used and then refresh the web page.
task 8:
Make a login script. You need:
- one regular txt file with the password in it
- one frontend login page
- a backend page with the php code
- one function to read the password from the file into a variable
- one function to check if the passwords match
- script to create the session and move on, or output an error message
task 9:
Make a chopping cart. Try to do it with no tips!
hiiiii o que é uma variavel???? lol
força!
Excelente. Também posso ir às aulas? O php estava-me a fazer jeito.