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
If you need shorthand condition than you can use ternary operator
as:
$passport = (isset($user['passport']) ? $user['passport'] : '');
$passport=$user['passport']?$user['passport']:"Value if not set";
Try this :
$passport = ($user['passport'])?: '';
You can use PHP ternary operator for this purpose
Link to learn about ternary operator http://php.net/manual/en/language.operators.comparison.php
Sample php code
<?php
// Example usage for: Ternary Operator
$passport = (isset($user['passport'])) ? $user['passport'] : '';
// The above is identical to this if/else statement
if (isset($user['passport'])) {
$passport = $user['passport'];
} else {
$passport = '';
}
?>
Classic ASP 'may not care', but don't be fooled by apparently none existent values. Sometimes they do contain something, e.g. Nothing
. This all depends, of course, on the original source of the value.
A quick and dirty method that I tend to use to force variants of a particular type is to append an empty string to the beginning, e.g.:
user = "" & Request.Form("user")
'As a numeric example from a different source...
count = CInt("0" & rs("record_count"))
For PHP you could consider:
$passport = "" . $user[passport];
(I don't know a great deal about PHP, unfortunately.)