Search code examples
phpvariablesif-statementasp-classicisset

Can I assign a value to a var from another which I don't know if exist in one row?


I'm looking for a better and easy way to do this. I remember I saw a shorthand somewhere.

if (isset($user['passport'])) $passport=$user['passport'];

Edited to let you know what I mean

Classic ASP:

user=request.form("user")
response.write user

ASP doesn't care if user exists, so prints nothing

The same in PHP

$user=$_POST['user'];
echo $user;

PHP prints Notice: Undefined variable


Solution

  • If you need shorthand condition than you can use ternary operator as:

    $passport = (isset($user['passport']) ? $user['passport'] : '');