Pages

Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

Wednesday, September 15, 2010

Tutorial PHP & MySQL – Part 2: Your first scripts

OK. So now we should go to the folder where the PHP files are stored. If you’ve installed XAMPP to the standard directory, the web folder should be in C:/XAMPP/htdocs. Now make a folder with the name, for example, ‘php’. It’s time to make a first script to test the functionality of PHP.

Step 1: start Notepad++ (or any other text editor you want to use)
Make a standard HTML page, like the one below (it’s not necessary by the way):
<html>
<head>
<title>Test page</title>
</head>
<body>
</body>
</html>
Now, between <body> and </body>, put the code provided below:
<?php
echo “Hello world!”;
?>
A PHP script always starts with a ‘<?php’ tag and always ends with a ‘?>’ tag. The echo statement brings the text you typed between the double quotes to the screen. A line of PHP code mostly ends with a semicolon (;). Now let’s save our script and test it. Go to the folder ‘htdocs’ and save the file as, for example, test.php in the folder you’ve created (in my case the folder ‘php’). Now lets test it. In your browser type ‘http://localhost/php/test.php’. You should see a web page with ‘Hello world!’ on the screen now.

Let’s talk about variables now. This is very important in programming in PHP (and not only in PHP, in all programming- and scripting languages). Variables can store data, data collections and many more things for you to call it later on. Let’s explain it with a piece of code (make a new file or type this in your existing file):
<?php
$name = “Duco”;  //PHP variables always begin with a dollar sign.
$age = 19;   //Number variables (integers) don’t need double quotes, text (strings) do.
echo “Hello, my name is “.$name.”, and I’m “.$age.” years old”;  //I always use dots to seperate plain text from variables, it looks clearer in my opinion.
?>
If you try this in your XAMPP webserver, it will say “Hello, my name is Duco, and I’m 19 years old”. Now play with the values of the variables. Variables are very useful. If you, for example, have to write a date four times, you can store the date in a variable. And if you want to change the date, you only have to do it once.
There are four major types of variables you will use a lot in PHP:
- Integers: numbers (i.e. $number = 16;)
- Strings: text, it varies from one character to whole stories (i.e. $string = “Groningen”;, $string = “B”;)
- Decimal: an integer with a decimal value (i.e. $decimal = 15.41;)
- Boolean: a representation of ‘true’ (1) or ‘false’ (0) (i.e. $bool = true;, $bool = 0;)

Now let’s do some comparations. PHP has some comparison signs to check things.
<: smaller than
>: bigger than
<=: smaller than or equal to
>=: bigger than or equal to
==: equal to
!: not equal to   //for example, !< (not smaller than), != (not equal to)

Now for some code:
<?php
$age = 15;
if($age >= 18)    // If your age is equal to or bigger than 18…
{
echo “You can take driving lessons.”;
}
else
{
echo “You can’t take driving lessons, you’re too young.”;
}
?>
This script will say “You can’t take driving lessons, you’re too young.”. Now change $age to 18. It now will say you can take driving lessons.

You also saw the if-else construction. It’s necassery in any scripting- and programming language. If the conditions aren’t met, the actions written between the else brackets will be performed (by the way, you can’t use ‘else’ if you haven’t used an ‘if statement’ before. You can however use an’ if statement’ without an ‘else’). The open- and close brackets are necessary at each check.

Well, that was the second part of the tutorial. And if things aren’t clear, please say it, because this is my first tutorial.

Tutorial PHP & MySQL – Part 1: Setting up your environment

A must-have for a programmer is to have a piece of software on his computer to directly test his webapplication. You make a file, and you can directly test it in your browser. The other way to test a file is to upload it to your webspace and then test it. It’s way easier to do this on your own computer (and you don’t have the restrictions your webhosts charges you). I’m going to explain how to get this piece of software and how to install it.


The piece of software we’re going to install is XAMPP (This stands for X Apache MySQL PHP Pearl. The X stands for ‘many operating systems’. I’ve never used Pearl, so don’t ask me about it). This is a package which contains Apache, a program which serves the webpages for you and interprets PHP files as PHP files. Visitors can’t see the source code of your PHP-application, so it’s pretty safe. It also contains MySQL, an essential program for programmers who want an interactive website. It stores all your data (like login data), so you can access it later on. It also contains an e-mail package (I’m going to talk about this later, I haven’t figured out how this program works yet) and a FTP-package (which we aren’t going to use).

Step 1: download & install XAMPP
Go to http://www.apachefriends.org/en/xampp.html, and select XAMPP for Windows (or XAMPP for another OS if you don’t have Windows, but since I have Windows, I’ll pick this one). Under ‘Download’, pick XAMPP. Now pick the EXE- or ZIP-file.

Step 2: start XAMPP
Starting XAMPP is pretty easy. After you’ve installed XAMPP, go to Start Menu and start ‘XAMPP Control Panel’ from the folder ‘XAMPP for Windows’. Now you see a window with four start buttons. Push the start buttons at ‘Apache’ and ‘MySql’. If it’s OK, you should see two green boxes with ‘running’ in it next to the buttons. You could also check the ‘Svc’ checkboxes next to ‘Apache’ and ‘MySql’. This will install ‘Apache’ and ‘MySQL’ as services, so it starts with your operating system.

Step 3: trying XAMPP
Start your favourite internet browser. In the address bar, type ‘localhost’. This will bring you to the page of your local webserver (Apache in this case). Now you are on the standard XAMPP web page. Play around with it for a bit, it gives you an idea with what you can make with PHP and MySQL.

Step 4: install a nice text editor
To make and edit your PHP files, you will need some kind of text editor. You could use Windows Notepad, but it’s way too simple in my opionion. I always use Notepad++. It has the possibility to install plugins (like an explorer plugin). You can download the program here.

Well, this was the first part of the tutorial, and my very first tutorial ever written. If things aren’t clear, please make a comment.