Speaking of PHP, anyone who codes in more than one language (and who doesn't?) knows that dredging up the right syntax for the less-used language from the depths of one's brain (or scanning the manual for the right function) can be painfully slow. To speed things up a bit, I made this table with actionscript (MX) on the left and its PHP (4) equivalent on the right:
| Actionscript | PHP |
| CONVERSIONS | |
| Get numeric value of string | |
rank_n = Number(rank_str); |
$nRank = intval($sRank); |
| Convert number to string | |
| Automatic conversion if used with string operator | $sRank = strval($nRank); |
| STRING OPERATIONS | |
| Get 3rd to 5th characters in address string | |
s_str = address_str.substr(2, 3); |
$s = substr($sAddress, 2, 3); |
| Find numeric position of start of string s within a big string | |
pos_n = big_str.indexOf(s); |
$n = strpos($sBigstr, $s); |
| Get length of string (ie, number of characters) | |
len_n = big_str.length; |
$n = strlen($sBigstr); |
| Concatenate strings | |
new_str = addr_str + zip_str; |
$sNew = $sAddr.$sZip; |
| Make string lowercase | |
lower_str = word_str.toLowerCase(); |
$sLower = strtolower($sWord); |
| Replace piece of string | |
addr_str = "402 My Street";
addr_str =
addr_str.split("My").join("18th"); |
// str_replace(find, repl, orig)
$s = "402 My Street";
$s = str_replace("My","18th",$s);
|
| ARRAY OPERATIONS | |
| Create 3 element array with string values | |
elem_arr = ["Pb","Au","Be"]; |
$aElem = array("Pb","Au","Be"); |
| Show 2nd element of array in textfield (Flash) or browser window (PHP) | |
my_txt.text = materials_arr[1]; |
echo $aMaterials[1]; |
| Get length of array | |
len_n = materials_arr.length; |
$n = count($aMaterials); |
| Create a two-dimensional PHP array with string indices, and an equivalent actionscript Array-of-objects (because Array objects in AS can only have numeric indices) and display the contents (to a browser for PHP, to the Output window for AS). Notice also that a PHP foreach loop goes through array in ascending order; an Actionscript for/in loop goes through the array in a technically undefined (actually, in all versions so far, reverse) order. | |
var pNames_arr = [
{category:'Trees',
kinds_arr: [
{name:'Maple',genus:'Acer'},
{name:'Oak',genus:'Quercus'},
{name:'Pine',genus:'Pinus'}] },
{category:'Flowers',
kinds_arr: [
{name:'Pansy',
genus:'Viola'},
{name:'Geranium',
genus:'Pelargonium'},
{name:'Daylily',
genus:'Hemerocallis'} ] }
];
for (var i in pNames_arr) {
trace(pNames_arr[i].category);
var kinds = pNames_arr[i].kinds_arr;
for (var j in kinds) {
trace(" " + kinds[j].name + "=>" +
kinds[j].genus);
}
}
|
$aNames = array(
'Trees'=>array(
'Maple'=> 'Acer',
'Oak'=> 'Quercus',
'Pine'=> 'Pinus'),
'Flowers'=>array(
'Pansy'=> 'Viola',
'Geranium'=> 'Pelargonium',
'Daylily'=> 'Hemerocallis')
);
foreach($aNames as $key => $value){
echo "$key<br>";
foreach($value as $key2 => $val2){
echo " $key2 => $val2<br>";
}
} |
| MATH OPERATIONS | |
| Get a random number between 1 and 5, inclusive: | |
r_n = 1 + Math.floor(Math.random()*5); |
$nRandom = rand(1, 5); |
| MISCELLANEOUS | |
| Double quotes vs single quotes | |
| No difference (use \" to include a double-quote within a double-quoted string; same for single quotes) | Double quotes cause inside to be parsed (and
escape sequences like \t for tab to be interpreted):
$s = "you"; echo "this is $s"; // this is you echo 'this is $s'; // this is $s |
| Scope of variables in a function | |
Can reference, from within a
function, variables defined outside the function if on the same timeline,
eg
this_str = "created outside";
function p() {
trace(this_str);
}
p(); // created outside |
Function doesn't know about variables outside
it, eg
$sThis = "created outside";
function p() {
echo $sThis;
}
p(); // <undefined>
function p2($s) {
echo $s;
}
p2($sThis); // created outside
|
| Find variable type | |
Use the typeof operator. Possible
return values are: String:"string", Movie clip:"movieclip", Button:"object",
Text field:"object", Number:"number", Boolean:"boolean", Object:"object",
Function:"function".
sal_str = "Dear "; trace(typeof sal_str); // stringCan also use instanceof. From the MX2004 Help: If object is an instance of class, instanceof returns true; otherwise, instanceof returns false. Note: class name is case sensitive. // true if _mc exists on stage: trace(_mc instanceof MovieClip); |
Use the gettype function. Possible return
values are (from the manual): "boolean" (since PHP 4), "integer", "double"
(for historical reasons "double" is returned in case of a float, and not
simply "float"), "string", "array", "object", "resource" (since PHP 4),
"NULL" (since PHP 4), "user function" (PHP 3 only, deprecated), "unknown
type".
$sSal = "Dear "; echo gettype($sSal); // string |
Discussed on this page:
lookup for actionscript php equivalents, conversion, array, string, type, syntax