PHP vs. ASP. NET

Source: Internet
Author: User
Tags php language oracleconnection what is asp

Compare the advantages and disadvantages of the two most popular web application Construction Methods

As mentioned in Web development, you have many options. Many of these methods involve preprocessing-that is, embedding code into HTML pages using specific tags that tell the Preprocessor that they contain code and should handle it. Similar to CGI, these codes run on the server and return some content, which shows the partial appearance of the HTML page returned to the browser. The Open Source scripting language PHP and Microsoft's ASP. NET Framework all belong to this type of language, and JavaServer Pages (JSP) and Perl/Mason also run in this way.

In this article, I will focus on the technologies that PHP-Oracle has chosen to merge into its products-and ASP. NET. I will outline the strengths and weaknesses of both, and focus on factors that will help you decide which technology your development project should choose. There are many factors to consider. Different projects may resort to different technologies. In short, you will see a comparison of price, speed and efficiency, security, cross-platform support, and other aspects, as well as the advantages of open source code solutions.

What is ASP. NET?

ASP. NET, the latest version of ASP, is not completely backward compatible with earlier versions of ASP, because the software has been completely rewritten. In fact, the early ASP technology has much more in common with PHP than ASP. NET. ASP. NET is a complete framework used to build Web applications. One of the main features of this model is the flexibility of programming languages. ASP. NET can use scripting languages (such as VBScript, JScript, Perlscript, and Python) and compilation languages (such as VB, C #, C, Cobol, Smalltalk, and Lisp ). The new framework uses the universal language runtime environment (CLR). First, the source code of your language is compiled into Microsoft intermediate language code, and then the CLR executes the code.

This framework also provides real Object-Oriented Programming (OOP) and supports true inheritance, polymorphism, and encapsulation .. . NET class libraries are organized into inherited classes based on specific tasks (such as using XML or image processing.

In addition to programming languages and methods, database access is also a key concern. When you use ASP. NET programming, You Can Use ODBC to integrate the database. ODBC provides a set of consistent calling functions to access your target database.

Strengths and weaknesses

The advantage of ASP. NET is its concise design and implementation. This is the dream of object-oriented programmers: flexible language and support for complex object-oriented features. In this sense, it can truly interwork with existing skills of programmers.

Another advantage of ASP. NET is its development environment. For example, developers can use WebMatrix (a tool supported by the community), Visual Studio. NET, or a variety of Borland tools (such as Delphi and C ++ Builder ). For example, Visual Studio allows you to set breakpoints, trace code segments, and view call stacks. All in all, it is a complex debugging environment. Many other third-party ASP. net ide solutions will also emerge.

However, if you get robustness, it will be at the cost of efficiency loss. ASP. NET consumes a lot of memory usage and execution time, most of which is due to a long code path. For Web-based applications, these limitations may be a serious problem, because on the Web, your applications may expand to thousands of users per second. Memory usage may also be a problem on the Web server.

What is PHP?

PHP is a script language based on the pre-processing HTML page model. When the PHP Preprocessor in the Web server finds a PHP language mark like the following, it will call the PHP engine to execute the Code:

<?php
some code here
?>

Any programmer who has used imperative programming languages will be very familiar with PHP; you will find it syntactically similar to Perl, C, and Java. Strictly speaking, Java is a imperative programming language, but it also utilizes object-oriented structures and concepts. PHP draws on this structure when appropriate, but it is not a pure OOP language.

In the above discussion with ASP. NET, I mentioned the ODBC driver and how to build an application while considering database abstraction. In PHP, you can also use ODBC to talk to a database, so you already have a series of supported databases to choose from. There are also native drivers for MySQL, Oracle, and ipvs. In addition, if you want to use Oracle, a special OCI8 database will provide more access to Oracle, allowing you to use features such as LOB, BLOB, CLOB, and BFILE.

At this time, you may ask "why is the database-related database called the PHP feature ?" Database abstraction or independence is a factor to consider when you try to build an application that uses multiple databases or that needs to be transplanted between databases (for example, from development to production. These are indeed considerations.

However, as Tom Kyte pointed out in his new book, objective Oracle by Design (Oracle Press), Database relevance will become your real goal, this maximizes your investment in this technology. If you perform general access to Oracle-whether using the ODBC or Perl DBI database, you will not be able to use the features that other databases do not have. In addition, the optimized query varies in different databases.

Zend Technologies is a commercial software company that has made significant contributions to PHP. It creates a commercial development environment called Zend Studio, which contains a complex debugger, a monitor, and other features. The company also built a free Zend Optimizer, which is used in combination with Zend Encoder to compile PHP code to improve performance. Other commercial products, such as Zend Performance Suite, can cache pre-compiled PHP pages to further significantly improve overall Performance.

Strengths and weaknesses

PHP 5 still has some disadvantages until beta 4, including the lack of exceptions and event-based error handling routines-they can interrupt normal program streams, and jump the code to a special error handling section. Java also provides exceptions for error handling, while C ++ provides Exception Handling through try, catch, and throw syntax. Of course, you can still manage errors in PHP, but the structure is not standardized, so programmers must use their own tools to determine how to handle errors, this will lead to lower consistency and often lead to re-engineering.

Another weakness is that PHP function names are case-insensitive. Although this is not a serious drawback, some programmers may find this feature very annoying.

However, I do have doubts about the object model of PHP. PHP is not specifically designed as an object-oriented language. Some of these features were added later-although you have noticed backward compatibility with PHP 3, the features of both models are retained. In fact, many of these vulnerabilities have been addressed in PHP 5. Pay attention to it.

If PHP lacks anything in some fields, it will quickly make up for what it is good. The price is reasonable, so you do not need to worry about licensing issues. It is also open-source, so the whole community will pay close attention to the development process: identify and fix errors. If you do not like a feature, you can modify the code. In addition, PHP can be combined with Apache: It can be compiled as a module or directly compiled into an Apache binary file.

However, running on Apache means that you can use PHP to invest on any server because Apache can run on Windows, Linux, Solaris, and other Unix platforms. In addition, using a web server with Apache tracking records means that security can be maintained at the highest priority. Finally, PHP has a smaller code path, which means less analysis and execution of PHP page server code, which will bring more efficient memory and usage and faster running.

What features are added in PHP 5?

PHP 5's 4th beta versions were launched by the end of December 2003. The change log clearly shows that many errors have been found and eliminated. Although it is still being tested, all its new features and advances are indeed worth noting.

The main new achievement of PHP 5 lies in its exception handling and a new object. The features introduced by this new object give PHP real OOP. Exception Handling is undoubtedly one of the most significant shortcomings in PHP 4, and the introduction of PHP 5 to exception handling is undoubtedly a mature sign. Exception Handling means that you have a language-defined and standardized error handling method in your software. By using try, catch, and throw methods, your PHP code becomes more robust and concise.

<?php

class blue {

function openFile ($inFile) {
if (file_exists ($inFile)) {
# code to open the file here
} else {
throw new Exception
("Cannot open file:$inFile");
}
}
}

$blueObj = new blue ();

try {
$blueObj->openFile ('/home/shull/file.txt');

} catch (Exception $myException) {
echo $myException->getMessage ();

# rest of exception handling code here
}

# rest of blue methods here

?>

The new object model has a lot of positive impact on PHP programs. In PHP 4, when an object is passed to a function or method, the value is passed-unless you explicitly inform PHP. This process means that one copy of the object (all data structures in the memory) must be copied ). This step uses memory to make access slow and congested. In PHP 5, objects are usually passed through references.

The new object-oriented features (including constructors and destructor) in PHP 5 are worth noting. Similar to C ++ and Java, they provide a standard method, that is, using a constructor method to create objects, allocate memory, and execute any necessary settings, A destructor is used to clean up the data.

PHP 5 also introduces more refined control over Chinese methods and variables. In PHP 4, everything is public: You can access variables in your class from outside the class or in the inherited class. In PHP 5, you can still make variables or methods public, but you can also make them private so that they can only be used in the class itself. You can also protect them, that is, you can view methods and variables in the class or in the subclass.

In addition, PHP 5 introduces Type prompts or better type checks. When you pass an object to a routine, PHP can check whether it is of the correct type and generate a Type Mismatch Error when the check fails.

Because there are other features (such as static methods, variables, and abstract classes), be sure to view the document for details.

Security comparison

ASP. NET officially requires you to use IIS. Unfortunately, IIS is vulnerable to attacks for a long time, which makes many administrators reluctant to deploy it to process Web sites. These vulnerabilities are caused by Microsoft defects or because IIS is the target of a hacker's attack and do not matter: these systems have a history of being hacked or attacked. PHP also runs on Apache. Apache is fast and open-source, and has a good security record. In addition, as I mentioned, Apache can run on many platforms.

If you are considering ASP. NET but want to use Apache as an Internet portal, you are lucky to have some options. First, you can use Apache to forward requests to IIS running on another computer. Apache then processes static content and sends the aspx content to the IIS server (not exposed to the Internet ).

However, if you want to use Apache to host ASP. NET, some options are provided, which may or may not be supported by Microsoft. As the final option, Project Mono with Ximian is dedicated to building an open source code module. Browse the www.go-mono.com for more information.

Database encoding example

When using PHP or ASP. NET, you must first consider the connection to the database. However, using ASP. NET is more complex because you can select any language from many alternative languages. Of course, these code examples must be embedded into HTML pages and instantiated classes. However, the following information will help you understand the encoding style of the two.

Connection between PHP 5 and Oracle

Below is a PHP 5 class, which provides an Oracle connection and disconnection routine to demonstrate the use of PHP 5 (other drivers (such as ODBC drivers) and general database interfaces) A method for connecting to Oracle:

class oracle_object {
protected $theDB;
protected $user;
protected $pass;
protected $db;

function __construct($u, $p, $d) {
$this->user = $u;
$this->pass = $p;
$this->db = $d;
}

function db_open () {
$theDB = @OCILogon($this->user, $this->pass, $this->db);
db_check_errors($php_errormsg);
}

function db_close() {
@OCILogoff($theDB);
db_check_errors($php_errormsg);
}

function __destruct () {
print ("so long...");
}

}

Connection between ASP. NET and Oracle

If you want to connect to Oracle using VB. NET (Visual Basic is Microsoft's default. NET Programming Language), take a look at this example from MSDN:

Imports System
Imports System.Data
Imports System.Data.OracleClient
Imports Microsoft.VisualBasic

Class Sample

Public Shared Sub Main()

Dim oraConn As OracleConnection = New OracleConnection("Data Source=MyOracleServer;Integrated Security=yes;")

Dim oraCMD As OracleCommand = New OracleCommand("SELECT CUSTOMER_ID, NAME FROM DEMO.CUSTOMER", oraConn)

oraConn.Open()

Dim myReader As OracleDataReader = oraCMD.ExecuteReader()

Do While (myReader.Read())
Console.WriteLine(vbTab & "{0}" & vbTab & "{1}", myReader.GetInt32(0), myReader.GetString(1))
Loop

myReader.Close()
oraConn.Close()
End Sub
End Class

Make a choice

Assuming that you have not decided to use PHP, I can assert that the advantage of PHP far exceeds its weakness. (See the Summary In table 1 .) These benefits are attributed to price, speed and efficiency, security, cross-platform applicability, and open source code opportunities. Its only weakness is the lack of a pure and perfect OOP implementation. However, this is a small drawback. Although the language structure is indeed helpful, good coding is ultimately brought about by practice, execution, good habits and norms.

Table 1
  PHP 4 PHP 5 ASP. NET
Software price Free Free Free
Platform price Free Free $
Speed Strong Strong Weak
Efficiency Strong Strong Weak
Security Strong Strong Strong
Platform Strong Strong Weak (for IIS only)
Platform Arbitrary Arbitrary Win32 (for IIS only)
Source code? Yes Yes No
Exception No Yes Yes
OOP Weak Strong Strong

Price. Here, we should not simply consider the initial investment-which is obviously free for PHP-as well as the cost of implementation, maintenance, and debugging. For PHP, you may need to purchase the Zend Optimization engine. However, with ASP, you will invest in it from the very beginning, and you will also pay for additional technologies, such as libraries that perform graphic processing. However, in the long run, PHP will not force you to upgrade and charge you more license fees. Everyone who has been familiar with complex licensing knows that many companies spend a lot of time and money just to ensure compliance. In addition, when you get an error fix, you get a different response. Of course, this will be converted into time, and time will be converted into the overall development cost.

Speed and efficiency. As I mentioned earlier, ASP. NET is a framework that allows you to use various programming languages. In addition, it is said to have an excellent object-oriented model. Although all of these are true, It is unfavorable when considering the speed. For the above reason, running ASP pages in ASP. NET requires more code than running equivalent PHP pages in the PHP engine. PHP is a "fast and rough" solution that is designed to complete the work. Although it has improved a lot of robustness since versions 2.0 and 3.0, it still retains the high-speed method of core optimization.

Speed is not the only factor to consider. Memory usage is also important.

Security. ASP. NET runs on IIS, and IIS has been attacked countless times-as confirmed in the IT news report every other week. IT has become a burden. In fact, despite its huge investment in sales promotion, many IT professionals still refuse to use IIS Web servers to open their networks. PHP uses Apache. Apache has proven fast, reliable, and secure records. Please visit www.securityfocus.com for more information.

Cross-platform applicability. ASP. NET runs on IIS and starts to run on Apache (Apache can run on many platforms ). PHP was designed to work with Apache from the very beginning, so you have many proven and reliable server platforms to choose from.

Open source code opportunities. Open-source code does not only rely on whimsical programmers or companies that want to save license fees. When you handle software errors, open source code may become a real godsend.

When PHP or ASP. NET is used, you will have a large user group. They use software and may encounter errors. With ASP. NET, these errors must be notified, repaired, tested, and eliminated in a new patch or version. However, PHP patches can be quickly patched and released. Anyone who has witnessed the development of open source code knows that new versions and patches are usually released within a few days rather than just weeks or months like commercial software. If this is not fast enough, you can fix the problem yourself (if necessary ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.