ASP-PHP Language Comparison

advertisement
ASP-PHP Syntax Comparison
Feature
PHP
VBScript
Example (print <html> <body>
<?php
1 to 10 on a
web page)
for ($i=1; $i<=10; $i++)
print "$i<br>";
?>
</body></html>
<html> <body>
<%
for i = 1 to 10
Response.Write(i &
"<br>")
next
%>
</body></html>
Script Tags
<?php ?>
If short tags are enabled in PHP.ini:
<?
?>
If ASP tags are enabled in PHP.ini:
<%
%>
<%= return expression %>
<%
%>
<%=return expression %>
End of
statement
semicolon (;) is compulsory
End of line
Comments
// PHP comment 1
/* This is a
multi-line
Comment */
# PHP comment 3
' VBScript comment
Variables
Prefixed by $
No prefix
Need to
declare
variables?
No, and every variable is preinitialised to Optional. Error if variable
type NULL, which is equivalent to false, not initialised when used.
the empty string and 0.
Dim AVAR
$AVAR = 123;
Loosely Typed Yes. This means that the type of a
Yes
variable can be changed at run-time.
Variables
To change variable type, use typecasting
or settype( )
CaseSensitivity
Yes for variables, no for reserved words
No
Strings
Delimited by single-quotes, doublequotes or PERL heredoc style.
$avar = 'this is a string';
$avar = "this is a string";
$avar = <<<EOS
this is a string
EOS;
Delimited by double-quotes.
The . (dot) symbol.
String
Concatenation $s = 'A' . 'B';
The & (ampersand) symbol.
s = "A" & "B"
String
Evaluation
Yes, variables embedded in strings are
evaluated if string delimited by doublequotes.
$a = "A";
$b = "$a B";
/* now $b == 'A B' */
No
Common
string
constants
Line feed: "\n"
Carriage return: "\r"
Note that single-quoted 'n' is not
Line feed: vbLf
Carriage return: vbCr
converted to a line-feed for PHP. Only
double-quoted strings are evaluated.
HTML
encoding
functions
htmlspecialchars($str) Converts < > &
and " to their HTML entity equivalents,
eg. < becomes <
urlencode( ) All punctuation, accented
characters, and any other non-ASCII
characters are replaced with %xx
encoding. Spaces converted to +.
urldecode( )All punctuation, accented
characters, and any other non-ASCII
characters are replaced with %xx
encoding. Spaces converted to +.
Requires ASP Server object
(and consequent COM
overhead).
Server.HMTLEncode( )
Similar to PHP's
htmlspecialchars.
Server.URLEncode( )
Similar to PHP's urlencode.
Regular
Expressions
Built-in.
if (preg_match ("/php/i", $str))
print ("php found");
Use COM object RegExp.
Only available with the latest
versions of VBScript (5.5 or
later).
Dates
$adate = time();
$adate=mktime(0,0,0,1,30,2000);
adate = Now
date= CDate('2000-1-30')
print date('d-m-Y',$adate);
FormatDateTime(adate,2)
Dates are formated using the
current locale configured in
the control panel, so you
have to hand-code date
formatting or change your
locale.
Declare with
$avar = array(1,'two');
For indexing use [ ]. Arrays begin with
zero element.
Use sizeof(array) function to get size of
array.
Arrays can be iterated over using:
$val = reset($arr);
$max = sizeof($arr);
for ($i=0; $i<$max; $i++) {
print "$val<BR>";
$val = next($arr);
}
Declare with
DIM avar(2)
For indexing use ( ). Arrays
begin with zero element.
Arrays
/* or more simply */
foreach ($arr as $element)
print "$element<BR>";
Associative
Arrays
Yes
$avar = array();
$avar['newton']='isaac';
echo $avar['newton'];
Note: You can append new elements to
arrays with
$avar[] = 'string to add';
True and False Has constants true and false.
The following also evaluate to false:
0 /* zero */
Not built into the language,
but can simulated using the
Dictionary object (which is
not thread-safe in VBScript
5.0).
Has constants true and false.
Unlike PHP and JScript, zero
does not evaluate to false.
"" /* empty string */
"0" /* string with zero */
Equality
== true if equal
!= not equal
= true if equal
<> not equal
=== true if equal & same type
!== not identical
Note that some functions can return 0 or
false (false is typically used to indicate
failure) depending on the context. For
these functions, you will need to use ===
to determine whether 0 or false was
returned. Some problematic functions:
strpos, strrpos, strstr, stristr, strrchr
Purpose
Assignment
statements
Allows C style shortcut assignments. For No shortcut assignments.
example, to append a string to a variable:
$avar .= 'append to end';
Sending
HTML to
Browser
print $avar;
echo "The avar variable '$avar' is a
string";
Response.Write(avar);
If statements
if (strlen($avar) == 0) {
$avar = "abc";
} else
$avar .= 'end';
if len(avar) = 0 then
avar = "abc"
else
avar = avar & "end"
endif
While
statements
while ($a > 0) {$a -= 1;}
while a > 0
a=a-1
wend
For loops
for ($i=0,$m=9; $i<$m; $i++){
print $i;
}
for i=1 to 100
Response.Write(i)
next
Switch/Case
switch($aColor) {
case 'red': do1;break;
case 'green': do2;break;
default:
do3; break;
}
select case aColor
case "red"
do1
case "green"
do2
case else
do3
end select
ASP Object
Writing HTML Response.Write(str)
Include files
Form, Cookie
and
PHP Equivalent
print $str;
echo $str;
print_r $debug_str;
Supported by IIS within page HTML using
<!--#include file=”filename”
PHP statements:
Available in Request object.
These variables are available automatically as global variables
if you have configured the following in PHP.ini:
require("header.html"); or
include("header.html");
Require generates a fatal error if the file cannot be found (preferred)
while include produces a warning at the bottom of the page.
variables_order="EGPCS"
register_globals=On
For security, I would recommend disabling register_globals
(set it to OFF). Then the variables are only available in the
arrays:
$_POST_VARS, $_COOKIE and $_GET_VARS.
See examples below.
QueryString
variables.
Redirecting to
another
location
Response.Redirect(url)
Header("Location: $url");
Cookie
Handling
Response.Cookies(cookiename) = newval
avar = Request.Cookies(cookiename)
setcookie($cookiename, $newval);
$avar = $_COOKIE[$cookiename];
Application
Variables
Application(appvarname)
Not available. Can be simulated with a database.
Session
Variables
Session(sessionname) = newval
avar = Session(sessionname)
In PHP4 or later, we mark certain variables as session
variables using session_register($sessionname), and we call
session_start( ) at the beginning of the .php page to restore the
session variable values.
Note that we pass the name of the variable, and not the
variable itself into session_register( ).
session_register('avar');
$avar = 99;
session_start();
/* 99 in $avar is overwritten now */
print $avar;
Form Variables Request.Form("formvar")
Request.QueryString("getvar")
$_POST["formvar"];
$_GET["getvar"];
Alternately, GET and POST vars can be converted
automatically to PHP variables. However this is a security risk
if hackers are aware of the variable names used in your code.
Server
Variables
There are many server variables. See ASP
documentation. An example:
Request.ServerVariables("HTTP_HOST")
For ISAPI modules, the server varibles are stored in the
$HTTP_SERVER_VARS array. For CGI, they are stored as
environment variables, available from the
$HTTP_ENV_VARS array or getenv( ). An example:
$HTTP_SERVER_VARS["HTTP_HOST"] using ISAPI
module
$HTTP_ENV_VARS["HTTP_HOST"] using CGI module
See http://php.weblogs.com/Apache_IIS for a more detailed
discussion of CGI vs. ISAPI.
Database
Access
Microsoft's ADO technology is commonly
used.
ADO can be simulated using the ADODB database library.
This PHP library emulates ADO.
Limitations: only supports forward scrolling read-only
cursors.
Buffering
Response.Buffer = true
Response.Write("abc");
Response.Flush()
ob_start();
print "abc";
ob_end_flush();
Script Timeout Timeout is in seconds:
Server.ScriptTimeout(240)
Modified from: http://php.weblogs.com/php_jscript_vbscript_1
Timeout is in seconds:
set_time_limit(240);
Download