Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rand() function cause error when I replace static value with random value

Tags:

php

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 = '0123456789' )

but still get an error?

like image 732
Gerald Ferreira Avatar asked Aug 04 '26 18:08

Gerald Ferreira


1 Answers

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);
    }
    // ...
}
like image 76
rid Avatar answered Aug 06 '26 08:08

rid