Till now we have dealt many server side programming languages. Also we have dealt with javascript which is a client side programming language along with DTD schema with XML, JSON etc. Today we will deal with a solid server side language called PHP which is used in many of the popular websites such as twitter, facebook, etc. PHP goes well with MYSQL.
So let’s learn basics of PHP in this PHP Fundamentals tutorial.
- What is PHP?
- PHP stands for PHP Hypertext Preprocessor.
- PHP was created by Rasmus Lerdorf in 1995.
- PHP is a server side scripting language i.e. everything is done on server, may it be decision making, condition checking, etc.
- PHP can be directly embedded into HTML using
<?php?>
tags.
- PHP is free and open source scripting language.
- PHP is a loosely typed language. It means that there is no need to define the type of a variable.
- PHP works great with databases like MSQL. It also works nicely with other database systems.
- What is PHP used for?
- PHP is mostly used with databases like MYSQL. It creates a user friendly interface when dealing with data.
- PHP generates web content/HTML.
- You can use command line scripting with PHP.
- PHP can even be used to build desktop applications.
- It can be used in an object oriented or procedural manner.
- Its more secure, more usable and a better programming language.
- Embedding PHP into HTML
- It is extremely easy to add PHP programming into HTML.
- All we need to do is embed
<?php?>
tag wherever we want to write the php code in the HTML document.
- Let’s have a look at an example below:
<h1>Welcome To My Page</h1> <p>This is my page</p> <p>Written by <?php echo $name;?></p>
<?php?>
tag.
<?php echo $name;?>
written above, $name is a variable name and echo is a command given for displaying/printing exactly like
document.write();
in javascript.
- All the programming basics are available in PHP such as variables, loops, arrays, functions and objects.
- Variables start with $ sign. ( Eg: $myVar=’my variable’;)
- Arrays can also have associative arrays. It is sometimes called as hash eg: array(“name” => “Brad”), here, name is a key and Brad is its value.
- In PHP we have for, while, do while and foreach loops.
- PHP works greatly with object oriented programming.
- We can create classes in PHP which represent an object.
- Objects in turn can have properties and methods.
- Let’s have a look on a small PHP class
Class dog { private $breed; private $color; public function bark() { echo “Bark”; } } $spot=new dog(); //To instantiate an object of class dog $spot->bark(); //call a class method
$spot->bark();
Thus we have gone through some introduction of PHP basics in this PHP Fundamentals tutorial.