Subscribe For Free Updates!

We'll not spam mate! We promise.

14 October 2012

Learn PHP.!!

How To Install PHP.??

If you want to learn writing and executing PHP scripts, you need PHP software to practice it yourself. Here are the steps for installing PHP in your system (Here, Windows 7 (32 bit) has been used).
Go to the download site: http://www.php.net/downloads.php
Download any of the complete source code.




Then click on Windows 5.4.6 binaries and source, download VC9 x86 Non Thread Safe in zip folder.
To run this application, you need Visual C++ package which can be downloaded fromhttp://www.microsoft.com/en-us/download/details.aspx?id=5582.
A webserver is required for executing PHP scripts. You can use Apache or IIS (Internet Information Service).
For downloading Apache, go to http://httpd.apache.org/download.cgi, download the msi installer file.

If you want to use IIS instead of Apache, download IIS from http://www.microsoft.com/en-us/download/details.aspx?id=2299, choose the files according to your system requirements.
After installation of PHP and Apache, you need to do configuration also which is as follows:
1) Go to C:/php/ext
Copy php.ini-development file from your Apache directory to Windows directory and rename it as php.ini and edit the following:
extension_dir = “C:/php/ext”
2) Remove semicolon in the following .dll files:
  • php_curl.dll
  • php_gd2.dll
  • php_mbstring.dll
  • php_mysql.dll
  • php_mysqli.dll
  • php_pdo.dll
  • php_pdo_mysql.dll
  • php_xmlrpc.dll

3) Go to C:/Program files/Apache Software Foundation/Apache 2.2/conf and open the file httpd.conf with notepad. Find the word DocumentRoot which appears twice in the file, change the paths at both the places as : C:/Program files/Apache Software Foundation/Apache 2.2/conf. Add the following lines in that file:
# PHP5 module
LoadModule php5_module “c:/php/php5apache2_2.dll”
AddType application/x-httpd-php .php
And edit the line : PHPIniDir “C:/php”
Edit ServerRoot C:/Program files/Apache Software Foundation/Apache 2.2
Add index.php at the end of the file as the Directory index as follows:
Directory Index index.html index.php

5)Add C:/php to the environment variable:
Right clicking My computer-> properties-> Remote Settings->Advanced tab-> Environment Variables; choose path variable, edit it and add C:/php at the end. Click ok.
In the same way, configuration is done in IIS also if it’s been used as a webserver.
6) Goto run, type services.msc, stop the IIS service and World Wide Web Publishing Service and restart again. Reboot your system.
7) Open notepad, type <?php phpinfo(); ?> and save it as index.php in C:
C:/Program files/Apache Software Foundation/Apache 2.2/htdocs
Open web browser and type http://localhost/index.php
The page displaying information about PHP will open.
A very good way of installing PHP is using Wampserver which will automatically install PHP and webserver in your system. You can download Wampserver athttp://www.wampserver.com/en/, choose the wampserver accordingly.
After installing Wampserver, you will see it is pinned in the task bar at the bottom near the speaker icon, click it and you will see a dialogue box.
Click on localhost and the PHP info page will open.
If this page appears, it shows PHP has been installed correctly.
Now create a PHP code in notepad and save it in c:/wamp/www with the extension .php at the end eg, prog.php. Open the web browser and type http://localhost/prog.phpand you will get the result.

[PHP Tutorial 1] An Introduction to PHP

PHP (Hypertext PreProcessor) is a scripting language used to develop web pages. PHP has been named according to the old version called Personal Page Home. It was developed by Rasmus Lerdoff in 1995. The web pages that end with .php are created using PHP code. It can be embedded in HTML codes for creating advance web pages without using external files.
PHP requires a web server (Apache or Internet Information Service), a database ( MySQL, Oracle, Generic ODBC etc) and the PHP module. PHP code is executed by the web server and the PHP process module, that results in creation of a web page. WordPress, Joomla, Drupal, MediaWiki are some of the softwares that use PHP. It is available free of charge atwww.php.net.
phpA PHP file has an extension as .php that contains html and non-html (PHP) code. A PHP script can be added anywhere in html code.
PHP tag
<?php is an open tag and ?> is a closing tag in every PHP script. There are so many tags used in PHP like that used in html.
Echo or print keyword is used for displaying and works the same way as “cout” in C++.
Eg:       echo “ Hello”;            will display Hello in the web page.
Similar to C++, // is used for a single line comment and /*  */ is used for multiple lines comment.
Eg:       // This is a one line comment
 /* This is a first line
This ia a second line */

PHP variable
Like any other programming language, PHP also uses variables to store values. PHP variables are case-sensitive and start with a $ sign. They should not contain spaces and can start with a letter or underscore. There is a difference in creating variables here. Like C++, variables are not required to be declared first and used then; in PHP, variables can be declared anywhere in the program whenever the need arises. Moreover, no need to assign data type to the variables, these are automatically understood by PHP according to the value assigned.
Eg: $x=10;
 For writing a PHP code, you need a text editor. Use notepad for this purpose, write the code and save the file in the webserver directory with an extension .php eg, file.php After saving the file, open the web browser and write http://localhost/file.php, you will get the result.
A simple PHP code is as shown below:
<?php
                        $x = 10; // declares and assigns value to x
                        echo $x; // displays the value of x
?>
This PHP code can be added in html code as follows:
 <html>
<body>
<?php
                        $x = 10; // declares and assigns value to x
                        echo $x; // displays the value of x
?>
</body>
</html>
Output would be:
10
Let’s write another program for adding two values;
<?php
$x=10;
$y=8;
$z=$x+$y;
echo $z;
?>
Save it as add.php. Type http://localhost.add.php and the output will be:
18
Advantage of using PHP is that the script can be used in other programs, so no need to write it again. Scripts can be edited for making changes required for some other program. The PHP scripts can be used for creating advance web pages like creating forums, username, password, pictures, surveys etc.

[PHP Tutorial 2] Using IF-ELSE and SWITCH statements in PHP

Like other programming languages, IF-ELSE statements can be used in PHP also. These statements are used for comparison of two or more than two values. Comparison operators like ==, !=,>,< etc. can be used for such purpose.
Let’s write a program using if-else:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
         $x=20;   
         if($x=="10")
{
             echo "hello";
}
         else
{
             echo "bye";
}
?>
If” is followed by conditional statement ($x= =“10”) which checks whether value of x (as defined before) is equal to 10 or not. “= =” used here, is the equal to operator used between two operands. After the conditional statement, the desired output is written under ‘{‘ curly braces. Here, “hello” word will be displayed if value of x is equal to 10. But we have assigned it 10 as the value. As this is a false statement, the control is forwarded to the else statement which has the code to display “bye”. The control of the statement goes to else when the “if” statement is found incorrect.
Thus, the output is bye which can be obtained by typinghttp:/localhost/file_name.php where the file_name is the name of the file by which it was saved.
ELSEIF
If there are multiple statements to be checked, we use elseif. See a program how elseifcan be used:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
            $x=20;
            $y=30;
            if($x=="10")
{
             echo "hello";
}
            elseif ($y==“30”)
{
            echo “sorry”;
}
else
{
            echo "bye";
}
?>
As explained above if the “if” statement is found to be false, the control of the flow goes to the next statement, which here is elseif. It will check whether the value of y is 30 or not. “Sorry” will get displayed as the value assigned to y before is 30. Thus the output is 30. Other than “= =” operator, there are few more operators like <, > etc that can be used for comparison.
Let’s take a quick look on these:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
            $x=20;
            $y=30;
            if($y>$x)       
{
                echo "y is greater";
}
?>
Greater than operator compares the two operands to check if first operand ($y) is greater than the second operator ($x) and displays the statement mentioned as echo.
If there are more than two conditional statements, logical operators can be used.
&& (and), || (or), ! (not) are three such operators. Eg:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
            $x=20;
            $y=30;
            if("$x>$y" || "$y<30")        
{
                echo "y is below 30";
}
?>
Here, OR (||) operator is used which gives the output if any of the two statements is correct. Here, since “$y<30” is true, the control passes to the next line even if “$x>$y” is false. Opposite to this, AND (&&) operator considers the conditional statement as true only if both the statements are correct. And the logical operator, AND (!) is used as “not equal” condition, opposite to (= =) equal operator.
SWITCH
This is required when there are number of possible statements to be checked. The example below shows how it is used.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
$x=100;
switch ($x)
{
case 10:
                   echo "It is not 100";
                   break;
case 20:
                   echo "It is not 100";
                    break;
case 30:
                    echo "It is not 100";
                     break;
case 50:
                     echo "It is not 100";
                     break;
case 100:
                     echo "It is 100";
                     break;
default:
                     echo "Try again";
}
?>

Switch will find for that case where the value of x is 100; if it does not find that value, default case will be executed and “Try again” will be displayed. Since there is a case 100in these case statements, the output will be “It is 100”.

[PHP Tutorial 3] For and Foreach Loops in PHP

We discussed the conditional statements, using if else, else if and switch in tutorial 2 but there are more ways to use conditional statements. Actually, it becomes cumbersome to write the same statement again and again if the conditional statements like if else have been used a number of times. To avoid repeating the statements, we use loops. You just need to mention the condition in the loop and the statement will get executed the number of times it has been defined so.
Let’s see what kind of loops we can use. We have, for loop, foreach loop, while loop,do-while loop:
 For loop
 The basic syntax for using for loop is
 For (initialization; condition; increment/decrement)
{
            Your statement;
}

Let’s take an example of php program using for loop:

?
1
2
3
4
5
6
7
8
9
10
11
<?php
 for($i=0; $i<5; ++$i)
 {
echo " the value of i is $i <br>";
}
 ?>
Here, “i” variable is defined and designated the value “0”. This is the first expression in thefor loop and is termed as initialization. The second expression defines the condition, that value of “i” should be less than 5. The third expression is an increment statement by which the value of “i” will be increased by 1. It can written either ++$i, which is called pre-increment operator or by $i++ which is called post-increment operator.
++$i is nothing but the statement, $i=$i+1. For example, if the value of “i” was 3will get added to it and the new value, i.e, 4 is assigned to the variable “i”. While the post-increment operator, assigns the value first and then add 1 but the pre-increment operator, adds 1 before assigning the value. Decrement operators are also of two types, eg. - – $i and $i- -
Now, coming back to the loop; the value of i is displayed when the conditions in the forloop is true. The output of the above program is:
the value of i is 0
the value of i is 1
the value of i is 2
the value of i is 3
the value of i is 4
We will see how this happened. At first, the first expression of the for loop says the value of i is 0; then according to the next expression, 0 is less than 5 which is also true. The third expression increments its value to 1, so now the flow goes to the next statement, ie. “the value of i is 0” gets displayed. Now, value of i1, is passed again to the for loop to check if it is less than 5, then increments to 2 and “the value of i is 1” gets displayed. When the value of i becomes 5, the second expression becomes false and the loop exits.
 <br> is used to give breaks within the lines and the output is shown in the next line. If we don’t use it, all the outputs will be shown in one line (consecutively).
Foreach loop
Before going for foreach, we should be aware of arrays because foreach is used with arrays.
An array is a collection of variables of same data type. If we have more than one variable and do not want to define them separately, we can use array. Eg: $a=array(1,2,3,4,5,6);or we can also write it as follows:
 $a[0]=1;
$a[1]=2;
$a[2]=3;
$a[3]=4;
$a[4]=5;
$a[5]=6;
Let us see how to use foreach with arrays:
The syntax for using foreach is:
 foreach(array_name as array_value)
{
            Your statement;
}
Where array_name is the name of the array variable and array_value is the variable used for the values taken from the array.
Example:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$arr=array(10,20,30,40,50);
foreach ($arr as $i)
 {
echo " the value of array is $i <br>";
}
?>

The output of the above program will be:

the value of array is 10
the value of array is 20
the value of array is 30
the value of array is 40
the value of array is 50
In this loop, for every value in the array, statement written after echo is displayed. Loop will first take 10 and displays the echo statement, then 20 and so on. Each value in the array will be recognized by the loop as $i variable.

[PHP Tutorial 4] While and do while loops in PHP

In tutorial 3, we saw there are loops that we can use to avoid writing long programs. After learning for and foreach loop, we will learn while and do while loops.
While loop is used when we are to display a statement only against a certain condition. If the condition mentioned in the while loop is true, the next statement following the whileloop will be executed, otherwise not.
The syntax for using while loop is:
While (condition)
{
            Your statement;
}
Example,
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$x=0;
 while($x<3)
 {
echo "Hello <br>";
++$x;
}
 ?>

The output is:
Hello
Hello
Hello
 In while loop, according to the mentioned condition, “Hello” is displayed till the value of$x is less than 3. For, value of x=0, “Hello” is displayed since is less than 30 gets incremented to by ++$x. Now, the value of x1 is also less than and the output is “Hello”. But the value is again incremented to 2, which is forwarded to the loop again. 2 is less than 3 and we get “Hello”. As 2 gets incremented this time, condition in the while loop becomes false and the loop then exits. Thus “Hello” is displayed only 3 times.

Do while
In comparison to while loop, do-while loop always displays the value or at least once whether the condition in the while loop is true or not. The syntax using do-while is:
Do
{
            Your statement;
}
While (condition)

The example is as below:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
$x=2;
do
{
            echo "how are you?";
            ++$x;
}
 while($x>5)
?>

The output will be:
how are you?

Because 2 is not greater than 5, the condition in the while statement becomes false. Still, as we have do statement, it will be executed and we get the output. This would not be the result if only while would have been used. This is the difference between while and do-while loop.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
$x=2;
do
{
            echo "how are you? <br>";
            ++$x;
}
while($x<5)
?>

The output is:   how are you? how are you? how are you?   Here, the condition comes out to be true and do statement is executed. The output will be shown till the time this condition is false. Thus, “how are you?” is shown three times.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
$x=2;
do
{
            echo "how are you?";
            ++$x;
}
 while($x>5)
?>


==========================================================
==========================================================

THANK YOU..!!

=========================================


Socializer Widget By erYMr
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment