ASP (VBScript)PHP (v4.3+)
General syntax
ASP Comments, inline'my dog has fleasPHP Comments, inline//my dog has fleas
ASP Comments, blocknot available?PHP Comments, block/*@郑州网建
The quick brown fox
jumped over the lazy dogs.
*/
ASP, Escaping quotes""
"var text1="""";"PHP, Escaping quotes\" or use ' like javascript
'var text1="";';
ASP Command terminationNone, but : can be used to separate commands
on the same line.PHP Command terminationEach command must end with ; but
multiple commands per line are allowed.
ASP Screen outputresponse.write "hello"PHP Screen outputecho "hello";
ASP Newline charactersvbCrLf
response.write "hello" & vbCrLfPHP Newline characters"\n" (must be inside "", not '')
echo "hello \n";
ASP Variable NamesNot case sensitive,
so fName is the same as FNAMEPHP Variable NamesCase sensitive AND must begin with $
so $fName is NOT the same as $FNAME
String Functions @camnpr
ASP String concatenation&
fname=name1 & " " & name2
emsg=emsg & "error!"PHP String concatenation. and .=
$fname=$name1." ".$name2;
$emsg.="error!";
ASP, Change caseLCase(), UCase()
lowerName=LCase(chatName)
upperName=UCase(chatName)PHP, Change casestrtolower(), strtoupper()
$lowerName=strtolower($chatName);
$upperName=strtoupper($chatName);
ASP String lengthLen()
n=Len(chatName)PHP String lengthstrlen()
$n=strlen($chatName);
ASP, Trim whitespaceTrim()
temp=Trim(xpage)PHP, Trim whitespacetrim() and also ltrim(), rtrim()
$temp=trim($xpage);
ASP String sectionsLeft(), Right(), Mid()
Left("abcdef",3) result = "abc"
Right("abcdef",2) result = "ef"
Mid("abcdef",3) result = "cdef"
Mid("abcdef",2,4) result = "bcde"PHP String sectionssubstr()
substr("abcdef",0,3); result = "abc"
substr("abcdef",-2); result = "ef"
substr("abcdef",2); result = "cdef"
substr("abcdef",1,4); result = "bcde"
ASP String search forward, reverseInstr(), InstrRev()
x=Instr("abcdef","de") x=4
x=InstrRev("alabama","a") x=7PHP String search forward, reversestrpos(), strrpos()
$x=strpos("abcdef","de"); x=3 //@camnpr
$x=strrpos("alabama","a"); x=6
ASP String replaceReplace(string exp,search,replace)
temp=Replace(temp,"orange","apple")
temp=Replace(temp,"'","\'")
temp=Replace(temp,"""","\""")PHP String replacestr_replace(search,replace,string exp)
$temp=str_replace("orange","apple",$temp);
$temp=str_replace("'","\\'",$temp);
$temp=str_replace("\"","\\\"",$temp);
ASP, split a string into an arraySplit()
temp="cows,horses,chickens"
farm=Split(temp,",",-1,1)
x=farm(0)PHP, split a string into an arrayexplode()
$temp="cows,horses,chickens";
$farm=explode(",",$temp);
$x=$farm[0];
ASP, convert ASCII to Stringx=Chr(65) x="A"PHP, convert ASCII to String$x=chr(65); x="A"
ASP, convert String to ASCIIx=Asc("A") x=65PHP, convert String to ASCII$x=ord("A") x=65
Control Structures
ASP, if statementsif x=100 then
x=x+5
elseif x<200 then
x=x+2
else
x=x+1
end ifPHP, if statementsif ($x==100) {
$x=$x+5;
}
else if ($x<200) {
$x=$x+2;
}
else {
$x++;
}
ASP, for loopsfor x=0 to 100 step 2
if x>p then exit fornextPHP, for loopsfor ($x=0; $x<=100; $x+=2) {
if ($x>$p) {break;}
}
ASP, while loopsdo while x<100
x=x+1
if x>p then exit do
loopPHP, while loopswhile ($x<100) {
$x++;
if ($x>$p) {break;}
}
ASP, branchingselect case chartName
case "TopSales"
theTitle="Best Sellers"
theClass="S"
case "TopSingles"
theTitle="Singles Chart"
theClass="S"
case "TopAlbums"
theTitle="Album Chart"
theClass="A"
case else
theTitle="Not Found"
end selectPHP, branchingswitch ($chartName) {
case "TopSales":
$theTitle="Best Sellers"; $theClass="S";
break;
case "TopSingles":
$theTitle="Singles Chart"; $theClass="S";
break;
case "TopAlbums":
$theTitle="Album Chart"; $theClass="A";
break;
default:
$theTitle="Not Found";
}
ASP functionsFunction myFunction(x)
myFunction = x*16 'Return value
End FunctionPHP functionsfunction myFunction($x) {
return $x*16; //Return value
}
HTTP Environment
ASP, Server variablesRequest.ServerVariables("SERVER_NAME")
Request.ServerVariables("SCRIPT_NAME")
Request.ServerVariables("HTTP_USER_AGENT")
Request.ServerVariables("REMOTE_ADDR")
Request.ServerVariables("HTTP_REFERER")PHP, Server variables$_SERVER["HTTP_HOST"];
$_SERVER["PHP_SELF"];
$_SERVER["HTTP_USER_AGENT"];
$_SERVER["REMOTE_ADDR"];
@$_SERVER["HTTP_REFERER"]; @ = ignore errors
ASP Page redirectsResponse.redirect("wrong_link.htm")PHP Page redirectsheader("Location: wrong_link.htm");
ASP, GET and POST variablesRequest.QueryString("chat")
Request.Form("username")PHP, GET and POST variables@$_GET["chat"]; @ = ignore errors
@$_POST["username"];
ASP, prevent page cachingResponse.CacheControl="no-cache"
Response.AddHeader "pragma","no-cache"PHP, prevent page cachingheader("Cache-Control: no-store, no-cache");
header("Pragma: no-cache");
ASP, Limit script execution time, in secondsServer.ScriptTimeout(240)PHP, Limit script execution time, in secondsset_time_limit(240);
ASP, Timing script executions_t=timer
...ASP script to be timed...
duration=timer-s_t
response.write duration &" seconds"PHP, Timing script execution$s_t=microtime();
...PHP script to be timed...
$duration=microtime_diff($s_t,microtime());
$duration=sprintf("%0.3f",$duration);
echo $duration." seconds";
//required function
function microtime_diff($a,$b) {
list($a_dec,$a_sec)=explode(" ",$a);
list($b_dec,$b_sec)=explode(" ",$b);
return $b_sec-$a_sec+$b_dec-$a_dec;
}
File System Functions
ASP, create a file system object (second line is wrapped)'Required for all file system functions
fileObj=Server.CreateObject
("Scripting.FileSystemObject")PHP, create a file system objectNot necessary in PHP
ASP, check if a file existspFile="data.txt"
fileObj.FileExists(Server.MapPath(pFile))PHP, check if a file exists$pFile="data.txt";
file_exists($pFile);
ASP, Read a text filepFile="data.txt"
xPage=fileObj.GetFile(Server.MapPath(pFile))
xSize=xPage.Size 'Get size of file in bytes
xPage=fileObj.OpenTextFile(Server.MapPath(pFile))
temp=xPage.Read(xSize) 'Read file
linkPage.ClosePHP, Read a text file$pFile="data.txt";
$temp=file_get_contents($pFile); //Read file
Time and Date Functions@camnpr
ASP, Server Time or DateNow, Date, TimePHP, Server Time or Datedate()
ASP, Date format (default)Now = 6/30/2012 6:14:53 AM
Date = 6/30/2012
Time = 6:14:53 AM
Various ASP functions extract date parts:
Month(Date) = 6
MonthName(Month(Date)) = June
Day(Date) = 30
WeekdayName(Weekday(Date)) = Saturday
WeekdayName(Weekday(Date),False) = SatPHP, Date formatThere is no default format in PHP.
The date() function is formatted using codes:
date("n/j/Y g:i:s A") = 6/30/2012 6:14:53 AM
date("n") = 6
date("F") = June
date("j") = 30
date("l") = Saturday
date("D") = Sat
Numeric Functions @郑州网建
ASP, convert decimal to integerInt()
n=Int(x)PHP, convert decimal to integerfloor()
$n=floor($x);
ASP, determine if a value is numericIsNumeric()
if IsNumeric(n) then ...PHP, determine if a value is numericis_numeric()
if (is_numeric($num)) {...}
ASP, modulus functionx mod yPHP, modulus function$x % $y