Vous êtes sur la page 1sur 6

ASP / PHP Cross Reference

v1.1.2, 2008.02.01

This is by no means a complete list but it should help you convert ASP to PHP or the other way around. PHP has many more built in commands
than ASP (VBScript), so several lines of code in ASP may convert so a single line in PHP. If you have a large application to move from ASP to
PHP, the ASP Translator is a free web application that will save you hours of work. It converts comments, variables, if/then, loops, and many
commands from ASP to PHP, all in your web browser.

ASP (VBScript) PHP (v4.3+)

General syntax
ASP Comments, inline PHP Comments, inline
'my dog has fleas //my dog has fleas
ASP Comments, block PHP Comments, block
/*
not available?   The quick brown fox
  jumped over the lazy dogs.
*/
ASP, Escaping quotes PHP, Escaping quotes
"" \" or use ' like javascript

"var text1=""<img 'var text1="<img


src=\""blank.gif\"">"";" src=\"blank.gif\">";';
ASP Command termination PHP Command termination
None, but : can be used to separate Each command must end with ; but
commands multiple commands per line are
on the same line. allowed.
ASP Screen output PHP Screen output
response.write "hello" echo "hello";
ASP Newline characters PHP Newline characters
vbCrLf "\n" (must be inside "", not '')
response.write "hello" & vbCrLf echo "hello \n";
ASP Variable Names PHP Variable Names
Not case sensitive, Case sensitive AND must begin with
so fName is the same as FNAME $
so $fName is NOT the same as $FNAME
String Functions
ASP String concatenation PHP String concatenation
& . and .=

fname=name1 & " " & name2 $fname=$name1." ".$name2;


emsg=emsg & "error!" $emsg.="error!";
ASP, Change case PHP, Change case
LCase(), UCase() strtolower(), strtoupper()

lowerName=LCase(chatName) $lowerName=strtolower($chatName);
upperName=UCase(chatName) $upperName=strtoupper($chatName);
ASP String length PHP String length
Len() strlen()

n=Len(chatName) $n=strlen($chatName);
ASP, Trim whitespace PHP, Trim whitespace
Trim() trim() and also ltrim(), rtrim()

temp=Trim(xpage) $temp=trim($xpage);
ASP String sections PHP String sections
Left(), Right(), Mid() substr()

Left("abcdef",3) result = "abc" substr("abcdef",0,3); result =


Right("abcdef",2) result = "ef" "abc"
Mid("abcdef",3) result = "cdef" substr("abcdef",-2); result =
Mid("abcdef",2,4) result = "bcde" "ef"
substr("abcdef",2); result =
"cdef"
substr("abcdef",1,4); result =
"bcde"
ASP String search forward, reverse PHP String search forward, reverse
Instr(), InstrRev() strpos(), strrpos()
x=Instr("abcdef","de") x=4 $x=strpos("abcdef","de"); x=3
x=InstrRev("alabama","a") x=7 $x=strrpos("alabama","a"); x=6
ASP String replace PHP String replace
Replace(string exp,search,replace) str_replace(search,replace,string
exp)
temp=Replace(temp,"orange","apple")
temp=Replace(temp,"'","\'") $temp=str_replace("orange","apple",
temp=Replace(temp,"""","\""") $temp);
$temp=str_replace("'","\\'",$temp);
$temp=str_replace("\"","\\\"",
$temp);
ASP, split a string into an array PHP, split a string into an array
Split() explode()

temp="cows,horses,chickens" $temp="cows,horses,chickens";
farm=Split(temp,",",-1,1) $farm=explode(",",$temp);
x=farm(0) $x=$farm[0];
ASP, convert ASCII to String PHP, convert ASCII to String
x=Chr(65) x="A" $x=chr(65); x="A"
ASP, convert String to ASCII PHP, convert String to ASCII
x=Asc("A") x=65 $x=ord("A") x=65
Control Structures
ASP, if statements PHP, if statements
if x=100 then if ($x==100) {
x=x+5 $x=$x+5;
elseif x<200 then }
x=x+2 else if ($x<200) {
else $x=$x+2;
x=x+1 }
end if else {
$x++;
}
ASP, for loops PHP, for loops
for x=0 to 100 step 2 for ($x=0; $x<=100; $x+=2) {
if x>p then exit for if ($x>$p) {break;}
next }
ASP, while loops PHP, while loops
do while x<100 while ($x<100) {
x=x+1 $x++;
if x>p then exit do if ($x>$p) {break;}
loop }
ASP, branching PHP, branching
select case chartName switch ($chartName) {
case "TopSales" case "TopSales":
theTitle="Best Sellers" $theTitle="Best Sellers";
theClass="S" $theClass="S";
case "TopSingles" break;
theTitle="Singles Chart" case "TopSingles":
theClass="S" $theTitle="Singles Chart";
case "TopAlbums" $theClass="S";
theTitle="Album Chart" break;
theClass="A" case "TopAlbums":
case else $theTitle="Album Chart";
theTitle="Not Found" $theClass="A";
end select break;
default:
$theTitle="Not Found";
}
ASP functions PHP functions
Function myFunction(x) function myFunction($x) {
myFunction = x*16 'Return value return $x*16; //Return value
End Function }
HTTP Environment
ASP, Server variables PHP, Server variables
Request.ServerVariables("SERVER_NAME") $_SERVER["HTTP_HOST"];
Request.ServerVariables("SCRIPT_NAME") $_SERVER["PHP_SELF"];
Request.ServerVariables("HTTP_USER_AGENT $_SERVER["HTTP_USER_AGENT"];
") $_SERVER["REMOTE_ADDR"];
Request.ServerVariables("REMOTE_ADDR") @$_SERVER["HTTP_REFERER"]; @ =
Request.ServerVariables("HTTP_REFERER") ignore errors
ASP Page redirects PHP Page redirects
Response.redirect("wrong_link.htm") header("Location: wrong_link.htm");
ASP, GET and POST variables PHP, GET and POST variables
Request.QueryString("chat") @$_GET["chat"];       @ = ignore
Request.Form("username") errors
@$_POST["username"];
ASP, prevent page caching PHP, prevent page caching
Response.CacheControl="no-cache" header("Cache-Control: no-store,
Response.AddHeader "pragma","no-cache" no-cache");
header("Pragma: no-cache");
ASP, Limit script execution time, in seconds PHP, Limit script execution time, in seconds
Server.ScriptTimeout(240) set_time_limit(240);
ASP, Timing script execution PHP, Timing script execution
s_t=timer $s_t=microtime();

...ASP script to be timed... ...PHP script to be timed...

duration=timer-s_t $duration=microtime_diff($s_t,micro
response.write duration &" seconds" time());
$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) PHP, create a file system object
'Required for all file system functions Not necessary in PHP
fileObj=Server.CreateObject
 ("Scripting.FileSystemObject")
ASP, check if a file exists PHP, check if a file exists
pFile="data.txt" $pFile="data.txt";
fileObj.FileExists(Server.MapPath(pFile) file_exists($pFile);
)
ASP, Read a text file PHP, Read a text file
pFile="data.txt" $pFile="data.txt";
xPage=fileObj.GetFile(Server.MapPath(pFi $temp=file_get_contents($pFile); /
le)) /Read file
xSize=xPage.Size 'Get size of file in
bytes

xPage=fileObj.OpenTextFile(Server.MapPat
h(pFile))
temp=xPage.Read(xSize) 'Read file
linkPage.Close
Time and Date Functions
ASP, Server Time or Date PHP, Server Time or Date
Now, Date, Time date()
ASP, Date format (default) PHP, Date format
Now = 2/14/2011 9:29:07 AM There is no default format in PHP.
Date = 2/14/2011 The date() function is formatted
Time = 9:29:07 AM using codes:

Various ASP functions extract date date("n/j/Y g:i:s A") = 2/14/2011


parts: 9:29:07 AM

Month(Date) = 2 date("n") = 2
MonthName(Month(Date)) = February date("F") = February
Day(Date) = 14 date("j") = 14
WeekdayName(Weekday(Date)) = Monday date("l") = Monday
WeekdayName(Weekday(Date),False) = Mon date("D") = Mon
Numeric Functions
ASP, convert decimal to integer PHP, convert decimal to integer
Int() floor()

n=Int(x) $n=floor($x);
ASP, determine if a value is numeric PHP, determine if a value is numeric
IsNumeric() is_numeric()

if IsNumeric(n) then ... if (is_numeric($num)) {...}


ASP, modulus function PHP, modulus function
x mod y $x % $y

Vous aimerez peut-être aussi