user3386779
user3386779

Reputation: 7175

Check if an array value is set or not

I'm fetching practice_string_id and program_string_id from a table

$project_type = DB::table('project')
              ->where('code',$asset_request->project_code)
              ->select('practice_string_id','program_string_id')
              ->first();
print_r($project_type); //output 

Output:

stdClass Object ( [practice_string_id] => PRACTICE0028 [program_string_id] => )

I want to check $project_type->program is set or not in if condition

if(isset($project_type->program_string_id)){
    //nothing in $project_type->program but reached here now 
}

I want to how to check the value is set or not in php if condition.now if(isset($project_type->program_string_id)) is passed and if condition is working.I want to skip if condition.

Upvotes: 0

Views: 68

Answers (3)

Narinder Bajwa
Narinder Bajwa

Reputation: 54

$project_type = DB::table('project')
              ->where('code',$asset_request->project_code)
              ->select('practice_string_id','program_string_id')
              ->first();
if(count($project_type) > 0 && $project_type != ""){

    echo $project_type->program_string_id;
}

Upvotes: 0

FULL STACK DEV
FULL STACK DEV

Reputation: 15951

if(isset($project_type-> program_string_id) && !empty($project_type-> program_string_id)  ) 
}

You have to use both isset() and empty() other wise it may throw error in some cases

Upvotes: 0

eisbehr
eisbehr

Reputation: 12452

As this is a database query, and you select the field program_string_id, it will be set always and any time. The question is, if there is any value. So you might want to use empty as check:

if (!empty($project_type->program_string_id)) {
    // ...
}

Upvotes: 1

Related Questions