What is the PHP shorthand for: print var if var exist -
we've encountered before, needing print variable in input field not knowing sure whether var set, this. avoid e_warning.
<input value='<?php if(isset($var)){print($var);}; ?>'>
how can write shorter? i'm okay introducing new function this:
<input value='<?php printvar('myvar'); ?>'>
but don't succeed in writing printvar() function.
my recommendation create issetor
function:
function issetor(&$var, $default = false) { return isset($var) ? $var : $default; }
this takes variable argument , returns it, if exists, or default value, if doesn't. can do:
echo issetor($myvar);
but use in other cases:
$user = issetor($_get['user'], 'guest');
as of php 7 can use null-coalesce operator:
$user = $_get['user'] ?? 'guest';
or in usage:
<?= $myvar ?? '' ?>
Comments
Post a Comment