3

I have the following code

dd(gettype($default));
$test = substr($default,0,2);

gettype returns that the type of $default is string The following error happens on the second line:

substr() expects parameter 1 to be string, array given

How is this happening, and how can I solve it?

edit var_dump also returns a string as type

edit $default contains the following string: "1015"

edit complete code block

Form::macro('time', function ($name, $default) {
$hours = value(function () use ($default){
    echo gettype($default);
    $test = substr($default,0,2);
    $hours = ['' => $test];
    for ($hour = 0; $hour < 24; $hour++) {
        $hours[$hour] = $hour;
    }
    return $hours;
});

edit code where the macro is used

{{ Form::label('reservation_starthour', 'starthour') }}
{{ Form::time('starthour',$reservation->starthour) }}

Schema of the reservation table

Schema::create('reservations', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('group_id')->unsigned();
        $table->date('startdate');
        $table->date('enddate');
        $table->string('starthour');
        $table->string('endhour');
    });
15
  • @VeeeneX It's a built in Laravel function: var_dump and die, dd. If you dd($default) without the gettype what does it return? Commented Feb 11, 2015 at 20:47
  • Do some basic trouble shooting, Var Dump all variables to see what they're worth Commented Feb 11, 2015 at 20:47
  • dd(gettype($default));var_dump($default);$test = substr($default,0,2); If var_dump will be array then it means that the problem is in dd() Commented Feb 11, 2015 at 20:48
  • @VeeeneX The problem isn't in dd(). That function doesn't modify anything. Commented Feb 11, 2015 at 20:49
  • @TimLewis This is becoming a strange question, what do you think that can be the problem? Commented Feb 11, 2015 at 20:50

2 Answers 2

1

gettype returns the type of the variable as a string result and that result might be "array", but it's still a string. So the dd / var_dump shows it as a string. Try:

echo gettype($default);
Sign up to request clarification or add additional context in comments.

1 Comment

that also returns the type as string
0

I was able to find a workaround, but not an explanation why the variable changed types.

Form::macro('time', function ($name, $default) {
$hours = ['' => substr(strval((int)$default),0,2)];
for ($hour = 0; $hour < 24; $hour++) {
    $hours[$hour] = $hour;
}

$minutes = ['' => substr(strval((int)$default),2)];
for ($minute = 0; $minute < 60; $minute += 15) {
    $minutes[$minute] = $minute;
}
return
    Form::select($name . '[hours]', $hours) .
    Form::select($name . '[minutes]', $minutes);
});

I cast the string/array to an int, get the string value, and then I could get the substring value.

I will leave this question open so someone can maybe shed some light on why the types changed.

additional information

Every function requiring a string as input, stated that $default was an array. Every function requiring an array as input, stated that $default was a string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.