Welcome To My Second PHP tut.
In order to understand this tutorial, you will need either basic knowledge in PHP, or to have read my previos tutorialmon PHP.
In this tutorial I will be covering:
-= Arrays
-= Pre-defined Variables
-= IF statements
-= Comments
Onto the tutorials…
Arrays are very simple to grasp and very useful when used in the correct manner.
to define an array in a variable your code would look something like this:
<?php
$array = array(„one“, „two“, „three“);
?>
Now in the variable ‚array‘ we have 3 peices of data, one two and three.
Now to call on a peice of data in an arrayed variable you need to call apon it via a number.
So if we wanted to echo the peice of data ‚one‘.
Our code would look something like this:
<?php
$array = array(„one“, „two“, „three“);
echo $array[0];
?>
That would successfully echo ‚one‘.
As you may be wandering, why is it called apon as 0?
This is just how the php developers created it.
0 is the first number in a php sequence and so is the first number in an array.
so if i wanted to call apon and echo ‚two‘ my code would be similar to this:
<?php
$array = array(„one“, „two“, „three“);
echo $array[1];
?>
Hopefully you understand now.
If not please send me an instant message over MSN or send me a private message over DarkMindZ as i’ll be more than happy to explain.
Aswel as calling apon arrays as the default numbers you can set a value to call apon, for example, we’re going to call the peice of data ‚three‘ as $array[bob].
This is what our code would be similar to if not identical to:
<?php
$array[bob] = „three“;
echo $array[bob];
?>
You can also use apostrophies before and after the value name for example instead of $array[bob] you could use $array[‚bob‘].
It makes no difference (that I know of) to your script apart from space and neatness.
There is also one more way of doing this to the extent that im showing it as. That was is shown below:
<?php
$array = array(„one“ => b, „two“ => o, „three“ => bob);
echo $array[bob];
?>
That would successfully echo ‚three‘.
Now i hope you understand the renaming of array values.Also i would like to show you one more thing on variables.
How to change a price of data in a variable in an array without changing the whole array:
<?php
$array = array(„one“, „two“, „three“);
$array[2] = „four“;
?>
Now the peice of data ‚three‘ would become ‚four‘.
I sinceaerly hope you understand everything so far..
– Moving onto Predefined Variables –
=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
There are Three wisely used predefined arrays.
They are:
$_SERVER
$_SESSION
$_COOKIE
$_POST
$_GET
There is also one called $_GLOBAL but it is very underused and shouldnt be used until a last resort (In my opinion that is).
These are predefined arrays and variables..
I’ll start off explaining $_SESSION:
the session variable / array is often used in web based shells to show server information and user information such as IP address etc..
Here are a few examples:
‚REMOTE_ADDR‘ – The IP address of the visitor to the page.
‚SERVER_ADDR‘ – The IP address that the script is currently on.
‚SERVER_NAME‘ – The Name of the server that the script is currently on.
‚SERVER_ADMIN‘ – The Email Address (if set in php.ini) of the Webmaster of that server.
‚PHP_SELF‘ – The name of the script currently running.
They are simply used, as all of predefined variables above like this:
$_PRE-VAR-NAME[‚value‘];
so say i wanted to store the email address of the server admin in a variable in my script i would simply do the following:
<?php
$email = $_SERVER[‚SERVER_ADMIN‘];
?>
Now the above would store the webmaster of the server’s email address in a variable for later usage.
Now we move onto SESSION.
Session are defined by the webmaster or user (depending n the situation).
They are often used for authentication as an exploit has not been found for a user to change them yet.
They’re even used in the CMS you’re using now, Recorded in your session data will be your UserID, Your Authentication level and possibly your username and password.
SESSIONS are set and released with the session functions such as:
session_Register();
session_destroy();
session_unset();
session_start();
session_register() is very heardly used anymore as it is only required in php versions older than PHP 4.5.0 i think.
Session_start() has to be at the very top of each script before any data is processed.
so a simple script would be the following:
<?php
session_start();
$_SESSION[‚level‘] = „guest“;
echo „You Are A „.$_SESSION[‚level‘];
?>
Now since we don’t have any other functionality in that script the person viewing it will always be declared as a guest.
Now the others are all admin defined (POST, GET, and COOKIE).
So to use the POST variable we’d need to set a form up using the post field.
I will not get into the others other than stated such as GET, POST and COOKIE as I will cover them in another tutorial.
– IF statements –
=-=-=-=-=-=-=-=-=-=
Now if statements are one of the most basic things ever, and most easiest thing to learn..
Here is a simple example:
<?php
$array = array(„one“, „two“, „three“);
IF($array[0] == ‚one‘){
echo „The Number Is One“;
}
?>
As you can see i’ve used spacing (tabbing the lines) to keep my work neater, that is important when coding as you can read what happens when and then you can easily see if something goes wrong.
Anyway back to the IF statement.
You can see it’s pretty simple
If something is this, do this
next is the command ELSE
It fits in with IF and can only be used in conjuction with the IF statment once per IF statement.
EG:
<?php
$array = array(„one“, „two“, „three“);
IF($array[0] == ‚one‘){
echo „The Number Is One“;
}ELSE{
echo „The Number Is Not One“;
}
?>
Hopefully you can grasp that concept and i dont have to explain it to much.
Explained:
IF(something) so this
else(if the statement isnt true then do this)
simple eh?
Now to the last peice of my tutorial – Commenting on code with PHP.
– Comments –
=-=-=-=-=-=-=-=
Commenting in your script when using php is very useful just to leave notes, copyright notices or just exmplaining something, eg:
<?php
// This is a one line comment, I can only use it for one line so is useful for writing short comments, not long like this one LOL..
/*
This is a multi line comment
I can use as many lines up as i wish with this and i
dont have to stick to one
line, these are best for a notice’s to fellow developer’s
and/or scripts you create for
public use and for
other people to use and learn
from
*/
?>
Now if you do not understand that god help you lolz.
Well That concludes my second PHP tutorial.
I hope you learnt from it.
Enjoy
Ac1d