Guys this tutorial will give you basic to intermediate concept about PHP.If you are already advanced in PHP,you can read it as a reminder.One thing more is that it is possible that you find that tutorial very long,so I will suggest you to take your time and read it thoroughly.
Introduction To PHP
PHP stands for “PHP Hypertext Preprocessor”.It is used to develop web-based applications.About 45% of the sites are PHP based and their number is increasing very rapidly.It is interpreter based,all of its scripts are executed by the interpreter installed on the server.Now you have first question in your mind.PHP Pattern or PHP Paradigm is imperative and object oriented.It is a server side scripting language.What does it mean by Server Side Scripting Language?Actually there are two kinds of scripting languages.
- Client Side Scripting Language
- Server Side Scripting Language
While in Server Side Scripting Language,all of the scripts are run on the server where the site is hosted and then after executing those scripts information is sent back to the user browser(mozilla,google chrome) in html coding.Examples are PHP,Java,ASP.NET,Perl.
Go to any link which ends with the extension .php
okay lets take that link
Go to that link,when a page is opened,view its source.(By Pressing CTRL+U for mozilla firefox)
As you can see it is a php file (index.php)
but when you check its source,you see no php coding at all.You see all the coding in HTML or CSS.
Why is that?
Because PHP is a server side scripting language,when we open that link in our browser.Server reads our request and run all the php script written in it on its own side and then sends back the information in HTML form because our browser can understand only HTML.I think now you have gotten the idea of server side scripting language.
Things You Need To Work With PHP
Text Editor=>Where we will write our php code
Web Browser=>Where we will see the output of our program
Server(Any hosting place) with PHP interpreter=>Where our php code will run.
How PHP Works?
As I have said earlier that PHP is interpreter based language unlike other languages like C,C++ and Visual Basic that are compiler based.So to make a PHP Based Website,first step is to install php on the web server.There are different versions of PHP like PHP 5.5.1 & PHP 5.4.17 .One of major advantage of PHP is that we can embed PHP codes in HTML file.
PHP syntax is pretty similar to the syntax of C++ which is also an object oriented programming language.PHP code starts from <?php and ends on ?>.Like in C++,PHP statements also ends on semicolon and same forward slashes are used for inline comments.It is Case-Sensitive too.
are three different things.
Lets have a look on the format of PHP code.What comes before and what comes after?Lets see
PHP Code Format
Logic Behind <?php and ?>
As we know that PHP can be embedded in the HTML file,so question arises that how interpreter will know that whether this is HTML coding or PHP coding?This is where the role of php tags <?php and ?> comes.
<?php // It Tells the interpreter that from that point PHP code is written
?> // It Tells the interpreter that PHP code is finished at that point.
PHP deals with the variable very,very simply and efficiently.You want any example?Well there you go then
In PHP you don’t need to declare the data type of variable! unlike C++ or other language where you have to first declare the variable and then assign value to it.
How To Use Variables?
In PHP variables names start from dollar sign.Example $test,$Test,$phone,$x,$house1,$name_school,$your-problem.Focus carefully on that example,I did not give you that example randomly.It has some meaning in it.Think about it till then I tell you how to assign values to variables.
Assigning values to any variable.
$saying=”God is Forgiving”;
As I told you before that PHP feels no need for variable declaration.Question arises that how PHP interpreter will know that which data type is it of?Is it a string, or s it an integer?The answer is that interpreter determines the data type from the way of value assigned to the variables.Like if it is a string then you will write value in double quotation marks ( $saying=”God is Forgiving”; ).If the value is integer then there is no need for quotation marks ( $number=23; ).We put that semicolon in the end to tell the interpreter that this statement is finished.
Did you focus on that about example as I said?
Okay that was the example
We can use variables like $test,$Test,$phone,$x,$house1,$name_school,$your-problem.
What was the meaning behind?
Okay now here I tell you.Actually above example tells you various things about variables used in PHP.
First- PHP is case sensitive language.Which means that $test and $Test are two different variables.
Second-Variable name can be a single alphabet like $x.
Third-Variable name can contain underscore and hyphen sign but no spaces.Like that $name_school and $your-problem.
Fourth-Variable name can contain a number in it.E,g $house1.But Variable name can not start with number.
Which one is better to be used,underscore or hyphen?
Actually underscore is better approach to be used.Because it can be easily distinguished while on the other hand hyphen is very ambiguous.It looks like a minus sign.It makes things complicated and hard to understand.
$Result=$Number-one – $Number-two;
While we can make it simple for understanding by using underscores
$Result=$Number_one – $Number_two;
Remember it depends upon the situation what to use and what to not.
Like in above scenario the best way was this
So it is not necessary and always good to use underscores every where.Sometimes we have to avoid underscores too.
Dont take that above thing very seriously if you are beginner because i learned that thing from my experience and you will learn it too when you will work in PHP so just forget about it if you can’t understand it now.
Your First PHP Program (Hello World)
Okay now let me give you an example of code that prints “Hello World” on the screen.
There are two ways for it:
You make variable $x and assign that string value “Hello World” to it and then print the variable $x.
Second one is that you just directly print “Hello World”
Okay now open any text editor and type that code in it.
Save it as filename.php after selecting “all files” option.
You have made your first PHP Program.
To watch the output of program,we will upload it on the server where PHP interpreter is installed and then we will open that file via web browser.
Where To Practice PHP?
This question is 200% person obvious.Where you will practice PHP?I know 90% of you don’t have any website,any hosting place where you can run php codes for practice.Well I can guess what are you thinking,You are thinking that will we have to purchase a domain and hosting place for it first to learn php?Not at all!
You will not go anywhere to practice PHP
You will not buy any domain or hosting to learn PHP.
You will do everything on your own computer.
The thing you need to have is a place for hosting,right?Other two things you already have,like web browser in which you are reading that tutorial and a text editor(notepad) which can be found at start–>Accessories–>Notepad.
There are two methods for getting free hosting.
000webhost is a place where you can have free domain and webhosting.
Just go to http://www.000Webhost.com
Make Your Account that is 100%.
Register Any domain name and start practicing on it.
2nd.Using WAMP or LAMP.
In this method our computer will act as a local host and we can independently practice PHP,even without having access to internet.
WAMP and LAMP allow our computer to do that.Now what are WAMP and LAMP?WAMP stands for WIndows Apache MYSQL PHP,actually it is a web development environment.It allows us to create web applications offline on our computer.LAMP is just a same thing,the only difference is that WAMP is for Windows users while LAMP is for linux users.If you find confusing that WAMP and LAMP thing then you can forget about it.Just remember XAMPP,it supports all of the user around the World.Everybody can use it freely.
Just go to the site http://www.apachefriends.org/en/xampp.html as I am windows user I will download XAMPP for windows.
How To Use It?
Install the downloaded XAMPP.
Go to the directory where it is installed.
There will be a folder named as “www” without quotation marks,Copy your php files to this server.
Now Turn on the WAMP and open your browser.
In address bar type this localhost/yourphpfilename.php
It will open your php file!.
In the end I would say that knowledge is power but sharing knowledge is powerful so feel free to share my tutorial on your blogs,sites and forums.