Article Tag Cloud

No of visitors who read this post: 2181
Category: Programming
Type: Article
Author: Daniel David
Average: 4.5 (29 votes)

Basic PHP

Introduction

PHP stands for PHP: Hypertext Preprocessor. The first P is actually recursive. It is created by  Rasmus Lerdorf in 1995 and was expanded as Personal Home Page, but was renamed to this form in version 3.0 when released in June 1998. Now PHP is greatly popular.
 
 
You might have wondered seeing the number of opportunities for PHP in freelancing and other job sites. PHP is a server side scripting language used to design dynamic web pages, similar to ASP and JSP. Though HTML is used for the same purpose, much more can be done using PHP. You can collect data from users, can have database interactions etc. Even large websites can be created very easily with reduced time.
 
PHP is designed to use along with HTML. You can use PHP inside your existing HTML code, or can put your HTML tags inside the PHP code. Actually PHP is not an isolated language. It adds some functionality to and existing HTML code. And once the request is sent to server, the output produced will be a pure HTML document that is if you right click on a page and click View Source (Internet Explorer), you can’t find that the code contained PHP. The files with PHP coding are to be saved with extension php file. PHP is mostly used along with MySQL database, but still it can be programmed to interact with any other database.
 
PHP Program
 
PHP code is always written within PHP tag like this <?php ........?>. It has syntax very similar to that of a C/C++/Javascript. All statements end with semicolon (;). All conditional statements and assignment statements are written in the same way. But variables can be declared same as in Javascript coding. The variables are un-typed and the variable names start with $.
 
Sample PHP Program
 
<html>
 
<head>
 
<title> My First PHP Page</title>
 
</head>
 
<body>
 
<?php
 
echo “Inside PHP”;
 
?>
 
</body>
 
</html>
 
The file with this code is to be saved with .php extension. If it is placed on PHP enabled server and loaded in the browser, it shows the output as Inside PHP. The first thing to be noted in the above sample is that the PHP code is written within php tag and the php statement ends with a semicolon. “echo” keyword is used to output a string. It is not actually a function, but a language construct.
 
Variable in PHP can be declared with $ sign added at the beginning, also they are case sensitive. For e.g. if you plan to use a string welcome with a value, you should create it as: $welcome=”Hello Good Morning”. But for defining Constants no dollar sign is needed. It can be done as follows:
 
define (“PI”, 3.14);
 
The data types available in PHP are integers, doubles and strings.
 
For a commenting a single line in PHP there are 2 options -
 
1. //
 
2. #
 
For multiple line comments, start the commenting with /* and end it with */.
 
Different Operators available in PHP are
  • Assignment Operators (=)
  • Arithmetic Operators (+,-,*, /, %)
  • Comparison Operators (==, =,<,>, ≤,≥)
  • String Operators (.)
  • Combination Arithmetic and Assignment Operators (+=,-=,*=, /=, %=,.=)
Different Statements available in PHP are
  • If, if/else if statements
  • Switch/case statement
  • For, while and do while statements
  • Include and require statements
All the above operators and statements are known to any programmer with basic programming knowledge. So it is not explained in detail in this article. Include and require statements are identical and they are used to avoid code repetition. Suppose the same code is to be repeated in many pages, that code can be kept in a single file and whenever it is required you can use an include statement with that particular file name. For e.g. if the repeating code is kept in a file named (common.php), whenever you want to use that code you can specify <? php include (“common.php”); > or <? php require (“common.php”);>. You can thus avoid repetition of code. An include statement if come across an error generates a warning and continue execution, while a require statement after generating the warning, it stops execution of the program. The difference is only in the way in which the error is handled.
 
Different Built-In Functions available are
  • Array Manipulator Functions (merge, sort, count etc)
  • Date and Time Functions (getdate, localtime, date etc)
  • File system Functions (For dealing with flat files)
  • Error Handling Functions
  • Directory Functions
  • Mail Functions (For dealing with e-mails)
  • Database Functions ( For dealing with database)
PHP can also be used to handle many complex software requirements like cookie handling, dynamic image handling, client server applications, encryption techniques, artificial intelligence, user interactions etc.
 
Advantages
  • Reduced Development Time: PHP helps to develop web applications very rapidly and efficiently.
  • Speed: When compared with other scripting languages, PHP is showing better performance in terms of speed. Even during complex process like database interaction, it takes considerably less time. So it is used in applications that need high performance e.g. server administration, mail functionalities etc.
  • Platform independent: PHP supports many platforms like Windows, Linux etc. But many scripting languages are designed for a particular platform.
  • Easy syntax: Anybody having a basic programming knowledge can easily learn the syntax of PHP. It is very much similar to C syntax.
  • Database connectivity: PHP can be connected to a number of different databases.
  • Open source: The user is given a free license to reuse PHP. Thus it is an open source language.
  • PHP supports both structural and object oriented programming concepts.
  • Easy deployment
Disadvantages
  • Error Handling: The main disadvantage of PHP is in the area of Error Handling. When compared to other scripting languages, PHP has a very poor error handling methods.
  • Security flaws: As PHP is a web programming language, it is more prone to vulnerabilities.
  • Performance: PHP does not provide performance of C or C++.
  • Considering the advantages and disadvantages of PHP and also the requirements of our web application, it is our duty to make a wise decision on the choice of scripting language.