Are you new to php? Confused where to start? Well, don't worry you have come to the right place.
This tutorial will cover the following:
- What is php?
- What is required and how to run your php scripts?
- Brief overview of webservers Apache and IIS
- Installing Xampp on vista/windows 7.
- A simple example.
What is php?
- PHP is short for Hypertext Preprocessor.
- It is an open source server side scripting language; hence free to download.
- Its main purpose is to make web applications dynamic.
- It is normally used with other scripting languages namely Javascript ( a client side language) and HTML.
- A PHP script is saved with .php or .php3 extension; .php is mostly used.
- PHP works fine with various database namely MySQL, Oracle, PostgreSQL etc.
What is required and how to run your php scripts?
Unlike HTML and Javascript, PHP scripts cannot be interpreted solely by
the browsers. For executing PHP scripts we need a web server (remember
we mentioned above, "PHP is a server side language") that supports php;
to name a few we have Apache, IIS.
Brief overview of webservers Apache and IIS
For you beginners, I will recommend downloading and installing Xampp; a freeware which is an all in one application comprising of Apache server, Mysql as database and supports PHP.
Another alternative would be to use a free hosting servicewhich provides pre installed Apache,mysql services.
(You can try X10Hosting or 000webhost hosting service. You will have to create an account first ) .
Installing Xampp on vista/windows 7:
- Run the .exe application.Click ok.
- After installation, cmd.exe prompt will appear. Enter y (yes) or n (no) to customize the type of installation you need.
- When done press 1, when you see the screen below:
4.Now you shall be see the Xamp Control panel.
5. Check Apache->click ok.
6. Check Mysql->click ok.
7. Now to start Mysql and Apache service->click start for each.
8. If successful, Running highlighted in green shall appear next to Apache and Mysql (see screenshot below)
Let's get started(PHP syntax)
- Every php script starts with <?php and ends with ?>.
- PHP codes go within the <?php ?> syntax.
- To print/output a plaintext, we use echo keyword.
- For example echo 'a' will output a.
- The text can be enclosed in double or single quote, either echo "a" or echo 'a'.
- To comment a line, we use // (for single line comment) or /* comments */(for multiline comments)
- Each line of code ends with a semicolon (;)
- To run a php script locally, we use http://localhost/file.php.
A simple example:
- We will write a small script to test whether xampp was successfully installed.
- The script will print "xampp was successfully installed ".
- Open notepadd++ or any text editor.
- Type in the code below:
<?php
echo " xampp was successfully installed " ; // use slash to comment a single line comment
/*
to comment more than 1 line
start with slash star
end with star slash!
*/
?>
3. Save script as test.php in htdocs folder (go to c:/xampp/htdocs).
4. Open your browser type "http://localhost/test.php".
You shall see the output 'xampp was successfully installed' ( see screenshot below).
Am a beginner, I found this blog really useful. Thanks.
ReplyDelete