![]() |
|
Introduction To PHP - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: PHP (https://sinister.li/Forum-PHP) +--- Thread: Introduction To PHP (/Thread-Introduction-To-PHP) |
Introduction To PHP - The Alchemist - 03-20-2013 Hello Hack Community. I'm The Alchemist. As a member of HC Developers group I'd like to introduce you guys to PHP through this thread. 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 Hack Community 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
Disadvantages Of PHP
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 Text Notepad++ PSPad ----> Frooxius and my personal choice. OR Netbeans 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 : XAMPP For Linux : LAMP For 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 : Code: http://127.0.0.1/scriptname.phpAny 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 Code: <?php
echo "Hello Hack Community";
?>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 Hack Community"(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 Code: <?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 Code: <?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 : Code: int $var = 5;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 Code: <?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 Code: <?php
$v1 = "P";
$v2 = "H";
$conc = $v1.$v2."P";
echo $conc; // Displays the concatinated result i.e. PHP
?>PHP Code: <?php
echo "First line"."<br />"."Next line";
?>So, the output of the above code will be like this : Code: First line
Next lineEscaping 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 Code: <?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 Operators These are use for performing arithmentic operations. + --> Addition operator - --> Subtraction operator * --> Multiplication operator / --> Division operator % --> Modulus operatot ++ --> Increment operator -- --> Decrement operator Example : PHP Code: <?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 Code: <?php
$var = 1; // $var is assigned 1
?>Comparison Operators These 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 Code: <?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 operator The 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 : Code: $var = (condition) ? Value if condition is true : Value is condition is false ;PHP Code: <?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 Statements Conditional 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 Code: <?php
if (condition)
{
// Code if condition is satisied
}
else
{
// Code if condition is not satisfied
}
?>Example : PHP Code: <?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 Code: <?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 Code: <?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 Operators With 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 Code: <?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 Statements Like 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 : Code: for(initial value of loop variable ; condition for running ; modification of loop variable)
{
// Code here
}Example : PHP Code: <?php
for ($i = 0 ; $i <= 10 ; $i = $i + 2 )
{
echo $i."<br />";
}?>The output for the above code is : Code: 0
2
4
6
8
10Syntax of while : Code: initial value of loop variable
while(condition for running)
{
// Code here
// modification of loop variable
}Example : PHP Code: <?php
$i = 1;
while ($i <= 5)
{
echo $i."<br />";
$i++;
}?>The output for the above code will be : Code: 1
2
3
4
5Syntax of do while : Code: initial value of loop variable
do
{
// Code here
// modification of loop variable
}while(condition for running);Example : PHP Code: <?php
$i = 1;
do
{
echo $i."<br />";
$i++;
}while($i <= 5);
?>The output for the above code will be : Code: 1
2
3
4
5NOTE : 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. Arrays Arrays 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 : Code: $arr = array(value1, value2, value3);Code: $arr = array(index1 => value1,
index2 => value2,
index3 => value3);Examples : PHP Code: <?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 Code: <?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 Code: <?php
$ar = array(array(1,2,3),array(4,5,6),array(7,8,9));
print_r($ar); // print_r is the function
?>foreach loop Suppose 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 Code: <?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 : Code: 10
20
30
40Another example : PHP Code: <?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 : Code: one = 1
two = 2
three = 3
four = 4Functions A 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 : Code: funcion function_name(arguments)
{
// Piece of code
}Syntax for calling a function in PHP : Code: function_name(arguments);Example : PHP Code: <?php
function hello() // Function definition
{
echo "Hello HC";
}
hello(); // Function call
?>Code: Hello HCWith the help of functions you can also return values. Example : PHP Code: <?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
?>Code: 10Thats 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 PHP Object Oriented Solution --> This is for intermediate level. Covers object oriented Concepts. Advanced PHP for Web Professionals --> This is for advanced level. 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.net or http://w3schools.org For video tutorials on PHP, go to http://thenewboston.org Thanks for reading. Hope you enjoyed it. ~The Alchemist RE: Introduction To PHP - zomgwtfbbq - 03-20-2013 Extremely good tutorial, you cover everything for a n00b to get started. *applauds* RE: Introduction To PHP - The Alchemist - 03-20-2013 (03-20-2013, 05:06 PM)zomgwtfbbq Wrote: Extremely good tutorial, you cover everything for a n00b to get started. *applauds*Thanks a lot man. It really means a lot to be appreciated by a cool web designer.
RE: Introduction To PHP - Ex094 - 03-20-2013 Awesome tutorial, Em gonna learn some PHP RE: Introduction To PHP - gunsmith14694 - 03-20-2013 very well written...great stuff RE: Introduction To PHP - The Alchemist - 03-21-2013 (03-20-2013, 07:03 PM)Ex094 Wrote: Awesome tutorial, Em gonna learn some PHPThanks a for the feedback. I'm glad you liked it. (03-20-2013, 07:05 PM)gunsmith14694 Wrote: very well written...great stuffThanks. RE: Introduction To PHP - H4X0RZ - 03-23-2013 Great Tutorial, But I am thinking that if it would be video tutorials then it would be great. Trying to Find A complete Series For PHP RE: Introduction To PHP - H4X0RZ - 03-23-2013 Great Tutorial, But I am thinking that if it would be video tutorials then it would be great. Trying to Find A complete Series For PHP RE: Introduction To PHP - The Alchemist - 03-23-2013 (03-23-2013, 04:19 PM)Mr.U Wrote: Great Tutorial, But I am thinking that if it would be video tutorials then it would be great.In the thread(at the end), I've mentioned a URL for video tutorials on PHP, you may have a look at them. You'll find video tutorials on other languages too. But, if you go for my thread, you'll learn faster. The Alchemist Wrote:For video tutorials on PHP, go to http://thenewboston.org RE: Introduction To PHP - The Alchemist - 03-23-2013 (03-23-2013, 04:19 PM)Mr.U Wrote: Great Tutorial, But I am thinking that if it would be video tutorials then it would be great.In the thread(at the end), I've mentioned a URL for video tutorials on PHP, you may have a look at them. You'll find video tutorials on other languages too. But, if you go for my thread, you'll learn faster. The Alchemist Wrote:For video tutorials on PHP, go to http://thenewboston.org |