Tutorial for PHP language part 1
In this tutorial I am going to discuss about the working of PHP, how to work PHP in a computer, different statements using in PHP and how to connect PHP with Apache server. In this article some conditional statements are included, I wish to add more statements and a project in PHP for effective study of PHP in my next articles.
Thank you
PHP – Server Side scripting
In this tutorial I am going to discuss about the working of PHP, how to work PHP in a computer, different statements using in PHP and how to connect PHP with Apache server.
PHP:-
First of all we start with PHP. What is PHP? It is a server-side scripting language like ASP. PHP is stand for Hypertext Preprocessor or sometimes refers it as Personal Home Page. As I told it is server side scripting language it executed on server. PHP supports databases like Mysql, Oracle, Sybase, Solid etc. The main advantage of PHP it is an open source software(OSS) so that it is free to download and use. It supports HTML tags and scripts. PHP has the file extension of ".php".
Apache Server:-
For the working of server side scripting language we must have a server. Here we use a server called 'Apache' which is a open software. The Apache HTTP Server commonly referred to as Apache.
Mysql:-
Mysql is a database. There are so many databases like mysql. The main advantage of mysql, it is easy to use and it is an open software.
WAMP:-
For the above purpose we can simply use the software which is integrated with all this software. WAMP means Windows Apache Mysql Php. All these support the software called WAMP. For this we have to download the software wampserver 2.0 or other versions of wamp.
1. Double click on wampserver.
2. Click on next button on the dialog box, then click the check box that 'I accept the agreement'.
3. Then again a dialog appear then again click on the next button then click the install button to install the wamp server.
4. The tick to launch the wamp and click ok.
5. After that a icon is come on the task bar like semi circle.
6. Click on that and icon and select it in to put online.
How to use WAMP:-
1. We type program containing PHP in a notepad and save it in the directory "C:\wamp\www\filename.php.
2. After saving it, click on the icon of the wamp server on the taskbar and select 'localhost'.
3. Then a window is open in the default browser and you can see the filename.php.
4. Then we can run the program.
Then we can see how to write a program in php.
Study PHP:-
The php script is start with the . We can place php in anywhere in the program by using these tags. A PHP file normally contains HTML tags.
Example of a php program:-
The keyword 'echo' is used to print string. The output of the about string is to print 'welcome to India study channel.
The variable naming rule:-
1. Every variable start with the sign '$'
2. A variable name must start with a letter or an underscore "_".
3. Variable name contain alpha-numeric characters and underscores(a-Z, 0-9,_)
4. Variable should not contain space between them.
PHP operators:-
Like all programming languages php contains the same operators.
1. Assignment operators
2. Arithmetic operators
3. Comparison operators
4. String operators
5. Combination arithmetic and assignment operators.
Now we can study about the statements in PHP.
• Conditional IF statement:-
The if statement is a important statement in any programming language and also in PHP. It contains a condition part, if it true then execute the following part.
Syntax-
If(condition)
Code to be executed
End of if
The output of the above program- print 'you are belong to India study channel', because the if condition is true always.
• If – else statement:-
It is like conditional if so that it contains a else part, so that when the if condition fails then it execute the else part.
Syntax:-
If ( condition )
Code to be executed
else
code to be executed
• Elseif condition:-
If we want to check so many if condition then we can use the conditional statement 'elseif'.
Syntax:-
If ( condition )
Code to be executed
elseif
code to be executed
else
code to be executed
example:-
In the above program a function is used called date(); which means to display the system date. In that function we use the "D" for specifying the day. So the variable $d contains the system day. If the system date is Friday then $d gets Fri. and print 'Have a nice weekend' or check ifelse and both these are false then execute the else part which print 'Have a nice day'.
• Switch statement:-
To select one of many blocks of statements we can use the switch statement. So that we can avoid the if..elseif..else code
Switch(expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed if other than these two labels.
}
The switch contain a expression so that it matches with the label to execute that case. At every case only one case is work because each case contains a break statement so that it break from the switch. If no code is matches with the case label then work the default statement.
The output of this program is when $x matches with the numbers 1,2 and 3 then execute the case belong to that case. If the case other than 1,2 and 3 then execute the default.
• for loop:-
For loop is used when you know how many times you want to execute a statement or a list of statements.
Syntax:-
for ( initialization; condition; increment ){
code to be executed;
}
For 'for' loop there are three part the first part is initialization process, the second parameter holds the condition and the last parameter is increment.
The output of the above program is print the 'India Study Channel' for 5 times. Because the condition false when $i=5.
• Foreach statement:-
The foreach statement is used for loop through arrays. For each loop the value of current position of the array is placed to a value. Then the foreach look for the next value.
Syntax:-
Foreach ( array as value ){
Code to be executed;
}
Here array means the name of array and value means the variable.
The output of the above program is print the values in the array. At first move the one to the variable $arr. And print all the values in the array.
• While loop:-
While loop is execute when the condition is true.
Syntax:-
While (condition){
Code to be executed;
}
Example:-
• Do….while loop:-
The do…while statement will execute at least once, because the condition is at the bottom of the loop. And it repeat until the condition is true.
Syntax:-
do{
code to be executed;
}
while(condition);
In this session we study about different type of php statements and how to use the PHP and how to install PHP. In next session we can see how the working of arrays and working of mysql. And to make a web page.
Recently I came across some great articles on your site.
The other day, I was discussing (http://www.indiastudychannel.com/resources/158254-Tutorial-PHP-language-part-1.aspx)with my colleagues and they suggested I submit an article of my own. Your site is just perfect for what I have written!
Would it be ok to submit the article? It is free of charge, of course!
Let me know what you think
Contact me at anelieivanova@gmail.com
Regards
Anele Ivanova