Dynamic Web Development PHP, ASP or asp.net (2)

Source: Internet
Author: User
Tags end error handling net variables php and php example variable mysql database

Practice-language comparison

variable declarations

in VBScript (both ASP and asp.net use VBScript), it is not always important to declare a variable before it is used, although technical documentation is usually recommended. Using the Option Explicit declaration, a developer can enforce a variable declaration through a program. In PHP, variables can be declared, although there is no way to force developers to do this. Indeed, variables are declared automatically before they are used. The advantage of PHP variables is that variables can be set as references to other variables (references), whereas in VBScript variables can be defined only by value.   

The following is a reference fragment:
<%
' VBScript Example
Option Explicit
MyVar = 1
Myothervar = MyVar   
MyVar = 2
' Myresult'll be 3
Myresult = MyVar + myothervar
%>

//PHP Example
$myVar = 1;
' Use the ampersand to make a reference
$myOtherVar = & $myVar;
$myVar = 2;
//$myResult would be 4
$myResult = $myVar + $myOtherVar;
?>

Variable Collection
  
The way you use forms and query string variables in PHP and in ASP is very similar. There are many ways to access a form and a collection of query string variables, such as by name or as an array. There are many different situations in asp.net, especially for form fields. Unlike blindly looking for a submitted form variable, Code-behind can know every form field in an HTML page and can trigger a check on the values of those form fields as a condition for any known event execution. One of these events is "postback", which is triggered when the form user submits. Other events can be client-side programs and can be triggered by JavaScript. In ASP.net, there is no difference in nature between the two.

The following is a reference fragment:
<%
' ASP Example
Myformval = Request.Form ("Myinputfield")
Myqsval = Request.QueryString ("Myqsitem")
Myval = Request.item ("Myformorqsitem")
%>
?
PHP 4.1+ Example
$myFormVal = $_post[' Myinputfield '];
$myQSval = $_request[' Myqsitem '];
PHP 3+ Example
$myFormVal = $HTTP _post_vars[' Myinputfield '];
If register_globals = On
$myVal = $myFormOrQSitem;
?>
<!--asp.net example-->
<script language= "VB" runat=server>
Sub SubmitBtn_Click (Sender as Object, E as EventArgs)
Message.Text = "Hello" & Name.text
End Sub
</script>
<body>
<form action= "action.aspx" method= "POST" runat= "Server" >
Name: <asp:textbox id= "name" runat= "Server"/>
<asp:button text= "OK"
runat= "Server"/>
<asp:label id= "message" runat= "Server"/>
</form>
</body>

strings concatenation (string concatenation)
  
PHP seems to give enough attention to this issue, which allows you to insert variables into a string without having to consider the usual concatenation (concatenation) problem. Asp. NET will make the whole process more troublesome, need to use its StringBuilder class, but this asp.net running speed is also much faster.
  
The following is a reference fragment:
?
PHP Example
$link = mysql_connect ("host", "User", "password") or Die ("mysql_error ());"
mysql_select_db ("database") or Die ("could not select Database");
$query = "SELECT * from Table";
$result = mysql_query ($query) or Die (Mysql_error ());
while ($line = Mysql_fetch_array ($result, Mysql_assoc)) {
foreach ($line as $col _value) {
Do something
}
}
?>

Connecting to a database
  
For the connection problem of the database, each kind of technology has demonstrated each outstanding normative. First, in each case, a connection to the database is established. For PHP, select the database after the build (for ASP and ASP.net will be completed during the connection phase). A query is then created and routed to the database, which may or may not produce a return record.
  
Because in essence asp.net is more object-oriented and supports complex error handling (the error handling), asp.net may need to write more code to accomplish simple tasks, whether it is relative to PHP or ASP. But in terms of advantages, ASP. NET to complete the display of data functionality is much less than PHP and asp--especially if you use built-in DataGrid control to automatically create HTML output.
  
The following is a reference fragment:
<%
' ASP Example
Set objconn = Server.CreateObject ("ADODB. Connection ")
objConn.Open "Driver={sql Server}; Server=myservername; "& _
"Database=mydatabasename; uid=; Pwd= "
Const STRSQL = "SELECT * from Table" Set objRS = Server.CreateObject ("ADODB. Recordset ")
Objrs.openstrsql, objconn
Do as Not objrs.eof
' Do something
Objrs.movenext
Loop
%>
' ASP.net Example
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SqlClient"%>
<script language= "VB" runat= "Server" >
Sub Page_Load (Sender as Object, E as EventArgs)
Dim myconn As SqlConnection = New SqlConnection ("server= (local) ... ")
Dim Mycomm as SqlCommand = New SqlCommand ("SELECT * from Table", myconn)
MyConn.Open ()
Dim dr As SqlDataReader = Mycomm.executereader ()
Mydatagrid.datasource = Dr
Mydatagrid.databind ()
Myconn.close ()
End Sub
</script>
<body>
<asp:datagrid id= "Mydatagrid" runat= "Server"
Width= "600"
Backcolor= "#FFFFFF"
Bordercolor= "#000000"
Showfooter= "false"
cellpadding=2
cellspacing= "0"
Font-name= "Verdana"
Font-size= "8pt"
Headerstyle-backcolor= "#EEEEEE"
Enableviewstate= "false"
/>
</body>
  
Conclusion
  
Choosing ASP, PHP, or asp.net will ultimately depend on the needs of your application and the system environment in which you run your program. Developers ' familiarity with similar programming languages or paradigms can also be a factor of choice. Remember that there is no perfect method or personal reality to show which technology is the best choice. For example, using ASP.net to create a one-page form mail application for a Windows server may seem a bit overkill, but this is an excellent application environment for ASPs. If a site needs to be connected to a MySQL database on a Linux Apache server, then using an ASP or asp.net will appear powerless. If the user's personal requirements are carefully considered in advance, the developer's choice of competing technologies is half the battle.



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.