Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php recognizing string as array

Tags:

php

laravel-4

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');
    });
like image 876
JorenV Avatar asked May 14 '26 03:05

JorenV


1 Answers

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);
like image 61
AbraCadaver Avatar answered May 15 '26 16:05

AbraCadaver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!