Question:
What is the syntax and format for a PHP user-function call?
H M M M
2009-12-31 12:32:23 UTC
I am new to using functions in PHP. I found (what appears to be) a good solution to a problem, using a user-defined function. The issue is, I do not know how to call it.
The function (http://www.jonasjohn.de/snippets/php/split-by-length.htm) is:
/**
* Splits the given string into chunks with the given length.
*
* @param string $string Input string
* @param string $chunkLength Chunk length
* @return array Splitted string
*/
function SplitByLength($string, $chunkLength=1){

$Result = array();
$Remainder = strlen($string) % $chunkLength;

$cycles = ((strlen($string) - $Remainder) / $chunkLength) + (($Remainder != 0) ? 1 : 0);

for ($x=0; $x < $cycles; $x++)
$Result[$x] = substr($string, ($x * $chunkLength), $chunkLength);

return $Result;
}

I tried:
call_user_func('SplitByLength($sub, $chunkLength=7)');

(($sub is the string I'm trying to cut into pieces by length!)

and

call_user_func('SplitByLength');

Neither work. If someone can explain what needs to be included in this particular function call, and more importantly WHY it needs to be called, that would be great.
Three answers:
kimcord_99
2009-12-31 12:41:47 UTC
How about



$chunkLength=7;



call_user_func('SplitByLength', $sub,$chunkLength );



or





$chunkLength=7;



$result = SplitByLength($sub,$chunkLength);

echo $result;
2009-12-31 12:41:09 UTC
Unless you have a specific need for it's special features,

call_user_func is inappropriate for simple standard function usage.



I suggest you do a tutorial - http://www.tizag.com/phpT/phpfunctions.php .

In 5 minutes you will understand how to define and use a function better than I could explain it.
?
2016-10-06 06:47:23 UTC
did you used


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...