New PHP programmer looking for advice on my 1st 2 functions.
Posted: Fri Sep 05, 2008 12:49 am
Hello all,
My name is Jeremy and I am an experienced programmer but not to PHP. These are my first two PHP functions and I would love some advice as to whether there are optimizations, language functionalities etc. that could improve the following two functions. They need to be PHP 4 compatible.
The first function simply extracts an array of tokens (separated by whitespace and a preceding character ~ before each token) from a file and skips over comments at the top of the file.
The second function extracts (from the array from function 1) arrays of objects who have a given number of tokens per object. The first token in the array passed to this function is the number of objects and all subsequent tokens are groupings of data objects whos number is passed as an argument.
Any help/optimizations etc. would be greatly appreciated. Thank you for your help.
Without further adieu ... here are the functions:
My name is Jeremy and I am an experienced programmer but not to PHP. These are my first two PHP functions and I would love some advice as to whether there are optimizations, language functionalities etc. that could improve the following two functions. They need to be PHP 4 compatible.
The first function simply extracts an array of tokens (separated by whitespace and a preceding character ~ before each token) from a file and skips over comments at the top of the file.
The second function extracts (from the array from function 1) arrays of objects who have a given number of tokens per object. The first token in the array passed to this function is the number of objects and all subsequent tokens are groupings of data objects whos number is passed as an argument.
Any help/optimizations etc. would be greatly appreciated. Thank you for your help.
Without further adieu ... here are the functions:
Code: Select all
function
_ExtractScriptTokens
(
$Scpt,
&$rTkns
)
{
$Hndl = fopen($Scpt, "r");
if(FALSE === $Scpt)
{
return ( FALSE );
}
do
{
$Char = fgetc($Hndl);
}
while("~" !== $Char);
$Strm = fread($Hndl, filesize($Scpt) - ftell($Hndl));
fclose($Hndl);
$rTkns = explode("~", $Strm);
return ( (FALSE === $rTkns)?FALSE:TRUE );
}
function
_ExtractFSResourceArray
(
$NObjTkns,
&$rTkns,
&$rResAry
)
{
$NObjs = current($rTkns);
if(FALSE === $NObjs)
{
return ( FALSE );
}
for($iObj = 0; $iObj < $NObjs; ++$iObj)
{
for($iElmnt = 0; $iElmnt < $NObjTkns; ++$iElmnt)
{
$Tkn = next($rTkns);
if(FALSE === $Tkn)
{
return ( FALSE );
}
$rResAry[$iObj][$iElmnt] = $Tkn;
}
}
return ( (FALSE === next($rTkns))?FALSE:TRUE );
}