Mario
12-26-2009, 05:16 PM
Tutorial Wrote By Mario A.K.A Str1k3r21.
Overview Index
1. Syntax
1.1 - Enabling PHP
1.2 - Comments
1.3 - Semi-Colon ( ; )
1.4 - Echo
1.5 - Jump Line
1.6 - Joint Together ( . )
1.7 - If .. else ..
1.8 - Variables
2. Handle Files
2.1 - fopen();
2.2 - fclose();
2.3 - fwrite();
2.4 - fgets();
3. Cookies
3.1 - SetCookies
3.2 - Is Cookie Set
3.3 - Using Cookies
4. Handle MySQL
4.1 - mysql_connect();
4.2 - mysql_close();
4.3 - mysql_select_db();
1. Syntax
1.1 - Enabling PHP
<?php
?>
It doesn't matter where the '?>' is at, as long as it's after '<?php'.
It's best to do it as shown above to start off with.
Reason is because your eyes already make the 'Start' & 'End' of it automatically.
If I was to do it like this,
<?php ?>
It would become really messy after adding a few lines to your PHP Script.
Your eye would have to search for the 'Start' & 'End' of it.
1.2 - Comments
Comments are used to help you.
They are not executed in the code, and are only visible when you write the PHP File.
Commenting a Line
<?php
// Comment
?>
Block Commenting
<?php
/*
The '/*' sets the start of the Block Comment.
The '*/' sets the end of the Block Comment.
In-between can be anything and as long as you like.
Just keep the text you want commented between '/*' & '*/'.
*/
1.3 - Semi-Colon ( ; )
You must put a Semi-Colon at the end of every command.
That's how PHP Process's it.
Good Ex:
<?php
echo "semi-colons";
?>
Bad Ex:
<?php
echo "semi-colons"
?>
1.4 - Echo
It is used to output text/variables/etc.. on your page.
<?php
echo "Hello !";
?>
1.5 - Jump Line
This feature is used to jump to the next line.
<?php
echo "Hello !<br \>";
echo "You've successfully Jumped the line !";
?>
That will look like this,
Hello !
You've successfully Jumped the line !
1.6 - Joint Together ( . )
The dot is used to joint together a variable/function/text/etc..
Here's an Example.
<?php
echo "Hello! " . "I'm Mario!";
?>
That will output the following ..
Hello! I'm Mario!
1.7 - If .. else ..
If is something self-explained.
It's just a check to see if something is on, do this.
Else, do this.
Example:
<?php
if( .. )
{
// Do Something
}
else
{
// Do Something
}
?>
I didn't know what to use for this so I put '..'.
1.8 - Variables
In PHP there is only one type of variable and you declare it always the same way.
You must always put '$' first and then the variable's name afterwards.
Let's declare some text,
<?php
$name = "Mario";
?>
That will save 'Mario', into the variable '$name'.
If you were to output '$name', it would be the same thing as outputting 'Mario'.
Let's declare a number,
<?php
$age = 15;
?>
Later on in this tutorial I will show you how it can declare a function.
2. Handle Files
2.1 - fopen();
Function fopen(); is used to create/read a file.
It has 2 parameters.
First is the file's name + extension.
Second is a specification telling PHP that your opening the file to WRITE ( 'w' ) to it or READ ( 'r' ) from it.
We will use a variable here so it's easier to call it afterwards.
<?php
$HandleFile = fopen("example.txt", 'w') or die("Can't Open File!");
?>
The above code create/open 'example.txt' file in the same directorie as your PHP Page is uploaded.
Also, the second parameter is 'w' telling PHP that it's preparing to write something.
Don't forget to close the file after use ( fclose() ).
2.2 - fclose();
Function fclose(); is used to close a file.
It has 1 parameter.
You must enter witch File Handle to close.
Here's how.
<?php
$HandleFile = fopen("example.txt", 'w') or die("Can't Open File!");
fclose($HandleFile);
?>
2.3 - fwrite();
Function fwrite(); is used for writing to a file.
It has 2 parameters.
First one is the File Handle.
Second one is what you want to write in it.
Example:
<?php
$HandleFile = fopen("example.txt", 'w') or die("Can't Open File!");
fwrite($HandleFile, "This is an example.");
fclose($HandleFile);
?>
2.4 - fgets();
Function fgets(); is used to read from a file.
It has 1 parameter.
I'm sure you've guessed it, the FileHandle.
Don't forget that since we're reading from the file, the 2nd parameter of 'fopen();' will be 'r'.
Like this ..
<?php
$HandleFile = fopen("example.txt", 'r') or die("Can't Open File!");
echo fgets($HandleFile);
fclose($HandleFile);
?>
The above will read what's in "example.txt" and output it on your page.
3. Cookies
Cookies are information stored temporarly into your web browser for later use.
3.1 - SetCookie
Setting a cookie is the first step.
To set a cookie we use the function 'setcookie();'.
It's minimum parameters are 2, but additional parameters can be added.
First parameter is the cookie's name.
Second parameter is the cookie's value (text/number/etc. ).
I will use a third parameter witch will set an expiration time.
Example:
<?php
setcookie("name", "Mario", time() + 3600);
?>
That will set a cookie named 'name' witch will contain 'Mario'.
It will expire 1 hour after the cookie is set.
3.2 - Is Cookie Set
Before actually calling/outputing the Cookie.
We must add a check to see if it is set and then to call/output it.
For that, we use the function isset();.
It only has 1 parameter, the cookie look at the syntax of how I called it.
Example:
<?php
if(isset($_COOKIE["name"]))
{
echo S_COOKIE["name"];
}
else
{
echo "Cookie Not Set !";
}
?>
That will check to see if the cookie 'name' is set.
If it is set, it will output it on your screen.
If it's not set, it will output 'Cookie Not Set !'.
4. Handle MySQL
4.1 - mysql_connect();
We use the function 'mysql_connect();' to connect to the MySQL.
Keep in mind that this function will be persistent in connecting.
It has 3 parameters.
First one is the host.
Second one is the username.
Third one is the password.
Here's an example:
<?php
$sql = mysql_connect("localhost", "username", "password")or die("Can't Connect !");
?>
Then you should be connected the the MySQL.
Note that if you open a connection, you must also close it ( mysql_close(); ).
4.2 - mysql_close();
The function 'mysql_close();' is used to close an active connection to a MySQL.
This will only work with a none-persistent connection.
It has 1 parameter, the SQL Handle.
Example:
<?php
$sql = mysql_connect("localhost", "username", "password")or die("Can't Connect !");
mysql_close($sql);
?>
It will connect to 'localhost' with the username 'username', and the password 'password'.
4.3 - mysql_select_db();
The function 'mysql_select_db();' is a self-explained function.
It selects the Database of your choice on that MySQL Connection.
It has 2 parameters.
First, the SQL Handle.
Second, the Database Name.
Use it like this ..
<?php
$sql = mysql_connect("localhost", "username", "password")or die("Can't Connect !");
mysql_select_db($sql, "Database")or die("Can't Select Database !");
mysql_close($sql);
?>
The code will look for the database named 'Database' in that Connection.
You are not allowed to post this on any other website as is content without my permission.If you want, you may redirect it to this Link.
Overview Index
1. Syntax
1.1 - Enabling PHP
1.2 - Comments
1.3 - Semi-Colon ( ; )
1.4 - Echo
1.5 - Jump Line
1.6 - Joint Together ( . )
1.7 - If .. else ..
1.8 - Variables
2. Handle Files
2.1 - fopen();
2.2 - fclose();
2.3 - fwrite();
2.4 - fgets();
3. Cookies
3.1 - SetCookies
3.2 - Is Cookie Set
3.3 - Using Cookies
4. Handle MySQL
4.1 - mysql_connect();
4.2 - mysql_close();
4.3 - mysql_select_db();
1. Syntax
1.1 - Enabling PHP
<?php
?>
It doesn't matter where the '?>' is at, as long as it's after '<?php'.
It's best to do it as shown above to start off with.
Reason is because your eyes already make the 'Start' & 'End' of it automatically.
If I was to do it like this,
<?php ?>
It would become really messy after adding a few lines to your PHP Script.
Your eye would have to search for the 'Start' & 'End' of it.
1.2 - Comments
Comments are used to help you.
They are not executed in the code, and are only visible when you write the PHP File.
Commenting a Line
<?php
// Comment
?>
Block Commenting
<?php
/*
The '/*' sets the start of the Block Comment.
The '*/' sets the end of the Block Comment.
In-between can be anything and as long as you like.
Just keep the text you want commented between '/*' & '*/'.
*/
1.3 - Semi-Colon ( ; )
You must put a Semi-Colon at the end of every command.
That's how PHP Process's it.
Good Ex:
<?php
echo "semi-colons";
?>
Bad Ex:
<?php
echo "semi-colons"
?>
1.4 - Echo
It is used to output text/variables/etc.. on your page.
<?php
echo "Hello !";
?>
1.5 - Jump Line
This feature is used to jump to the next line.
<?php
echo "Hello !<br \>";
echo "You've successfully Jumped the line !";
?>
That will look like this,
Hello !
You've successfully Jumped the line !
1.6 - Joint Together ( . )
The dot is used to joint together a variable/function/text/etc..
Here's an Example.
<?php
echo "Hello! " . "I'm Mario!";
?>
That will output the following ..
Hello! I'm Mario!
1.7 - If .. else ..
If is something self-explained.
It's just a check to see if something is on, do this.
Else, do this.
Example:
<?php
if( .. )
{
// Do Something
}
else
{
// Do Something
}
?>
I didn't know what to use for this so I put '..'.
1.8 - Variables
In PHP there is only one type of variable and you declare it always the same way.
You must always put '$' first and then the variable's name afterwards.
Let's declare some text,
<?php
$name = "Mario";
?>
That will save 'Mario', into the variable '$name'.
If you were to output '$name', it would be the same thing as outputting 'Mario'.
Let's declare a number,
<?php
$age = 15;
?>
Later on in this tutorial I will show you how it can declare a function.
2. Handle Files
2.1 - fopen();
Function fopen(); is used to create/read a file.
It has 2 parameters.
First is the file's name + extension.
Second is a specification telling PHP that your opening the file to WRITE ( 'w' ) to it or READ ( 'r' ) from it.
We will use a variable here so it's easier to call it afterwards.
<?php
$HandleFile = fopen("example.txt", 'w') or die("Can't Open File!");
?>
The above code create/open 'example.txt' file in the same directorie as your PHP Page is uploaded.
Also, the second parameter is 'w' telling PHP that it's preparing to write something.
Don't forget to close the file after use ( fclose() ).
2.2 - fclose();
Function fclose(); is used to close a file.
It has 1 parameter.
You must enter witch File Handle to close.
Here's how.
<?php
$HandleFile = fopen("example.txt", 'w') or die("Can't Open File!");
fclose($HandleFile);
?>
2.3 - fwrite();
Function fwrite(); is used for writing to a file.
It has 2 parameters.
First one is the File Handle.
Second one is what you want to write in it.
Example:
<?php
$HandleFile = fopen("example.txt", 'w') or die("Can't Open File!");
fwrite($HandleFile, "This is an example.");
fclose($HandleFile);
?>
2.4 - fgets();
Function fgets(); is used to read from a file.
It has 1 parameter.
I'm sure you've guessed it, the FileHandle.
Don't forget that since we're reading from the file, the 2nd parameter of 'fopen();' will be 'r'.
Like this ..
<?php
$HandleFile = fopen("example.txt", 'r') or die("Can't Open File!");
echo fgets($HandleFile);
fclose($HandleFile);
?>
The above will read what's in "example.txt" and output it on your page.
3. Cookies
Cookies are information stored temporarly into your web browser for later use.
3.1 - SetCookie
Setting a cookie is the first step.
To set a cookie we use the function 'setcookie();'.
It's minimum parameters are 2, but additional parameters can be added.
First parameter is the cookie's name.
Second parameter is the cookie's value (text/number/etc. ).
I will use a third parameter witch will set an expiration time.
Example:
<?php
setcookie("name", "Mario", time() + 3600);
?>
That will set a cookie named 'name' witch will contain 'Mario'.
It will expire 1 hour after the cookie is set.
3.2 - Is Cookie Set
Before actually calling/outputing the Cookie.
We must add a check to see if it is set and then to call/output it.
For that, we use the function isset();.
It only has 1 parameter, the cookie look at the syntax of how I called it.
Example:
<?php
if(isset($_COOKIE["name"]))
{
echo S_COOKIE["name"];
}
else
{
echo "Cookie Not Set !";
}
?>
That will check to see if the cookie 'name' is set.
If it is set, it will output it on your screen.
If it's not set, it will output 'Cookie Not Set !'.
4. Handle MySQL
4.1 - mysql_connect();
We use the function 'mysql_connect();' to connect to the MySQL.
Keep in mind that this function will be persistent in connecting.
It has 3 parameters.
First one is the host.
Second one is the username.
Third one is the password.
Here's an example:
<?php
$sql = mysql_connect("localhost", "username", "password")or die("Can't Connect !");
?>
Then you should be connected the the MySQL.
Note that if you open a connection, you must also close it ( mysql_close(); ).
4.2 - mysql_close();
The function 'mysql_close();' is used to close an active connection to a MySQL.
This will only work with a none-persistent connection.
It has 1 parameter, the SQL Handle.
Example:
<?php
$sql = mysql_connect("localhost", "username", "password")or die("Can't Connect !");
mysql_close($sql);
?>
It will connect to 'localhost' with the username 'username', and the password 'password'.
4.3 - mysql_select_db();
The function 'mysql_select_db();' is a self-explained function.
It selects the Database of your choice on that MySQL Connection.
It has 2 parameters.
First, the SQL Handle.
Second, the Database Name.
Use it like this ..
<?php
$sql = mysql_connect("localhost", "username", "password")or die("Can't Connect !");
mysql_select_db($sql, "Database")or die("Can't Select Database !");
mysql_close($sql);
?>
The code will look for the database named 'Database' in that Connection.
You are not allowed to post this on any other website as is content without my permission.If you want, you may redirect it to this Link.