Hello Evil Zone. I'm
The Alchemist.. If you find anything wrong in it, please do leave a comment here or PM me. Anything you'd want me to add here or any doubts regarding the contents of this thread, you may leave a comment here and I'll surely help.
Introduction To PHP What is PHP? PHP stands for "PHP hypertext pre-processor".
It is predominantly a web programming language, so you can design and create web systems. It is a server-side language, which means that it doesn't run on the user's browser. It is an interpreted language and not a compiled language which means, the source code isn't converted to a form that can directly be run by the compiler. Every time a PHP acript is run, the script is re-interpreted.
It is an HTML embedded scripting language. You'll see that most of its syntax is like C, Java, etc.
Why learn PHP? PHP is the most popular web programming language in the world. HTML is static, a web scripting language like PHP makes webpages dynamic in nature. There are other web-scripting languages like ASP, JSP, etc. but PHP is way better and popular than them.
PHP is used everywhere in the internet. You try loginng in to your Evil Zone or facebook account, a PHP script verified whether your username and password combination is valid or not. You go to your school's website to check your grades, a PHP script shows you your grades when you enter your roll number.
Advantages Of PHP- Open Source
- Cross-Platform
- Support(Its very popular, so you can easily get guidelines and tutorials)
- High Returns(A site made in PHP has the potential of the site being visited a lot)
- Huge Community(Lots of PHP developers around the world)
- Embedding(Can be embedded in HTML)
- Stability, Flexibility and Speed
- Bright Future(A lot of good stuff can be made in the future)
- Quick
- Extensions(Extremely scalable)
- Other Tools(Easy to access tools such as google maps)
- Pre-Configured(Most hosting services have PHP pre-installed)
What do I need type scripts? First of all, you'll need a text editor or an IDE. Its always better to use a text editor and hard code everything. But then, again there are hundreds of functions in PHP which are impossible to memorize. So, its upto you to decide. You may even use something as simple as notepad.
You may use either of these text editors :
Sublime TextNotepad++PSPad ----> My personal choice.
What do I need to run PHP scripts? As PHP is server sided, its not as easy as running HTML scripts. You'll need to set up a PHP environment in your computer to run them. So, you'll need one of these softwares :
For Windows :
XAMPPFor Linux :
LAMPFor MacOS :
MAMP After you install one of these in your computer, go to the installation directory and navigate to the directory named
htdocs. Now, paste your PHP script inside the
htdocs folder and then, run Apache(your localhost webserver). For XAMPP, you need to open the file
xampp_start.exe from the
xampp folder.
Now, open your browser and type this on your address bar :
http://127.0.0.1/scriptname.php
And your, PHP script that you'd placed in the
htdocs folder will get executed in the Apache server and the corresponding HTML code will be displayed in your browser.
Any doubts about running a PHP script? You're free to post your questions in on this thread or PM me.
Lets begin a bit of coding NOTE : I'll be covering the very basics of PHP coding and I won't be going into details. I'll include enough information for you to understand simple PHP codes.As I'd said earlier, PHP has a syntax very similar to C, Java and other similar languages. So, if you know any other programming language, it'll be very easy for you to learn and understand PHP.
Hello world A PHP code begins with
<?php and ends with
?>. Its the convention for writing PHP codes. A "Hello World" program would look like this :
<?php
echo "Hello Evil Zone";
?>
As you can see, the code begins with
<?php and ends with
?>. In PHP,
echo is the equivalent of
printf(or
cout<< in C/C++) or
System.out.print(in Java).
What this code does is, it displays
"Hello Evil Zone"(without quotes) in your browser. The keyword
echo is used for outputting text or variables.
And mind the semicolon, unlike VB or VB.NET, statements in PHP are terminated by a semicolon.
Comments Like every other programming language, PHP also supports comments. I recommend you to put lots of comments on your code as you write them. There are three ways in which you can comment PHP codes.
<?php
// This is a single line comment
# This is another single line comment.
/* This
is a multi-line
comment */
?>
Variables and datatypes There are four types of data in PHP. They are :
Integer --> Any whole number.
Float --> Any whole number or real number.
String --> Any character or a collection of characters.
Boolean --> Either
true or
false.
Declaring and initializing a variable This is how we declare and initialize a variable in PHP :
<?php
$varname1 = 1; // This is an integer
$varname2 = 100.5; // This is a float
$varname3 = "PHP"; // This is a string
$varname4 = true; // This is a boolean
?>
PHP automatically recognizes the datatype of the variable as we initialize the value to it.
Hence, you don't need to do this :
int $var = 5;
^^^ This is wrong. And not allowed in PHP.
Quotation Marks You can declare strings with both the
" ans well as
' marks.
The difference between them are :
You can use Escape sequences such as \n, \t, \r, etc within
" but within
', these are treated as normal text.
You can output variables within
" but not within
'.
Example :
<?php
echo "\n"; // Goes to the next line
echo '\n'; // Displays \n, treated as simple text
$acc = 10;
echo "$acc"; // Displays 10 (the value of $acc)
echo '$acc'; // Displays $acc (treated as simple text)
echo "{$acc}with{$acc}"; // The {} are used to separate the variables from the text inside the quotes
// ^^ this displays 10with10
?>
Concatenation In PHP, strings can easily be concatinated using the
. operator.
Example :
<?php
$v1 = "P";
$v2 = "H";
$conc = $v1.$v2."P";
echo $conc; // Displays the concatinated result i.e. PHP
?>
<?php
echo "First line"."<br />"."Next line";
?>
Here, have a look at
"<br />". What this does is, it takes the output to the next line, i.e. it marks the end of the line. Don't be confused, remember what I'd said earlier? "PHP scripts are executed in the server and the output is displayed in your browser as HTML code".
So, the output of the above code will be like this :
First line
Second line
Escaping Just like any other language, the
\ is used for escaping characters.
To display
' within single quotes, we need to escape it.
To display
" within double quotes, we need to escape it.
Example :
<?php
echo """; // This is wrong if you want to display "
echo "\""; // This is right if you want to display "
echo '"'; // This is also right if you want to display "
echo '''; // This is wrong if you want to display '
echo '\''; // This is right if you want to display '
echo "'"; // This is also right if you want to display '
?>
Remember, to use escape sequences, use
"" and not
''. I'd said this earlier.
Operators Arithmetic OperatorsThese are use for performing arithmentic operations.
+ --> Addition operator
- --> Subtraction operator
* --> Multiplication operator
/ --> Division operator
% --> Modulus operatot
++ --> Increment operator
-- --> Decrement operator
Example :
<?php
$a = 1 + 9; // Equals 10
$a = 9 - 4; // Equals 5
$a = 1 * 9; // Equals 9
$a = 9 / 3; // Equals 3
$a = 9 % 2; // Equals 1
echo $a++; // Displays the value of $a and then increases the value of $a by 1
echo ++$a; // Increases the value of $a by 1 and then displays the value of $a
?>
Assignment operator = is the assignment operator used for assigning values to variables
Example :
<?php
$var = 1; // $var is assigned 1
?>
Comparison OperatorsThese are used for comparing variables or values
== --> Equals to operator
< --> Lesser than operator
> --> Greater than operator
<= --> Lesser than or equal to operator
>= --> Greater than or equal to operator
=== --> Equals to operator with datatype check
!= --> Not equals operator
Example :
<?php
$a = (5 == 5); // Returns true
$a = (5 < 5); // Returns false
$a = (2 > 1); // Returns true
$a = (5 <= 5); // Returns true
$a = (6 >= 7); // Returns false
$a = (5 != 5); // Returns false
$a = (5 === '5'); // Returns false
$a = (5 === 5); // Returns true
?>
Ternary operatorThe ternary operator is used to initialize a value to a variable within a user defined condition.
The
? and
: are the ternary operators.
Syntax is this :
$var = (condition) ? Value if condition is true : Value is condition is false ;
Example :
<?php
$val = (1 < 2) ? true : false; // $val assigned to true
$val = (1 > 2) ? true : false; // $val assigned to false
$val = (1 > 2) ? "yes" : "no"; // $val assigned with "no"
?>
Conditional StatementsConditional statements are what control the logic of your application. They are If/Else statements. They are used to execute certain codes onlt when a certain condition is fulfilled.
Syantax for using conditional statements :
<?php
if (condition)
{
// Code if condition is satisied
}
else
{
// Code if condition is not satisfied
}
?>
Example :
<?php
$age = 18;
if ($age > = 18)
{
echo "You are adult"; // Displays this as condition is satisfied
}
else
{
echo "You are not adult";
}
$age = 5;
if ($age > = 18)
{
echo "You are adult";
}
else
{
echo "You are not adult"; // Displays this as condition is not satisfied
}
?>
Now, suppose you have more than one condition to be checked. We then use the
else if statement.
Example :
<?php
$num = 100;
if($num > 0)
{
echo $num." is positive"; // Displays this as this condition is satisfied
}
else if($num < 0)
{
echo $num." is negative";
}
else
{
echo $num." is zero";
}
$num = -100;
if($num > 0)
{
echo $num." is positive";
}
else if($num < 0)
{
echo $num." is negative"; // Displays this as this condition is satisfied
}
else
{
echo $num." is zero";
}
$num = 0;
if($num > 0)
{
echo $num." is positive";
}
else if($num < 0)
{
echo $num." is negative";
}
else
{
echo $num." is zero"; // Displays this as no conditions are satisfied
}
?>
Using
else if statement, you can check for any number of conditions.
You can even perform nesting of conditional statements.
Example :
<?php
if (condition1)
{
if (condition11)
{
// Code if condition1 and condition11 is satisied
}
else
{
// Code if condition1 is satsfied and condition11 is not satisfied
}
}
else
{
if (condition22)
{
// Code if condition1 not satisfied and condition 22 is satisied
}
else
{
// Code if condition1 and condition22 both are not satisfied
}
}
?>
Logical OperatorsWith logical operators, we can combine conditions.
Here are the logical operators :
&& -->
LOGICAL AND operator, used to check if
ALL the conditions are satisfied
|| -->
LOGICAL OR operator, used to check if
ANY ONE the conditions are satisfied
Example :
<?php
$age1 = 10;
$age2 = 20;
$age3 = 30;
if(($age1 > 5) && ($age2 > 5) && ($age3 > 5)) // All the conditions are satisfied so, returns true
echo "Everyone is above 5 years of age";
if(($age1 > 10) && ($age2 > 10) && ($age3 > 10)) // All the conditions are not satisfied so, return false
echo "Everyone is above 10 years of age";
if(($age1 > 10) || ($age2 > 10) || ($age3 > 10)) // Two out of three conditions are true so returns true
echo "One or more out of the three is more than 10 years of age";
if(($age1 > 20) || ($age2 > 20) || ($age3 > 20)) // One out of three conditions is true so returns true
echo "One or more out of the three is more than 20 years of age";
if(($age1 > 90) || ($age2 > 90) || ($age3 > 90)) // No condition is satisfied so returns false
echo "One or more out of the three is more than 90 years of age";
?>
Iterative StatementsLike C, Java, PHP too has iterative statements.
for,
while,
do while and
foreach(I'll discuss foreach loop after I teach arrays)
They are used for executing a piece of code repeatedly.
Syntax of
for :
for(initial value of loop variable ; condition for running ; modification of loop variable)
{
// Code here
}
Example :
<?php
for ($i = 0 ; $i <= 10 ; $i = $i + 2 )
{
echo $i."<br />";
}?>
The output for the above code is :
0
2
4
6
8
10
Syntax of
while :
initial value of loop variable
while(condition for running)
{
// Code here
// modification of loop variable
}
Example :
<?php
$i = 1;
while ($i <= 5)
{
echo $i."<br />";
$i++;
}?>
The output for the above code will be :
1
2
3
4
5
Syntax of
do while :
initial value of loop variable
do
{
// Code here
// modification of loop variable
}while(condition for running);
Example :
<?php
$i = 1;
do
{
echo $i."<br />";
$i++;
}while($i <= 5);
?>
The output for the above code will be :
1
2
3
4
5
NOTE : As you can see, in a do while loop, the code inside the loop is executed and then, the condition is checked. This means that a do while loop will run atleast once even if the condition for running is initially not satisfied. This makes it a exit control loop. Whereas in case of for and while loop, if the condition for running is initially not satisfied, the loop will not execute. Hence, they are entry control loops. ArraysArrays are variables that can contain a set of values. Arrays have two parts, values and their respective indexes.
Unlike C, Java, etc. in PHP an array can hold unlimited number of values. Also, an array in PHP can hold different types of values and you can name the indexes with strings unlike C, Java, etc.
Syntax for initializing arrays :
$arr = array(value1, value2, value3);
OR
$arr = array(index1 => value1,
index2 => value2,
index3 => value3);
Examples :
<?php
$arr1 = array(10,20,30,40);
echo $arr1[0]."<br />"; // Displays the value of index 0 i.e. 10
echo $arr1[1]."<br />"; // Displays the value of index 1 i.e. 20
echo $arr1[2]."<br />"; // Displays the value of index 2 i.e. 30
echo $arr1[3]."<br />"; // Displays the value of index 0 i.e. 40
$arr2 = array("one" => 1,
"two" => 2,
"three" => 3,
"four" => 4);
echo $arr2["one"]."<br />"; // Displays the value of index "one" i.e. 1
echo $arr2["two"]."<br />"; // Displays the value of index "two" i.e. 2
echo $arr2["three"]."<br />"; // Displays the value of index "three" i.e. 3
echo $arr2["four"]."<br />"; // Displays the value of index "four" i.e. 4
?>
PHP supports multi-dimensional arrays too :
Example :
<?php
$ar = array(array(1,2,3),array(4,5,6),array(7,8,9));
echo ar[0][0]; // Displays 1
echo ar[1][1]; // Displays 5
?>
Now suppose you don't know the dimension of an array, there's a special function in PHP to display all the values of an array.
Here is how we use it :
<?php
$ar = array(array(1,2,3),array(4,5,6),array(7,8,9));
print_r($ar); // print_r is the function
?>
foreach loopSuppose you do not know how many values an array contins or the name of the indexes of the array, but you need to print out all the values contained in the array, this is where the
foreach loop comes into play.
Example :
<?php
$arr = array(10,20,30,40);
foreach($arr as $temp_value) // What this loop does is, it gets the next value of the array and store it in $temp_value variable
{
echo $temp_value."<br />";
}
?>
The above code displays :
10
20
30
40
Another example :
<?php
$arr = array("one" => 1,
"two" => 2,
"three" => 3,
"four" => 4);
foreach($arr as $key => $val) // This loop gets the next index and store it in $key and its corresponding value is stored in $val variable
{
echo $key." = ".$val."<br />";
}
?>
The above code displays :
one = 1
two = 2
three = 3
four = 4
FunctionsA function is essentially a piece of code given an identifier. The main idea behind a function is for the reuseability of the piece of code.
PHP has hundreds of inbuilt functions which makes our work extremely easy.
Syntax for defining functions in PHP :
funcion function_name(arguments)
{
// Piece of code
}
Syntax for calling a function in PHP :
function_name(arguments);
Example :
<?php
function hello() // Function definition
{
echo "Hello EZ";
}
hello(); // Function call
?>
The output of the above code would be :
Hello EZ
With the help of functions you can also return values.
Example :
<?php
function greater($v1, $v2) // function definition
{
if($v1 > $v2)
return $v1; // return statement
else
return $v2; // return statement
}
echo greater(5,10); // function call
?>
The output of the above code would be :
10
Thats all.
I've covered the very basics of PHP in this thread, not in much detail but enough for general idea and understanding. And as I've said earlier, any doubt, any mistake you point out, put it to my notice by leaving a comment here.
Here are some ebooks I'd recommend :
PHP 6 And MYSQL Bible
PHP Solutions Dynamic Web Design Made Easy 2nd Edition
Just google them and you'll easily get their free download links.
If you want to learn PHP in detail, the best place would be :
http://php.netFor video tutorials on PHP, go to
http://thenewboston.org Thanks for reading. Hope you enjoyed it.
~
The Alchemist