AS Reference  :  Notes Index  :  Resources  :  About/Contact  :  Downloads

ActionScript - PHP Lookup Table


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);
or
color_n = parseInt(color_str, 16); // hex string -> number
$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);  // string
Can 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	
Intro
Flash: What & How
Example Sites
Create
Draw, Edit Shapes
Gradients
More Drawing Tips
Import
A Sample
Animate
Frames, Keyframes
Motion Tweens
More Motion Tweens
Shape Tweens
Masks
Control
Stop/Replay
Movieclips Intro
Movieclip Reference
Site Structure 1
Slideshow Movieclip
Contact Form
Scroll Resume
Preloader
Site Structure 2
Publish
Display Options
Player Detection
Optimize
AS 2.0 Basics
Intro to Syntax
Playhead Commands
Playhead Cmds 2
Coded Tween
onEnterFrame
Intro to Classes
Declare/Assign
Comments, Trace
Simple Data Types
Arrays & Objects
Code Blocks
Operators
Beyond Buttons
Code Structure
Toggle Controls
Group of Buttons
Drag and Hit
Distort Magnifier
Scroll Text
Bee Game
Dart Shooter
Sound Control
Easing Slider
Easing Slider 2
Components Intro
Timers & Delays
Dynamic Content
Intro
Drawing API
Create Text
Attach Movieclips
Easing Slider 3
Easing Slider 4
Load jpg/swf
Sliding Viewer
Preload swf
XML
Easing Slider 5
Server Comm
LoadVars (w/ PHP)
AS - PHP Lookup
Text File
Database 1:LoadVars
Database 2:Remoting
Read from directory
AS 2.0 Classes
Intro
Math
Key
Date
Color
EventDispatcher
New Samples
Pie Chart
Event-model Emailer
Tween Sequence
Fuse Sequence
SVG in Flash
Bitmap Topo
SWF as Data Holder
Two-level Menu
Yahoo! Flash Maps
Class-based Game
ASTB Samples
Disclaimer
3D Outlines
Bounce Collide
Address Book
Save Drawings
Home  :  Notes Index  :  Resources  :  About/Contact  :  Downloads