ASP/PHP/Javascript/SQL Cross Reference

ASP/PHP/Javascript/SQL Cross Reference

Python/Applescript cross reference.

Command ASP3 (VBScript 5.6) PHP Javascript (ECMAscript) MS SQL (2000+)
General syntax
Comments, inline 'my dog has fleas //my dog has fleas // comment -- comment
Comments, block Not available
Small workaround:

If 2 == 1 Then
.. do stuff
End If

("do stuff" must contain valid code.)
/*
  The quick brown fox
  jumped over the lazy dogs.
*/
/* lines of code */ /* lines of code */
Command termination None ; ; None
Quotes " " or ' ' '
Escaping quotes ""
"var text1=""<img src=\""blank.gif\"">"";"
\" or use ' to quote and just " to write \' ''
Multiple commands per line Separate with : Allowed Allowed ?
Screen output Response.Write("hello") echo "hello"; document.write('hello'); PRINT 'hello'
Newline characters vbCrLf, outside quotes Response.Write("hello" & vbCrLf) "\n", inside quotes (must be inside "", not '') echo "hello \n"; ? ?
Equality test 2 = 3 2 = 2, 2 <> 3 2 == 32 = 2, 2 != 3 2 == 3 2 = 3
Case sensitivity Everything is case insensitive Variables case sensitive, functions and commands insensitive Everything is case sensitive Everything is case insensitive
Variable names Case insensitive,
so strName is the same as STRNAME
Case sensitive AND must begin with $
so $fName is NOT the same as $FNAME
Case sensitive Case insensitive AND must begin with @
Usual naming convention variables Hungarian notation strName, intZipCode ? Camel case thisIsVariable Pascal Casing @Name
Usual naming convention functions Pascal Casing MyFunction ? Camel case myFunction Pascal Casing MyFunction
Unsorted (Class, session)
Class delimiter . -> ? ?
Function kald Call Test(var1, var2)
Function(var1, var2)
'Alle funktionens parametre skal stå i kaldet
? ? ?
Function kald fra samme class Call Test() $this->test ? ?
Function kald fra anden class Call Regular.Test() $regular->test ? ?
Session Session("name") $_SESSION["name"] ?
Other
Include outside code <!--#include virtual="/filename.inc"-->
<!--#include file="/filename.inc"-->
<script language="vbscript" runat="server" src="/filename.inc"<>/script>
? document.write("<SCR" + "IPT language=\"Javascript\" src=\"filename.js\"><SCRIPT>"); ?
Execute inside code Server.Execute("filename.inc")
Server.Transfer("filename.inc")
require, require_once, include, include_once (include will only include the file if the line it's in actually executes, and will continue on errors) ? ?
Control Structures
If statements
if x=100 then
x=x+5 
elseif x<200 then 
x=x+2 
else 
x=x+1 
end if
If (1 = 1) Or (2 = 2) Then
If (1 = 1) And (2 = 2) Then
If (1 = 2) And (function()) Then (function bliver eksekveret, selvom det er overflødigt, da sætningen aldrig bliver sand)
if ($x==100) { 
$x=$x+5; 
} 
else if ($x<200) { 
$x=$x+2; 
} 
else { 
$x++; 
}
if (1 == 1 || 2 == 2)
if (1 == 1 && 2 == 2)
if (1 == 2 && function()) (function bliver ikke eksekveret, da det er overflødigt, da sætningen aldrig bliver sand)
For loops
for x=0 to 100 step 2 
if x>p then exit for
next
for ($x=0; $x<=100; $x+=2) { 
if ($x>$p) {break;}
}
While loops
do while x<100 
x=x+1 
if x>p then exit do
loop
while ($x<100) { 
$x++; 
if ($x>$p) {break;}
}
Branching
select 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 select
switch ($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";
}
Functions and procedures
Function myFunction(x)
myFunction = x*16  'Return value
End Function
function myFunction($x) {
return $x*16;  //Return value
}
String Functions
ASP String concatenation
&

fname=name1 & " " & name2
emsg=emsg & "error!"
PHP String concatenation
. and .=

$fname=$name1." ".$name2;
$emsg.="error!";
ASP, Change case
LCase(), UCase()

lowerName=LCase(chatName)
upperName=UCase(chatName)
PHP, Change case
strtolower(), strtoupper()

$lowerName=strtolower($chatName);
$upperName=strtoupper($chatName);
ASP String length
Len()

n=Len(chatName)
PHP String length
strlen()

$n=strlen($chatName);
ASP, Trim whitespace
Trim()

temp=Trim(xpage)
PHP, Trim whitespace
trim() and also ltrim(), rtrim()

$temp=trim($xpage);
ASP String sections
Left(), 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 sections
substr()
	
	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, reverse
Instr(), InstrRev()
	
	x=Instr("abcdef","de")        x=4 
	x=InstrRev("alabama","a")     x=7
PHP String search forward, reverse
strpos(), strrpos()
	
	$x=strpos("abcdef","de");      x=3
	$x=strrpos("alabama","a");     x=6
ASP String replace
Replace(string exp,search,replace)

temp=Replace(temp,"orange","apple")
temp=Replace(temp,"'","\'")
temp=Replace(temp,"""","\""")
PHP String replace
str_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 array
Split()
	
	temp="cows,horses,chickens"
	farm=Split(temp,",",-1,1)  
	x=farm(0)
PHP, split a string into an array
explode()
	
	$temp="cows,horses,chickens";
	$farm=explode(",",$temp);
	$x=$farm[0];
ASP, convert ASCII to String
x=Chr(65) x="A"
PHP, convert ASCII to String
$x=chr(65); x="A"
ASP, convert String to ASCII
x=Asc("A") x=65
PHP, convert String to ASCII
$x=ord("A") x=65
HTTP Environment
GET, POST and other variables:
Request.QueryString("name")
Request.Form("name")
Request.Cookies("name")
Other variables:
$_GET["name"]
@$_GET["name"]     (@ = ignore errors)
$_POST["name"]
$_COOKIE["name"]
$_SERVER
$_FILES
$_ENV
$_REQUEST
$_SESSION
ASP, Server variables
Request.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 redirects
Response.redirect("wrong_link.htm")
PHP Page redirects
header("Location: wrong_link.htm");
ASP, prevent page caching
Response.CacheControl="no-cache"
Response.AddHeader "pragma","no-cache"
PHP, prevent page caching
header("Cache-Control: no-store, no-cache");
header("Pragma: no-cache");
ASP, Limit script execution time, in seconds
Server.ScriptTimeout(240)
PHP, Limit script execution time, in seconds
set_time_limit(240);
ASP, Timing script execution
s_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 object
Not necessary in PHP
ASP, check if a file exists
pFile="data.txt"
fileObj.FileExists(Server.MapPath(pFile))
PHP, check if a file exists
$pFile="data.txt";
file_exists($pFile);
ASP, Read a text file
pFile="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.Close
PHP, Read a text file
$pFile="data.txt";
	$temp=file_get_contents($pFile);  //Read file
Time and Date Functions
ASP, Server Time or Date
Now, Date, Time
PHP, Server Time or Date
date()
ASP, Date format (default)
Now = 6/29/2006 9:24:44 AM
Date = 6/29/2006
Time = 9:24:44 AM

Various ASP functions extract date parts:

Month(Date) = 6
MonthName(Month(Date)) = June
Day(Date) = 29
WeekdayName(Weekday(Date)) = Thursday
WeekdayName(Weekday(Date),False) = Thu
PHP, Date format
There is no default format in PHP.
The date() function is formatted using codes:

date("n/j/Y g:i:s A") = 6/29/2006 9:24:44 AM

date("n") = 6
date("F") = June
date("j") = 29
date("l") = Thursday
date("D") = Thu
Numeric Functions
ASP, convert decimal to integer
Int()

n=Int(x)
PHP, convert decimal to integer
floor()

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

if IsNumeric(n) then ...
PHP, determine if a value is numeric
is_numeric()

if (is_numeric($num)) {...}
ASP, modulus function
x mod y
PHP, modulus function
$x % $y

PHP 4 to PHP 5 differences

PHP 5's major new achievements come in the area of its exception handling and a new object that introduces features that bring true OOP to PHP. Exception handling was certainly one of the most noticeable missing features in PHP 4, and its addition to PHP 5 is certainly a sign of maturity. Exception handling means you have language defined and standardized ways of handling errors in your software. Just use the try, catch, and throw methods, and your PHP code becomes more robust and clean.

<?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 number of positive impacts on programs written in PHP. In PHP 4, when an object was passed to a function or method, it was passed by value, unless you explicitly told PHP otherwise. This procedure meant that a copy of that object, all the data structures in memory, would have to be copied. This step used memory and made access slow and clunky. In PHP 5, however, objects are always passed by reference.

The new object-oriented features in PHP 5, including constructors and destructors, are noteworthy. As with C++ and Java, they provide a standard way to create the object, allocate memory, and do any necessary setup via a constructor method and perform cleanup with a destructor method.

PHP 5 also introduces more subtle control of methods and variables in your classes. In PHP 4, everything was public: You could access variables from your classes outside the class or in derived classes. In PHP 5, you can still make variables or methods public, but you can also make them private, so they're used only within the class itself. A third option is to make them protected, which means that methods and variables can be viewed within the class or when subclassed.

Furthermore, PHP 5 introduces type hinting, or better type checking. When you pass an object into a routine, PHP can check that it is the right type and give a type-mismatch error if the check fails.

Additional features such as static methods and variables and abstract classes exist, so be sure to check the documentation for details.