PHP 回显不存在的变量是否有效?

发布于 2024-10-20 05:46:14 字数 1459 浏览 5 评论 0原文

我在各个页面中使用此代码来创建唯一的标题和活动选项卡。

<?php 
    $title = "Blanktree Design"; 
    $home = "class=\"active\"";
    include "header.php"; 
?>

我在 header.php 中使用此代码来实现这些差异。

<title><?php echo $title; ?></title>

<ul id="nav">
   <li <?php echo $home ?>><span>begin</span><a href="/">home</a></li>
   <li <?php echo $about ?>><span>curious?</span><a href="/about">about</a></li>
   <li <?php echo $contact ?>><span>locate</span><a href="/contact">contact</a></li>
</ul>

由于我是 PHP 的新手,我最好奇的是……仅回显一个不存在的变量是否有效? 我的意思是...它有效并且不会出错。但我只是想知道这是否是糟糕的编程。

提前致谢。

已解决

在一天结束时,所有讨论都已完成,这是我发现在我的特定场景中最有意义的内容:

<?php 
    $title = "Blanktree Design"; 
    $home = "class=\"active\"";
    $about = $contact = "";
    include "header.php";
?>

并像以前一样调用变量:

<ul id="nav">
   <li <?php echo $home ?>><span>begin</span><a href="/">home</a></li>
   <li <?php echo $about ?>><span>curious?</span><a href="/about">about</a></li>
   <li <?php echo $contact ?>><span>locate</span><a href="/contact">contact</a></li>
</ul>

I'm using this code in my individual pages to create a unique title and active tab.

<?php 
    $title = "Blanktree Design"; 
    $home = "class=\"active\"";
    include "header.php"; 
?>

And I'm using this code in my header.php to carry out these differences.

<title><?php echo $title; ?></title>

<ul id="nav">
   <li <?php echo $home ?>><span>begin</span><a href="/">home</a></li>
   <li <?php echo $about ?>><span>curious?</span><a href="/about">about</a></li>
   <li <?php echo $contact ?>><span>locate</span><a href="/contact">contact</a></li>
</ul>

What I'm most curious of, since I'm brand new to PHP is... Is it valid to just echo a non-existent variable?
I mean... it works and it doesn't error out. But I was just wondering if this is poor programming.

Thanks in advance.

RESOLVED

At the end of the day all the discussion is complete and this is what I've found to make the most sense in my specific scenario:

<?php 
    $title = "Blanktree Design"; 
    $home = "class=\"active\"";
    $about = $contact = "";
    include "header.php";
?>

And calling the variables the same as before:

<ul id="nav">
   <li <?php echo $home ?>><span>begin</span><a href="/">home</a></li>
   <li <?php echo $about ?>><span>curious?</span><a href="/about">about</a></li>
   <li <?php echo $contact ?>><span>locate</span><a href="/contact">contact</a></li>
</ul>

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

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

发布评论

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

评论(3

⒈起吃苦の倖褔 2024-10-27 05:46:14

是的,回显(以及任何其他用法)不存在的变量总是糟糕的编程。
它实际上是错误的 - 你只是因为错误的 PHP 设置而看不到它。

error_reporting(E_ALL);

脚本顶部将显示这些错误。

尽管 PHP 几乎不允许使用未定义的变量,但一个好的编程习惯是在使用代码之前定义每个变量
它将使您避免许多错误和意外行为甚至严重的漏洞。

然而,大多数人错误地理解了这个问题,认为应该在使用变量之前检查变量是否存在。但稍微思考一下这个话题,你会发现它非常荒谬 - 似乎这种检查的唯一目的是堵住错误消息。那么,语言开发人员发明这样的警告只是为了打扰程序员进行不必要的验证吗?

当然不是。
此警告(与任何其他警告一样)的真正目的是让程序员知道出现了问题。
这真的很简单:PHP 只是告诉你“兄弟,你难道忘记给这个变量赋一些值吗?看起来你会使用它,所以,你期望它有一些值,但是没有”只是为了让你们知道。”
但是通过使用 isset() 我们只是阻止这个警告,仅此而已。伤害自己。

此外,通过初始化一个变量,我们可以确定变量的存在及其值。

一个小例子:

<?
while ($i < 10) {
  echo $i." ";
}
echo "<br>";
while ($i < 10) {
  echo $i." ";
}
?>

这个片段将打印多少行?只有一个。

因此,每个脚本变量都应该在使用前初始化。

一个特殊的问题是外部变量。< br>
程序员无法控制其行为。有时外部变量的存在是应用程序逻辑的一种情况。例如:

if (isset($_GET['id'])) {
  //displays particular article
} else {
  //displays list of articles
}

允许这样检查。
但原始外部变量的使用应受到严格限制。
例如,如果您要使用之前输入的值填充表单字段,建议创建另一个变量(例如 $FORM),并使用从原始外部变量正确准备的值初始化其项目。

Yes, it's always poor programming to echo (and any other usage) a non-existent variable.
And it's actually errors out - you just doesn't see it due to wrong PHP settings.

error_reporting(E_ALL);

at the top of the script will show ye these errors.

Although PHP scarcely allow usage of undefined variable, a good programming practice is to define every variable in the code before it's usage.
It will let you to avoid numerous mistakes and unexpected behaviors or even serious vulnerabilities.

Most people, however, take this issue wrong, thinking that a variable should be checked for existence before use. But thinking over this topic a bit, you'll find it quite ridiculous - it seems the only intention of such checking is to gag an error message. So, the language developers invented such warning only to bother a programmer with unnecessary verification?

Of course not.
A real intention of this warning (as any other warning) is to let a programmer know that something is going wrong.
It's very easy really: PHP just tells you "Don't you forget to assign some value to this variable, bro? It seems you're gonna use it, so, you expect some value it it, but there isn't. Just to let ye know."
But by using isset() we just gag this warning and nothing more. Doing a disservice to youself.

Moreover, by initializing a variable, one can be sure of both variable existence and it's value.

a little example:

<?
while ($i < 10) {
  echo $i." ";
}
echo "<br>";
while ($i < 10) {
  echo $i." ";
}
?>

How many lines will print this snippet out? Only one.

Thus, every one script variable should be initialized before use.

A special matter is outside variables.
A programmer is unable to control it's behavior. And sometimes existence of a outside variable is a case of application logic. For example:

if (isset($_GET['id'])) {
  //displays particular article
} else {
  //displays list of articles
}

It's allowed to check this way.
But usage of raw outside variables should be strictly limited.
For example, if you're gonna to populate form fields with previously entered values, it's recommended to create another variable, say, $FORM, and initialize it's items with values, properly prepared from raw outside variable.

生寂 2024-10-27 05:46:14

更好的编程实践是首先检查它是否已设置。例如

if (isset($variable))
  echo $variable;

Better programming practice would be to check if it's set first. Such as

if (isset($variable))
  echo $variable;
眸中客 2024-10-27 05:46:14

它会抛出警告,请参阅PHP:打印未定义的变量而不警告,但是您是否看到它取决于您的错误报告级别。

It will throw a warning, see PHP: printing undefined variables without warning, but whether or not you see it depends on your level of error reporting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文