PHP 的简写是什么:如果 var 存在则打印 var

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5836515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:38:00  来源:igfitidea点击:

What is the PHP shorthand for: print var if var exist

phpvarisset

提问by bart

We've all encountered it before, needing to print a variable in an input field but not knowing for sure whether the var is set, like this. Basically this is to avoid an e_warning.

我们以前都遇到过,需要在输入字段中打印一个变量,但不确定是否设置了该变量,就像这样。基本上这是为了避免电子警告。

<input value='<?php if(isset($var)){print($var);}; ?>'>

How can I write this shorter? I'm okay introducing a new function like this:

我怎样才能写得更短?我可以引入这样的新功能:

<input value='<?php printvar('myvar'); ?>'>

But I don't succeed in writing the printvar() function.

但是我没有成功编写 printvar() 函数。

回答by NikiC

For PHP >= 5.x:

对于 PHP >= 5.x:

My recommendation would be to create a issetorfunction:

我的建议是创建一个issetor函数:

function issetor(&$var, $default = false) {
    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');

For PHP >= 7.0:

对于 PHP >= 7.0:

As of PHP 7 you can use the null-coalesce operator:

从 PHP 7 开始,您可以使用空合并运算符

$user = $_GET['user'] ?? 'guest';

Or in your usage:

或者在您的使用中:

<?= $myVar ?? '' ?>

回答by Galen

Another option:

另外一个选项:

<input value="<?php echo isset($var) ? $var : '' ?>">

回答by arychj

<input value='<?php @print($var); ?>'>

回答by McAngujo

The shortest answer I can come up with is <?php isset($var) AND print($var); ?>

我能想到的最短答案是 <?php isset($var) AND print($var); ?>

Further details are here on php manual.

进一步的细节在这里 php 手册

A simple alternative to an if statement, which is almost like a ternary operator, is the use of AND. Consider the following:

if 语句的一个简单替代方法(几乎类似于三元运算符)是使用 AND。考虑以下:

'; // 这是一个替代的 isset( $value ) AND print( $value ); ?>

This does not work with echo() for some reason. I find this extremely useful!

由于某种原因,这不适用于 echo() 。我觉得这非常有用!

回答by Jani

This worked for me:

这对我有用:

<?= isset($my_variable) ? $my_variable : $my_variable="Some Value"; ?>

回答by Dan Breen

You could do <?php echo @$var; ?>.Or <?= @$var ?>.

你可以做<?php echo @$var; ?>.Or <?= @$var ?>

回答by ThiefMaster

Better use a proper template engine - https://stackoverflow.com/q/3694801/298479mentions two nice ones.

更好地使用适当的模板引擎 - https://stackoverflow.com/q/3694801/298479提到了两个不错的。

Here's your function anyway - it will only work if the var exists in the global scope:

无论如何,这是您的函数 - 只有当 var 存在于全局范围内时,它才会起作用:

function printvar($name) {
    if(isset($GLOBALS[$name])) echo $GLOBALS[$name];
}

回答by mfonda

There's currently nothing in PHP that can do this, and you can't really write a function in PHP to do it either. You could do the following to achieve your goal, but it also has the side effect of defining the variable if it doesn't exist:

目前 PHP 中没有任何东西可以做到这一点,而且您也无法真正在 PHP 中编写函数来做到这一点。您可以执行以下操作来实现您的目标,但如果变量不存在,它也会产生定义变量的副作用:

function printvar(&$variable)
{
    if (isset($variable)) {
        echo $variable;
    }
}

This will allow you to do printvar($foo)or printvar($array['foo']['bar']). However, the best way to do it IMO is to use issetevery time. I know it's annoying but there's not any good ways around it. Using @isn't recommended.

这将允许您执行printvar($foo)printvar($array['foo']['bar'])。但是,IMO 最好的方法是isset每次都使用。我知道这很烦人,但没有任何好方法可以解决。使用@不推荐使用。

For more information, see https://wiki.php.net/rfc/ifsetor.

有关更多信息,请参阅https://wiki.php.net/rfc/ifsetor

回答by mellamokb

<input value='<?php printvar(&$myvar); ?>' />

function printvar(&$val) {
    if (isset($val)) echo $val;
}

回答by Waseem Akhter

You could also use the following:

您还可以使用以下内容:

<input type="text" value="<?php echo @$var; ?>">//means if(isset($var)){ echo $var; }