Professional Documents
Culture Documents
PHP ASP JavaScript Equivalence
PHP ASP JavaScript Equivalence
This is a summary of the language differences between PHP, JScript/Javascript and VBScript that did to help my company learn the ! server side scripting languages" Hope it#s helpful to others" Feature Example (print 1 t 1! n a "e# page$ PHP
<html> <body> <?php for ($i=1; $i<=10; $i++) print "$i<br>"; ?> </body></html>
VBScript
<html> <body> <% for i = 1 to 10 Response.Write(i & "<br>") Response.Write(""+i+"<br>" next ) %> %> </body></html> </body></html>
')
&*
*)
&*
*)
f short tags are enabled in PHP"ini$ &' ') f %SP tags are enabled in PHP"ini$ &* *) &*+ return expression *) End , statement semicolon &'( is compulsory
/* This is a multi-line Comment */ # PHP comment 3
&*+return expression *)
)nd of line
' VBScript comment
Varia#les
Prefi*ed by .
+o prefi*
+o prefi* 0pti nal" )rror if variable not initialised when used" 4im %V%0 1es
/eed t / , and every variable is preinitialised 0pti nal" )rror if variable not declare to type +,--, which is e.uivalent to initialised when used" varia#les' false, the empty string and 0" var %V%0 /%V%0 1 23!' L sely %yped Varia#les 1es" This means that the type of a variable can be changed at run5time" To change variable type, use typecasting or settype& ( -ase2 1es for variables, no for reserved Sensitivity words Strings 4elimited by single5.uotes, double5 .uotes or P)0- heredoc style"
$avar = 'this is a string'; $avar = "this is a string";
1es
/ 4elimited by double5.uotes"
String 1es, variables embedded in strings are / Evaluati n evaluated if string delimited by double5.uotes" /a 1 8%8' /b 1 8/a B8' /9 now /b 11 #% B# 9/ - mm n string c nstants -ine feed$ 8:n8 ;arriage return$ 8:r8 +ote that single5.uoted #n# is not converted to a line5feed for PHP" <nly double5.uoted strings are evaluated" H%3L enc ding ,uncti ns -ine feed$ 8:n8 ;arriage return$ #:r#
(tmlspecialc(ars&/str( ;onverts = > escape&str( %ll spaces, 0e.uires %SP Server obAect 7 and 8 to their HT?- entity punctuation, accented characters, e.uivalents, eg" = becomes 7lt' and any other non5%S; &and conse.uent ;<? characters are replaced with @** encoding urlenc de& ( %ll punctuation, overhead(" accented characters, and any other non5%S; characters are replaced unescape&str( inverse of escape Server4H3%LEnc de& ( with @** encoding" Spaces converted to 6" Similar to PHP#s urldec de& (%ll punctuation, accented characters, and any other non5%S; characters are replaced with @** encoding" Spaces converted to 6" htmlspecialchars" Server456LEnc de& ( Similar to PHP#s urlencode"
,se ;<? obAect 0eg)*p" <nly available with the latest versions of VBScript &D"D or later("
7ates
$adate = time !; adate = ne* +ate !; $adate=m"time #$#$#$%$&#$'###!; adate = ne* +ate '###$%$&#! print date 'd(m()'$$adate!; ,esponse-.rite ''/adate!
4ates are formated using the 4ates are formated current locale configured in the control panel, so you have to using the current locale hand5code date formatting or change your locale" configured in the control panel, so you have to hand5code date formatting or change your locale" 8rrays 4eclare with /avar 1 array&2,#two#(' 4eclare with avar 1 new %rray&2,#two#(' 4eclare with 4 ? avar&3( Eor inde*ing use & (" %rrays begin with Hero element" ,se si9e ,&array( function to get siHe ,se array"length to get siHe of of array" array" %rrays can be iterated over using$
$val = reset $arr!; $ma4 = si5eo6 $arr!; 6or $i=#; $i<$ma4; $i//! 7 print "$val<8,>"; $val = ne4t $arr!; 9 /: or more simply :/ 6orea;h $arr as $element! print "$element<8,>";
Eor inde*ing use F G" %rrays begin with Eor inde*ing use F G" %rrays Hero element" begin with Hero element"
1es
+ot built into the language, but can simulated using the avar = ne* >rray !; 4ictionary obAect &which avar<'ne*ton'= = 'isaa;'; is not thread5safe in ,esponse-.rite avar<'ne*to VBScript D"J(" n'=!
+ote$ Iou can append new elements to arrays with /avarFG 1 #string to add#' %rue and False Has constants true and false" The following also evaluate to false$
# /: 5ero :/ "" /: empty string :/ "#" /: string *ith 5ero :/
Has constants true and false" )mpty strings and Hero will evaluate to false"
Has constants true and false" ,nliKe PHP and JScript, Hero does not evaluate to false"
E:uality
== tr?e i6 e@?al A= not e@?al === tr?e i6 e@?al B same type A== not identi;al
== tr?e i6 e@?al A= not e@?al === tr?e i6 e@?al B same type A== not identi;al
+ote that some functions can return J JScript does not have the J/false or false &false is typically used to problem" indicate failure( depending on the conte*t" Eor these functions, you will need to use 111 to determine whether J or false was returned" Some problematic functions$ strpos$ strrpos$ strstr$ stristr$ strr;hr 8ssignment %llows ; style shortcut assignments" %llows ; style shortcut statements Eor e*ample, to append a string to a assignments" Eor e*ample, to variable$ append a string to a variable$ /avar "1 #append to end#' Sending H%3L t Br "ser print /avar' echo 8The avar variable #/avar# is a string8'
9 else $avar -= 'end';
+o shortcut assignments"
i6 len avar! = # then avar = "ab;" else avar = avar B "end" endi6 *hile a > # a = a ( % *end 6or i=% to %## ,esponse-.rite i! ne4t sele;t ;ase a1olor ;ase "red" do% ;ase "green" do' ;ase else do& end sele;t
*hile
a > #! a (= %;
F r l ps
6or i=#$m=C; i<m; i//! 7 ,esponse-.rite i!; 9 s*it;h a1olor! 7 ;ase 'red'D do% brea" ;ase 'green'D do' brea" de6a?ltD do&; brea"; 9
Part >: PHP, JScript and VBScript Functi n and -lass Syntax
PHP Functi n Example JScript and Javascript
6?n;tion in; $val! 7 ret?rn $val / %;9
VBScript
6?n;tion in; val! 7 ret?rn val / %;9 6?n;tion in; val! in; = val / %
end 6?n;tion
Procedures are functions that return Procedures are functions no value" that return no value"
' pro;ed?re s?b in;' byre6 val! val = val / % end s?b
-lass Example
;lass parent 7 var property; 6?n;tion parent ! 7 9 6?n;tion method ! 7 9 9 /: inheritan;e :/ ;lass ;hild e4tends parent 7 var property='ne* val?e'; 9
+ot available
Sc pe , Varia#les
Variables defined outside a function Similar to PHP, e*cept no or class are available globally' need to declare global variables defined in a class or variables in functions" function are available locally" ,nliKe other languages, you must declare a global variable in a function or class before you use the variable, using the Keyword gl #al"
$globalvar = %; 6?n;tion sho*Eglobal 7 global $globalvar; print $globalvar; 9 !
Similar to PHP, e*cept no need to declare global variables in subroutines nor functions"
-all #y 6e,erence
6eturning By default, return is by value" Eor +ot an issue #y 6e,erence obAects and arrays, we want to return by reference for superior performance" ,se
6?n;tion getarray%'& ! 7 $val = array %$'$&!; ret?rn B $val; 9
+ot an issue
-lasses
nheritance and constructors supported" Parent constructor not called automatically during obAect creation, only derived constructor" +o multiple5inheritance" class child e*tends parent true
;lasses and constructors Primitive form of classes in supported" +o inheritance" newer versions of VBScript" ;onstructors and destructors available" nheritance not supported" See ,sing ;lasses in %SP ,se try and catch"
try 7 6?n;tionE;anE6ail ! 9 ;at;h err! 7 ,esponse-.rite err! 9
Err r Handling
The scope of L is the current statement" The last error can be checKed in .p(p?err rmsg if trac@?err rs+0n is set in PHP"ini"
The scope of this statement is the current procedure" ,se the global )rr"number and )rr"description to find out what was the last error number and description"" +ewer versions of VBScript support try and catch"
PHP E:uivalent
print $str; e;ho $str; printEr $deb?gEstr;
These variables are available automatically as global variables if you have configured the following in PHP"ini$
variablesEorder="EGH1S" registerEglobals=On
Eor security, would recommend disabling registerBglobals &set it to <EE(" Then the variables are only available in the arrays$
$I33HEHOS3EJ>,S$ $I33HE1OOKLEEJ>,S and $I33HEGE3EJ>,S-
See e*amples below" 6edirecting t an t(er l cati n - @ie Handling 8pplicati n Varia#les Sessi n Varia#les
,esponse-,edire;t ?rl!
Header&8-ocation$ /url8('
,esponse-1oo"ies ;oo"iename! = set;oo"ie $;oo"iename$ $ne*val!; ne*val $avar = $I33HE1OOKLEEJ>,S<$;oo"iename=; avar = ,e@?est-1oo"ies ;oo"iename!
%pplication&appvarname(
Session sessionname! = ne*val avar = Session sessionname!
+ot available" ;an be simulated with a database" n PHPM or later, we marK certain variables as session variables using sessionBregister&/sessionname(, and we call sessionBstart& ( at the beginning of the "php page to restore the session variable values" +ote that we pass the name of the variable, and not the variable itself into sessionBregister& ("
sessionEregister 'avar'!; $avar = CC; sessionEstart !; / !! in $a"ar is o"er#ritten no# print $avar;
F rm Varia#les
0e.uest"Eorm&8formvar8( 0e.uest"NueryString&8getvar8(
/HTTPBP<STBV%0SF8formvar8G' /HTTPBO)TBV%0SF8getvar8G' %lternately, O)T and P<ST 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 Varia#les
There are many server variables" See %SP Eor S%P modules, the server varibles are stored documentation" %n e*ample$ in the /HTTPBS)0V)0BV%0S array" Eor ;O , they are stored as environment variables, 0e.uest"ServerVariables&8HTTPBH<ST8( available from the /HTTPB)+VBV%0S array or getenv& (" %n e*ample$ /HTTPBS)0V)0BV%0SF8HTTPBH<ST8G using S%P module /HTTPB)+VBV%0SF8HTTPBH<ST8G using ;O module See http$//php"weblogs"com/%pacheB S for a more detailed discussion of ;O vs" S%P "
7ata#ase 8ccess
?icrosoft#s %4< technology is commonly %4< can be simulated using the %4<4B used" database library" This PHP library emulates %4<"
-imitations$ only supports forward scrolling read5only cursors" Bu,,ering Script %ime ut
,esponse-8?66er = tr?e ,esponse-.rite "ab;"!; ,esponse-2l?sh ! obEstart !; print "ab;"; obEendE6l?sh !;