Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Bloomanity's avatar

Check if a variable exists

Is there a way to check if a variable is defined? Given I have the string "video" ... I want to know if a variable with that name exists.

0 likes
3 replies
Bloomanity's avatar

@freekmurze isset won't check to see if there is a variable set give a string you pass to it, rather you need to pass it an actual variable

>>> isset('video');
PHP Parse error: Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING on line 1
>>> $video = 'test';
=> "test"
>>> isset($video);
=> true
>>> isset($video1);
=> false

I need something like

<input type="text" name="title" value="<?= oldInputOrExistingValue('title', isset($video) ? $video : null) =>">
function oldInputOrExistingValue($field, $model) {
    return old($field, isset($model) ? $model->{$field} : null);
}

but where I pass the $model as a string in the view and not check again to see if I have the $video variable

Please or to participate in this conversation.