What am I doing wrong
I have this script, and added the $randnumber = rand(100, 500);
function to it, this should generate a random number for me between 100 and 500.
$randnumber = rand(100, 500);
function word_limiter( $text, $limit = $randnumber, $chars = '0123456789' )
The problem is that it gives me a error:
Parse error: syntax error, unexpected T_VARIABLE
While if I use the function as:
function word_limiter( $text, $limit = '200', $chars = '0123456789' )
it works 100%, I have tried it like this:
function word_limiter( $text, $limit = ''.$randnumber.'', $chars = '012345开发者_如何学Python6789' )
but still get an error?
That is a syntax error. You cannot assign the value of an expression as a default value. Default values can only be constants. Instead of doing that, you could be doing something like:
function word_limiter ($text, $limit = null, $chars = '0123456789') {
if ($limit === null) {
$limit = rand(100, 500);
}
// ...
}
What you are doing wrong is trying to use a variable as a default parameter value. You cannot do this.
You could do it like this:
function word_limiter( $text, $limit = null, $chars = '0123456789' ){
if (is_null($limit)){
$limit = rand(100, 500);
}
}
You can not use a variable as the default to an argument - it must be a constant value.
You could try this...
function word_limiter($text, $limit = NULL) {
if ($limit === NULL) {
// Make its default value.
}
}
精彩评论