PHP 条件运算符和自赋值

发布于 2024-08-01 21:45:40 字数 178 浏览 1 评论 0原文

这种事情在 PHP 中被认为是可以的吗?

$foo = $_GET['foo'];
$foo = empty($foo) || !custom_is_valid($foo) ? 'default' : $foo;

有更清洁的替代品吗? 我基本上试图避免额外的表查找。

Is this sort of thing considered OK in PHP?

$foo = $_GET['foo'];
$foo = empty($foo) || !custom_is_valid($foo) ? 'default' : $foo;

Are there cleaner alternatives to this? I'm basically trying to avoid extra table look-ups.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月寒剑心 2024-08-08 21:45:40

怎么样:

$foo = 'default';
if (array_key_exists('foo', $_GET) and custom_is_valid($_GET['foo'])) {
    $foo = $_GET['foo'];
}

不要害怕数组查找,它们并没有那么慢:)

How about:

$foo = 'default';
if (array_key_exists('foo', $_GET) and custom_is_valid($_GET['foo'])) {
    $foo = $_GET['foo'];
}

And don't be afraid of the array lookups, they are not that slow :)

过期以后 2024-08-08 21:45:40

也许不只是检查它是否有效,而是通过采用默认值的清理函数来运行它。

另外,我喜欢使用以下函数,这样在运行 E_STRICT 时就不会收到有关访问不存在的数组键的警告:

function GetVar($var, $default = '') {
  $value = $default;
  if(isset($_GET[$var])) {
    $value = $_GET[$var];
  }
  return $value;
}

function custom_clean($value, $default = '') {
  ... validation logic or return $default ...
}

$foo = custom_clean(GetVar('foo'), 'default');

Perhaps instead of just checking if it is valid, run it though a cleaning function that takes a default.

Also, I like to use the following function so I don't get warnings on accessing non-existant array keys when running E_STRICT:

function GetVar($var, $default = '') {
  $value = $default;
  if(isset($_GET[$var])) {
    $value = $_GET[$var];
  }
  return $value;
}

function custom_clean($value, $default = '') {
  ... validation logic or return $default ...
}

$foo = custom_clean(GetVar('foo'), 'default');
谜兔 2024-08-08 21:45:40

在这里上课会让你的生活变得更加轻松。

<?php

class ParamHelper
{
  protected $source;

  public function __construct( array $source )
  {
    $this->source = $source;
  }

  public function get( $key, $default=null, $validationCallback=null )
  {
    if ( isset( $this->source[$key] ) && !empty( $this->source[$key] ) )
    {
      if ( is_null( $validationCallback ) || ( !is_null( $validationCallback ) && call_user_func( $validationCallback, $this->source[$key] ) ) )
      {
        return $this->source[$key];
      }
    }
    return $default;
  }
}

// Just for the demo
function validateUpper( $value )
{
  return ( $value == strtoupper( $value ) );
}

// Mimic some query-string values
$_GET['foo'] = 'bar';
$_GET['bar'] = 'BAZ';
$_GET['lol'] = 'el oh el';

$getHelper = new ParamHelper( $_GET );

echo $getHelper->get( 'foo', 'foo default', 'validateUpper' ), '<br>';
echo $getHelper->get( 'bar', 'bar default', 'validateUpper' ), '<br>';
echo $getHelper->get( 'baz', 'baz default' ), '<br>';
echo $getHelper->get( 'lol' ), '<br>';
echo $getHelper->get( 'rofl' ), '<br>';

A class here would make your life a lot easier.

<?php

class ParamHelper
{
  protected $source;

  public function __construct( array $source )
  {
    $this->source = $source;
  }

  public function get( $key, $default=null, $validationCallback=null )
  {
    if ( isset( $this->source[$key] ) && !empty( $this->source[$key] ) )
    {
      if ( is_null( $validationCallback ) || ( !is_null( $validationCallback ) && call_user_func( $validationCallback, $this->source[$key] ) ) )
      {
        return $this->source[$key];
      }
    }
    return $default;
  }
}

// Just for the demo
function validateUpper( $value )
{
  return ( $value == strtoupper( $value ) );
}

// Mimic some query-string values
$_GET['foo'] = 'bar';
$_GET['bar'] = 'BAZ';
$_GET['lol'] = 'el oh el';

$getHelper = new ParamHelper( $_GET );

echo $getHelper->get( 'foo', 'foo default', 'validateUpper' ), '<br>';
echo $getHelper->get( 'bar', 'bar default', 'validateUpper' ), '<br>';
echo $getHelper->get( 'baz', 'baz default' ), '<br>';
echo $getHelper->get( 'lol' ), '<br>';
echo $getHelper->get( 'rofl' ), '<br>';
岁月流歌 2024-08-08 21:45:40

custom_is_valid() 是否检查空变量? 因为能够删除empty()和“or not”将对改进该代码大有帮助。

Does custom_is_valid() check for an empty variable? Because being able to remove the empty() and "or not" would go a long way to improving that code.

短叹 2024-08-08 21:45:40

正如您将看到的,如果您打开 error_reporting(E_ALL),这实际上并不是最好的方法。 PHP 基本上希望你做

$foo = empty($_GET['foo']) || !custom_is_valid($_GET['foo']) ? 'default' : $_GET['foo'];

As you'll see if you turn error_reporting(E_ALL) on, that isn't really the best way to do it. PHP basically wants you to do

$foo = empty($_GET['foo']) || !custom_is_valid($_GET['foo']) ? 'default' : $_GET['foo'];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文