This tutorial is for anyone who has never touched PHP coding before...if you have, you'll find it kind of basic, but I will guide you through installing, and creating several basic scripts to get a feel for the language.
It would help if you know HTML, but this is not necessary for this tutorial.
Step One
Firstly, you will need to obtain access to a PHP Capable server.
Don't fear, for if you do not have access to a suitable web server, for there are plenty of easy options for creating scripts offline.
If you don't have access, then head on over to Apache Friends and download yourself a copy of Xampp.
This brilliant program will fully install and configure PHP, MySQL and Apache Web Server on to your computer.
Download, install the program. Install MySQL and Apache as services.
Once installation is complete, point your browser over to:
http://localhost/
and if the installation was successful, you will see the Xampp welcome screen.
Choose your language, and you are ready to go. Navigate on the left - you can access phpMyAdmin, the internet standard for creating and managing SQL Databases, but for now, we aren't worried about that.
Step Two
You are ready to begin coding your first PHP script.
Open up notepad or dreamweaver, or essentially any text application.
Start a new document, and type in the following:
Save the file as firstscript.php in this location:
C:\Program Files\xampp\xampp\htdocs
(I would suggest making a new folder for your scripts, so maybe:
C:\Program Files\xampp\xampp\htdocs\myphp\)
now, point your browser over to:
http://localhost/firstscript.php
or if you saved it in a folder..
http://localhost/myphp/firstscript.php
Hopefully, if all has gone well, you will see the following:
if you don't or get an error..something went wrong in the installation of php.
If you do, however, then, move on.
Step Three
PHP Code is a server side scripting language...all the content is dynamic and so everything is carried out on the server before being sent to the browser, as HTML. This means that your code is safe from people trying to steal it, and it enables you to do some awesome things with websites and applications.
The basic syntax is:
note php can be opened and closed many times within a document, it integrates very well with HTML and forms. You just must make sure the file is saved with a .php extension for the browser to parse php.
note, you terminate every instruction in php with " ; ". There are some exceptions, but as a whole, every line of code will end in ;, else there will be a parse error.
Variables are a crucial part in any language, and PHP is no exception.
Variables are declared like this:
Some of the basic syntax in php are listed below. You've already looked at one of the most common, ECHO.
Last thing for the tutorial for now, IF/Else statements.
These are vital for any application to have more than one possiblity, based on whether or not an arguement is true or false.
the strucutre for an IF/Else is:
in english this translates to:
If something is true/false (it can be either, or something more advanced such as if a value is present), then do something. Else, if this is not true, then execute something else.
It's easier in practice.
Load a new script:
load that, and see what the result is.
You should be displayed with the first message.
Now go back to the script and change the name in $name, to some random crap.
Now run the script again. As the $name is no longer equal to (==) "Waffles", the other code is executed.
And there we have..very, very, basic PHP.
It would help if you know HTML, but this is not necessary for this tutorial.
Step One
Firstly, you will need to obtain access to a PHP Capable server.
Don't fear, for if you do not have access to a suitable web server, for there are plenty of easy options for creating scripts offline.
If you don't have access, then head on over to Apache Friends and download yourself a copy of Xampp.
This brilliant program will fully install and configure PHP, MySQL and Apache Web Server on to your computer.
Download, install the program. Install MySQL and Apache as services.
Once installation is complete, point your browser over to:
http://localhost/
and if the installation was successful, you will see the Xampp welcome screen.
Choose your language, and you are ready to go. Navigate on the left - you can access phpMyAdmin, the internet standard for creating and managing SQL Databases, but for now, we aren't worried about that.
Step Two
You are ready to begin coding your first PHP script.
Open up notepad or dreamweaver, or essentially any text application.
Start a new document, and type in the following:
PHP:
<?php
echo "This is my first script!";
?>
Save the file as firstscript.php in this location:
C:\Program Files\xampp\xampp\htdocs
(I would suggest making a new folder for your scripts, so maybe:
C:\Program Files\xampp\xampp\htdocs\myphp\)
now, point your browser over to:
http://localhost/firstscript.php
or if you saved it in a folder..
http://localhost/myphp/firstscript.php
Hopefully, if all has gone well, you will see the following:
Code:
This is my first script!
if you don't or get an error..something went wrong in the installation of php.
If you do, however, then, move on.
Step Three
PHP Code is a server side scripting language...all the content is dynamic and so everything is carried out on the server before being sent to the browser, as HTML. This means that your code is safe from people trying to steal it, and it enables you to do some awesome things with websites and applications.
The basic syntax is:
PHP:
<?php //START php
//PHP Code here
?> //END php
note php can be opened and closed many times within a document, it integrates very well with HTML and forms. You just must make sure the file is saved with a .php extension for the browser to parse php.
PHP:
<?
#this, is also a comment. it won't be displayed to the user. good for annotating code.
/* And this, is also
a comment, stretching
over multiple lines
*/
?>
//You can also start and end PHP by using
<? ?> //but this is informal and not XHTML compliant
note, you terminate every instruction in php with " ; ". There are some exceptions, but as a whole, every line of code will end in ;, else there will be a parse error.
Variables are a crucial part in any language, and PHP is no exception.
Variables are declared like this:
PHP:
<?
$variablename = "variable value";
//note that variable names CANNOT start with an integer..they can start with letters or an underscore.
#also note that variable values must be contained in ""s unless it is an integer, in which case this is not necessary
$myname = "Waffles";
$_myage = 17;
?>
Some of the basic syntax in php are listed below. You've already looked at one of the most common, ECHO.
PHP:
<?php //start PHP
//function name
echo ("Something here"); //this function simply prints whatever text/variable/string was entered between the "'s and ().
//example usage:
echo "Hey, this is an echoed statement.";
//prints Hey, this is an echoed statement. to the browser
echo "$string";
//prints whatever value the variable string had
?>
Last thing for the tutorial for now, IF/Else statements.
These are vital for any application to have more than one possiblity, based on whether or not an arguement is true or false.
the strucutre for an IF/Else is:
PHP:
<?
if (something is true) {
// do something
} else {
// do something else
}
?>
in english this translates to:
If something is true/false (it can be either, or something more advanced such as if a value is present), then do something. Else, if this is not true, then execute something else.
It's easier in practice.
Load a new script:
PHP:
<?php
// IF/Else statement
//lets set up some variables..
$name = "Waffles";
if($name == "Waffles") { //it is, so:
echo "My name is Waffles!";
} else {
echo "My name is not waffles!";
} //end the statement.
?>
load that, and see what the result is.
You should be displayed with the first message.
Now go back to the script and change the name in $name, to some random crap.
Now run the script again. As the $name is no longer equal to (==) "Waffles", the other code is executed.
And there we have..very, very, basic PHP.