Uncategorized

What Is Php Used For In Web Development

php

What is PHP and why it is used?

PHP is a server side scripting language. that is used to develop Static websites or Dynamic websites or Web applications. PHP stands for Hypertext Pre-processor, that earlier stood for Personal Home Pages. PHP scripts can only be interpreted on a server that has PHP installed.

PHP is a general-purpose scripting language especially suited to web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994. The PHP reference implementation is now produced by The PHP Group

What is the difference between HTML and PHP?

PHP stands for Hypertext Preprocessor. PHP is a server-side, scripting language (a script-based program) and is used to develop Web applications.

PHP vs HTML.

PHPHTML
It is used for the development of dynamic websites and dynamic web applications.It is used to create web pages.
PHP codes are dynamic.HTML codes are static.

Is PHP front end or backend?

PHP is a back end development language only. PHP belongs to the LAMP stack, which stands for Linux, Apache, MySQL, and PHP/Perl/Python. To develop a web app with this technology stack, a software engineer needs to know four different syntax systems, as well as HTML and CSS.

What are the basics of PHP?

Some basic PHP statements include:

  • echo: Output one or more strings.
  • print: Also output one or more strings.
  • The assignment statement: Assigns a value to a variable.
  • include: Include and evaluate the specified file.
  • require: Same as include except it produces a fatal error on failure instead of a warning.

PHP’s main Features

It is most prevalent and every now and again utilized overall scripting dialect, the fundamental reason of notoriety is; It is open source and exceptionally straightforward.

PHP's main Features
Simple

It is very simple and easy to use, compared to another scripting language it is very simple and easy, this is widely used all over the world.

Interpreted

It is an interpreted language, i.e. there is no need for compilation.

Faster

It is faster than other scripting languages e.g. asp and jsp.

Open Source

Open source means you no need to pay for using PHP, you can free download and use.

Platform Independent

PHP code will be run on every platform, Linux, Unix, Mac OS X, Windows.

Case Sensitive

PHP is case sensitive scripting language at the time of variable declaration. In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive.

Speed Comparision of ASP PHP JSP

PHP is faster than other scripting languages e.g. asp and jsp.

to learn more about PHP, Please visit PHP official website

PHP FUNCTIONS

PHP functions are similar to other programming languages. A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value.

You already have seen many functions like fopen() and fread() etc. They are built-in functions but PHP gives you option to create your own functions as well.

There are two parts which should be clear to you −

  • Creating a PHP Function
  • Calling a PHP Function

In fact you hardly need to create your own PHP function because there are already more than 1000 of built-in library functions created for different area and you just need to call them according to your requirement.

Creating PHP Function

Its very easy to create your own PHP function. Suppose you want to create a PHP function which will simply write a simple message on your browser when you will call it. Following example creates a function called writeMessage() and then calls it just after creating it.

Note that while creating a function its name should start with keyword function and all the PHP code should be put inside { and } braces as shown in the following example below −Live Demo

<html>
   
   <head>
      <title>Writing PHP Function</title>
   </head>
   
   <body>
      
      <?php
         /* Defining a PHP Function */
         function writeMessage() {
            echo "You are really a nice person, Have a nice time!";
         }
         
         /* Calling a PHP Function */
         writeMessage();
      ?>
      
   </body>
</html>

This will display following result −

You are really a nice person, Have a nice time!

PHP Functions with Parameters

PHP gives you option to pass your parameters inside a function. You can pass as many as parameters your like. These parameters work like variables inside your function. Following example takes two integer parameters and add them together and then print them.Live Demo

<html>
   
   <head>
      <title>Writing PHP Function with Parameters</title>
   </head>
   
   <body>
   
      <?php
         function addFunction($num1, $num2) {
            $sum = $num1 + $num2;
            echo "Sum of the two numbers is : $sum";
         }
         
         addFunction(10, 20);
      ?>
      
   </body>
</html>

This will display following result −

Sum of the two numbers is : 30

Passing Arguments by Reference

It is possible to pass arguments to functions by reference. This means that a reference to the variable is manipulated by the function rather than a copy of the variable’s value.

Any changes made to an argument in these cases will change the value of the original variable. You can pass an argument by reference by adding an ampersand to the variable name in either the function call or the function definition.

Following example depicts both the cases.Live Demo

<html>
   
   <head>
      <title>Passing Argument by Reference</title>
   </head>
   
   <body>
      
      <?php
         function addFive($num) {
            $num += 5;
         }
         
         function addSix(&$num) {
            $num += 6;
         }
         
         $orignum = 10;
         addFive( $orignum );
         
         echo "Original Value is $orignum<br />";
         
         addSix( $orignum );
         echo "Original Value is $orignum<br />";
      ?>
      
   </body>
</html>

This will display following result −

Original Value is 10
Original Value is 16 

PHP Functions returning value

A function can return a value using the return statement in conjunction with a value or object. return stops the execution of the function and sends the value back to the calling code.

You can return more than one value from a function using return array(1,2,3,4).

Following example takes two integer parameters and add them together and then returns their sum to the calling program. Note that return keyword is used to return a value from a function.Live Demo

<html>
   
   <head>
      <title>Writing PHP Function which returns value</title>
   </head>
   
   <body>
   
      <?php
         function addFunction($num1, $num2) {
            $sum = $num1 + $num2;
            return $sum;
         }
         $return_value = addFunction(10, 20);
         
         echo "Returned value from the function : $return_value";
      ?>
      
   </body>
</html>

This will display following result −

Returned value from the function : 30

Setting Default Values for Function Parameters

You can set a parameter to have a default value if the function’s caller doesn’t pass it.

Following function prints NULL in case use does not pass any value to this function.Live Demo

<html>
   
   <head>
      <title>Writing PHP Function which returns value</title>
   </head>
   
   <body>
      
      <?php
         function printMe($param = NULL) {
            print $param;
         }
         
         printMe("This is test");
         printMe();
      ?>
      
   </body>
</html>

This will produce following result −

This is test

Dynamic Function Calls

It is possible to assign function names as strings to variables and then treat these variables exactly as you would the function name itself. Following example depicts this behaviour.Live Demo

<html>
   
   <head>
      <title>Dynamic Function Calls</title>
   </head>
   
   <body>
      
      <?php
         function sayHello() {
            echo "Hello<br />";
         }
         
         $function_holder = "sayHello";
         $function_holder();
      ?>
      
   </body>
</html>

This will display following result −

ADVANTAGES AND DISADVANTAGES OF PHP

Advantages of Using PHP Frameworks

Speed up custom web application development

Nowadays, PHP programmers have to write web applications based on complex business requirements. Likewise, they have to explore ways to make the web application deliver richer user experience. The tools, features, and code snippets provided by PHP frameworks help developers to accelerate custom web application development.

Simplify web application maintenance

Unlike other programming languages, PHP does not emphasize on code readability and maintainability. The PHP frameworks simplify web application development and maintenance by supporting model-view-controller (MVC) architecture. The developers can take advantage of MVC architecture to divide a web application into models, views and controllers. They can use a MVC framework for PHP to keep the application’s user interface and business logic layers separated.

No need to write additional code

PHP, unlike other programming languages, does not allow programmers to express concepts without writing longer lines of code. Hence, the PHP programmers have to write lengthy and complex code while adding features or functionality to a website. The PHP frameworks reduce coding time significantly by providing code generation feature. The code generation features provided by certain PHP frameworks enable programmers to keep the source code of web application clean and maintainable.

Work with databases more efficiently

Most PHP frameworks allow programmers to work with a number of widely used relational databases. Some frameworks further simplify database operations by providing object relational mapping (ORM) systems. The programmers can take advantage of the ORM systems to perform database operations without writing lengthy SQL code. The ORMs even allows programmers to write object code directly in PHP programming language.

Automate common web development tasks

While building a web application, developers have to perform a number of tasks in addition to writing code. Some of these common web development tasks require programmers to invest additional time and effort. The functions and tools provided by PHP frameworks help developers to automate common web development tasks like caching, session management, authentication, and URL mapping.

Protect websites from targeted security attacks

PHP is considered to be one of the most unsecured programming languages. Often programmers have to explore ways to protect the PHP applications from various security attacks. The built-in security features and mechanisms provided by PHP framework make it easier for developers to protect the website from existing and emerging security threats. Also, the PHP web developers can easily prevent common security threats like SQL injections, cross-site request forgery, and data tampering.

Perform unit testing efficiently

While building a custom web application, developers have to perform unit testing regularly to evaluate its individual units or components. A large percentage of web developers use PHPUnit to perform unit tests quickly and efficiently. In addition to being an object-oriented unit testing framework for PHP, PHPUnit further helps developers to write and run unit tests by providing coding assistance. Many PHP frameworks support PHPUnit natively, and enable programmers to perform unit testing smoothly.

No need to increase web development cost

As an open source server-side programming language, PHP helps users to curtail web development cost significantly. The developers also have option to choose from several open source web frameworks for PHP. They can even avail the features and tools provided by these open source PHP frameworks speed up custom web application development without increasing project overheads.

Disadvantages of Using PHP Frameworks

Programmers need to learn PHP frameworks instead of PHP

The PHP frameworks enable programmers to add functionality to a web application without writing additional code. But the programmers have to put some time and effort to learn the PHP framework. They can even learn and use certain frameworks without being proficient in PHP coding.

Quality of PHP frameworks differs

Most widely used PHP frameworks are open source and free. Hence, the web developers can take advantage of these web frameworks without increasing the project cost. But the community strength of individual frameworks differs. Hence, some PHP frameworks lack prompt and adequate support.

Lack of option to modify core behavior

In addition to proving a basic structure for web application development, the PHP frameworks further accelerate custom web application development. But the developers still lack any option to make changes to the core behavior of these frameworks. Some frameworks even requirements developers to use specific tools or adopt a particular web development pattern.

Affect Speed and performance of websites

Most PHP frameworks come with robust features and tools to accelerate development of large and complex websites. But web developers may not need these advanced features while building small or simple web applications. Also, these additional features often impact the performance and speed of websites adversely.

On the whole, the web developers have option to choose from several full-stack and micro web frameworks for PHP. But the widely used PHP frameworks differ from each other in the category of functionality, usability, and performance. Also, each PHP framework has its own pros and cons. That is why, the web developers must keep in mind precise project requirements while assessing advantages and disadvantages of using PHP frameworks.

Drop your comment.

Leave a Reply

Your email address will not be published.