使用不同类型的变量简化empty()+ echo [英] Simplify empty() + echo with different kind of variables

查看:80
本文介绍了使用不同类型的变量简化empty()+ echo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我的代码错误并通知您。因此,我应该检查每个要回显的变量。我感兴趣的特定情况是一个新页面,它也可以充当编辑页面。如果选择了某些内容,它将显示版本页面,其中的字段填充有要编辑的内容。如果未选择任何内容,则该页面位于同一页面,但字段为空。这给出了我的意思的想法:

I'm trying to get my code error and notice free. Therefore I should check every variable I want to echo. The specific case I am interested in, is a "new" page that can also act as an "edit". If something was selected, it displays the edition page with the fields filled in with the content to be edited. If there was nothing selected it's the same page but with empty fields. This gives an idea of what I mean:

  <li class="bordertop">
    <label>Recommended knowledge</label>
    <input class="first_input" name="recommended[]" value="<?php if (count ($Subject->retrieveLink('recommended')) > 0) echo current($Subject->retrieveLink('recommended')); ?>" placeholder="Title"/>
    <input class="second_input" name="recommended_url[]" value="<?php if (key ($Subject->retrieveLink('recommended')) != null) echo key($Subject->retrieveLink('recommended')); ?>" placeholder="http://www.example.com"/>
  </li>

我真的不喜欢页面中有那么多代码。我试图将逻辑与表示分离得尽可能多,并保持一种OOP方法。我正在考虑做类似的事情:

I really don't like THAT much code in my page. I am trying to separate as much as I can logic from presentation as well as keeping an OOP approach. I was thinking about doing something similar to this:

// Check + echo
function checko($arg = null)
  {
  if (!is_array($arg))
    if (!empty($arg))
      echo $arg;
  }

但这似乎在将来也带来麻烦(从内部回荡)不允许我进一步修改字符串,也根本感觉不到合适的解决方案)。我想检查和回显很多变量,有时会填充这些变量,有时又是空的。最好的方法是什么?我知道我可以通过执行 if(empty($ variable))echo $ variable 到处重复我的代码,但是:

But that also seems to be calling for trouble in the future (echoing from inside doesn't let me further modify the string and it doesn't feel like a 'proper' solution at all). There are many of variables that I want to check and echo which sometimes will be filled and some times empty. What's the best approach for this? I know I could repeat my code everywhere by doing the if (empty($variable)) echo $variable, but:


  1. 这无法从对象返回值(从PHP 5.4起,我相信它将在5.5中工作)。这是我最关心的问题,因为我经常使用它。

  1. This doesn't work (as of PHP 5.4, I believe it'll work in 5.5) with returning values from objects. This is my main concern, since I use that very often.

我重复的代码太多了。否

I'd have too much repeated code. No


推荐答案


我正在尝试得到我的代码错误,并免费通知。因此,我应该检查我想回显的每个变量。

I'm trying to get my code error and notice free. Therefore I should check every variable I want to echo.

不,您不应该。

这些通知不仅使您忙于与之抗争。它们只是为了帮助,而不是使您的代码变得复杂。

您只需要确定要操作的变量即可。并且添加批量 if(empty())不会有任何好处。

Nope, you shouldn't.
Look, these notices aren't only to make your busy fighting them. They are for help, not to make your code complicated.
You just need to be sure what variables you operate. And adding bulk if(empty()) won't do any good.


我真的不喜欢页面中有那么多代码。

I really don't like THAT much code in my page.

确实是模板的太多代码。其中一些应移至控制器中

在调用模板之前准备好数据,然后使其变得清晰

That's indeed too much code for the template. Some of it should be moved into controller
Prepare your data before calling template, and then make it plain

<input class="first_input" name="recommended[]" value="<?=$recommended?>" placeholder="Title"/>
<input class="second_input" name="recommended_url[]" value="<?=$recommended_url?>" placeholder="http://www.example.com"/>

这篇关于使用不同类型的变量简化empty()+ echo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆