PHP shorthand for isset()?
Update for PHP 7 (thanks shock_gone_wild)
PHP 7 introduces the null coalescing operator which simplifies the below statements to:
$var = $var ?? "default";
Before PHP 7
No, there is no special operator or special syntax for this. However, you could use the ternary operator:
$var = isset($var) ? $var : "default";
Or like this:
isset($var) ?: $var = 'default';
Shorthand isset(), if not, return default value
https://stackoverflow.com/a/18603279/165330 :
before php 7 : no
$address = isset($node->field_naam_adres['und'][0]['value']) ? $node->field_naam_adres['und'][0]['value'] : '';
from php 7 : yes
$address = $node->field_naam_adres['und'][0]['value'] ?? 'default';
Shorthand for isset and !empty
isset
and empty
are both language constructs. And empty()
internally does an isset
check first, then negates that, or alternatively also checks for values that equate FALSE
in boolean context.
So yes, !empty()
is sufficient.
Get value of variable when isset() is used in PHP ternary shorthand operator
It sounds like you are asking about the new (as of PHP 7) null coalesce operator. You can do something like:
$foo = $_GET['bar'] ?? 'hello';
echo $foo;
Which if $_GET['bar']
is null will set $foo
to hello
.
The first operand from left to right that exists and is not NULL. NULL if no values are defined and not NULL. Available as of PHP 7.
Functional demo: https://3v4l.org/0CfbE
$foo = $_GET['bar'] ?? 'hello';
echo $foo . "\n";
$_GET['bar'] = 'good bye';
$foo = $_GET['bar'] ?? 'hello';
echo $foo;
Output:
hello
good bye
Additional reading on it: http://www.lornajane.net/posts/2015/new-in-php-7-null-coalesce-operator
PHP shorthand for if (!isset($var)) $var = 1;
Yes it's called the ternary operator:
$var = isset($var) ? $var : "Some default";
Since PHP 5.3 there's a shorthand version for this as well:
$var = isset($var) ?: "Some default";
And an alternate version for PHP 5.3+:
isset($var) ?: $var = "Some default";
Shorthand check if variable isset or not and echo them to value of input field
You need to use brackets round your isset()
condition
$mycontent='<input type="text" name="myfield" class="form-control"
value="'.(isset($post['myfield']) ? $post['myfield'] : '').' "
id="myfield">';
or you can use null coalescing operator (??)...
$mycontent='<input type="text" name="myfield" class="form-control"
value="'.($post['myfield'] ?? '').' "
id="myfield">';
PHP Shorthand for isset form POST value
You can define variables in beginning of your script before HTML output, for example:
$name = isset($_POST['submit']) ? $_POST['name'] : null;
in your html section you can print $name without worrying it was not defined
<p><input type="text" name="name" value="<?php echo $name ?>" /></p>
Also if $_POST['submit'] does not contain any value you are more likely to receive FALSE statement. To avoid such issues use array_key_exists
PHP: a short cut for isset and !empty?
You can use a simple function
function post_value_or($key, $default = NULL) {
return isset($_POST[$key]) && !empty($_POST[$key]) ? $_POST[$key] : $default;
}
Then use:
$pg_title = post_value_or('pg_title');
// OR with a default
$pg_title = post_value_or('pg_title', 'No Title');
What is the PHP shorthand for: print var if var exist
For PHP >= 7.0:
As of PHP 7 you can use the null-coalesce operator:
$user = $_GET['user'] ?? 'guest';
Or in your usage:
<?= $myVar ?? '' ?>
For PHP >= 5.x:
My recommendation would be to create a issetor
function:
function issetor(&$var, $default = null) {
return isset($var) ? $var : $default;
}
This takes a variable as argument and returns it, if it exists, or a default value, if it doesn't. Now you can do:
echo issetor($myVar);
But also use it in other cases:
$user = issetor($_GET['user'], 'guest');
Simple PHP isset test
Remove the !
. You don't want to negate the expression.
$friendid = isset($_GET['friendid']) ? $_GET['friendid'] : 'empty';
Related Topics
"Call to Undefined Function SQLsrv_Connect()" When Trying to Connect to Azure Db from PHP
Get Content Within a HTML Tag Using PHP and Replace It After Processing
How to Rename a Tag in Simplexml Through a Dom Object
How to Remove <Br /> Tags and More from a String
Data Transfer from JavaScript to PHP
How to Get Files in Ftp Folder Sorted by Modification Time
How to Do Url Rewriting in PHP
PHP Sending Variables to File_Get_Contents()
Split a Time Range into Pieces by Other Time Ranges
Multiple Http Get Parameters with the Same Identifier
Using Return in Ternary Operator
Laravel Eloquent: How to Get Only Certain Columns from Joined Tables