[BLAMINATORS TUTORIAL TO PHP]
Just whipped up an intro for some people to read :P, enjoy
This tutorial will take you through the basics of PHP, and by the end of it you will be able to create small and simple PHP scripts.
PHP is a powerful language, which its main aim is to create dynamic web pages, which is what Blam Forums is built on, you will notice that all threads within Blam Forums are all from showthread.php, but each one has different content, this is a dynamic web page.
But before you learn how to create dynamics within PHP you must first learn some of its basic rules, such as syntax, which is best translated into grammer. Like all languages, english, french, german etc, you have certain ways of writing sentances, using grammer. Syntax is the same, there are certain ways to write PHP, otherwise it is wrong and wont work.
Step one would be to know the PHP tags, tags like HTML tags, shown below:
Anything inbetween those tags is treated as PHP and is executed, anything outside them is treated as HTML. But don't forget that your php files need to have the extension ".php".
The next step is to learn about variables, also known as "vars", these are best described as temporary memory, they can contain huge amounts of data, and stored in a small name. Lets look at how we would declare a variable:
PHP Code:
$MyVar = "Hello World!";
Okay, several things to take note of here, firstly the variable name, which is "$MyVar", and yes, the doller sign needs to be infront of every declared variable. The variable can be alphanumeric, with underscores, but not with spaces.
Next we are putting an equals sign after the variable, so show that we are "declaring" it, or in simple terms, putting data into it, also known as a "string" (ie: "String Of Data"). After that we put down what we are storing within the variable, which is "Hello World", due to this been text we need to put quotes around it, so PHP knows its a string of data and doesn't treat it as PHP syntax.
After the quotes we have a ;, this is part of PHP syntax, about 95% of PHP needs this at the end of a line, its best to think of it as a full stop. If you don't put it in, you can get all kinds of errors ;)
So now you know how to declare a variable, but i told you that PHP could make pages dynamic, and you're probably wondering how do i output data on the page.
Outputting data onto a page requires to use of a command, known as echo, this will output anything to the webpage that you specify. An example of it is shown below:
PHP Code:
echo "Outputting Data";
As you can see the syntax applies here, but there is no equals sing because we arn't declaring a variable, we are using a command.
The above example ouputs data, but what if you wanted to output data from a variable? This can be done too, shown below:
Notice how there is no quotes this time, because it is a variable and not plain text, so the PHP will swap the variable with its contents. (The quotes are delcared when we set the variable)
Now you know how to ouput plain text and variables, the next step is to output them both together.
PHP Code:
$Name = "Blaminator";
echo "Good Morning " . $Name . ", There are 10000 new posts.";
Here we have quotes around the plain text, but when it comes to putting a variable in, we put in an ending quote, but then a fullstop, PHP treats the fullstop as a joiner as such, it will join the 2 things together, or in this case 3 things, as we have "PlainText Variable PlainText".
Now you will see how PHP can be dynamic, using an "If Conditional", which is designed to check if something is true or false, and depending on the result you can execute different code.
An example If Conditional is shown below:
PHP Code:
$Name = "Blaminator";
if($Name == "Blaminator"){
echo "Your Name Is Blaminator!";
** else {
echo "Your Name Is Not Blaminator.";
**
It may look like its crazy complicated, but it is really simple when looked at in a different perspective, i will re-write the above code in "pseudo-code", this is half-code, half english, used to give you guys a better understanding.
[php]
$Name = "Blaminator";
If $Name Equals Blaminator Then
echo "Your Name Is Blaminator";
Else
echo "Your Name Is Not Blaminator.";
End If
Read that code outloud(
), and you will see it is very simple. But getting back to the propper If Conditional, a couple of syntax rules.
The if conditional uses { brackets, anything inbetween these are what is executed depending on which side of the statement is on (if its true or false).
These brackets dont have a ; after them, it isn't required. And lastly you will notice that the statement has 2 equal signs instead of 1, this must be applied.
Building onto the If Conditional, it can have several other conditions joint onto it, an "Else-If Statement", an example of one of those is shown below, but it doesn't need explaining:
PHP Code:
$Name = "Blaminator";
if($Name == "Blaminator"){
echo "Your Name Is Blaminator!";
** else if($Name == "Bob"){
echo "Your Name Is Bob!";
** else if($Name == "Bill"){
echo "Your Name Is Bill!";
** else {
echo "Who are you?";
**
Next, functions, also known in other programming languages as routines. Lets look at how to construct a function:
PHP Code:
function FUNCTIONNAME(PASSED-VARS){
**
The function name, like variables has to be aplphanumeric, and can contain underscores, with no spaces, however a doller sign is not needed here.
The passed-vars is an area where variables can be sent into the function when its called, we will look at this after a little more on functions.
Here is a basic function, doesn't do much, but its a function:
PHP Code:
function pastedata()
{
echo "Hello guys";
**
Now, if you put that in a php file you would fine that Hello World would NOT appear, any code within functions is not executed unless the function is called, so lets see how to call this function:
Jjust put the function name, the brackets, and ; at the end, the function will be executed. Now lets look into those passed-vars i was talking about earlier.
PHP Code:
function AddNumbers($Num1, $Num2){
$Answer = $Num1 + $Num2;
echo $Num1 . " + " . $Num2 . " = " . $Answer;
**
Note that the variables "$Num1" and "$Num2" do NOT need to be declared, these are just names given for when the function is called, calling this function is shown below:
PHP Code:
AddNumbers(4,5);
So when the function is called, it stores 4 in $Num1, and 5 in $Num2, just as names for the function to use within its code. The function has a minor maths equation, but nothnig that would trouble you.
Basic Mathmaticle operators are: + (Plus/Addition), - (Minus/Take-Away), * (Times/Multiply), / (Divide)
If that all makes sense to you then you will be able to tell the output on that page would be: "4 + 5 = 9" (Without the quotes)
So whats the point in functions, well if you wanted to execute a math equation, like in the function above, but several times, it would be a pain to just keep writing the same equation over and over, but with that function, i can just call the function several times over, less writing, less code, and more effecient.
Well that's the introduction to functions, variables and if conditional statements. More soon :)
Source: Blam Forums Tutorials
In this tutorial I’m going to teach you how to make your own shoutbox! I assume you have simple knowledge of PHP and MySQL. I'll do it in plain text because this is a coding tutorial, not deisning! Well first we need to think of what the steps will be. Here’s a little scheme of the project.
- Creating a MySQL table where the data will be stored, it will have 5 columns. ID, IP, Name, Time, Message.
- Creating a HTML form where people can add a shout. It will contain: Name, Message.
- Creating a PHP script that inserts those values into the appropriate columns in the MySQL table.
- Creating a PHP script that echoes (displays) out the content of the MySQL table, with a limit of 5.
Step 1:
The MySQL table:
Code:
CREATE TABLE `shoutbox` ( `id` TINYINT( 10 ) NOT NULL AUTO_INCREMENT ,
`ip` VARCHAR( 30 ) NOT NULL ,
`name` VARCHAR( 30 ) NOT NULL ,
`time` VARCHAR( 30 ) NOT NULL ,
`message` VARCHAR( 250 ) NOT NULL ,
PRIMARY KEY ( `id` ) );
This sets up a MySQL table where the values will be added in later.
Step 2:
Creating the HTML form
This step shouldn’t cause any problems because it is just plain HTML. Create a new .PHP document and call it shoutbox.php. Type this code:
[html]
<form method=“POST” action=“shoutbox.php”>
Name: <input type=“text” name=“name”><br> //field with the name ‘name’
Message <input type=“text” name=“message”><br><br> //field with the name ‘message’
<input type=“hidden” name=“posted” value=“true“> // hidden form field containing value “true”
<input type=“submit” value=“Shout!”>
[/html]
Step 3:
Creating a PHP script that inserts those values into the appropriate columns in the MySQL table.
We need to create a database file. Type this code and save the file as 'connect.php'
PHP Code:
<?php
$connect = mysql_connect(“host”,”username”,”password”) or die(“Failed to connect to host”)
$db = mysql_select_db(“database”) or die(“Failed to connect to database“);
?>
This file just simply connects to the database. We did this so we can include it.
Now type this code in the .PHP document before the form.
PHP Code:
<?php
include(“connect.php”)
If(isset($_POST[‘posted’]))
{
$name = $_POST[‘name’];
$message = $_POST[‘message’];
$time = time();
$ip = $_SERVER[‘REMOTE_ADDR‘]
$check_name_length = strlen($name); // checks length
$check_message_length = strlen($message); // checks length
If($check_name_length => 30) || if($check_message_length => 250) // if statements for length
{
echo “Please fill in the textboxes properly.”;
** else {
mysql_query(“INSERT INTO shoutbox( id, ip, name, time, message) VALUES (“NULL”,”$ip”, “$name”, “$time”, “$message”)”) or die (“Failed to add shout”);
**
**
Well this was quite a long script but it checks if the values don’t exceed the max. lengths (30 and 250).
Step 4:
Creating a PHP script that echoes out the content of the MySQL table, with a limit of 5.
Now for the last step. The page that actually puts the text on the screen. It’s very simple. Make a new PHP document and name it show_shouts.php. Now for the code:
PHP Code:
<?php
include(“connect.php”);
$query = Mysql_query(“SELECT * FROM shoutbox DESC id LIMIT 5”);
while($r = Mysql_fetch_array($query)
{
$time = $r[‘time’];
$name = $r[‘name’];
$message = $r[‘message’];
**
echo “Shout by $name at $time:”;
echo $message;
?>
Ok, this basically gets the values out of the database table and puts them on the screen. I hope you all learned something from this tutorial.
Thanks for reading.