在定义变量后将其设置为空(“")的目的是什么? [英] what is the purpose of setting variables to empty (" ") after defining them?

查看:119
本文介绍了在定义变量后将其设置为空(“")的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习有关验证表单输入数据的知识,w3schools上有一些我不理解的代码.见下文:

Im learning about validating form input data and there was this bit of code on w3schools that i don't understand. see below:

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

//bla bla bla

我不明白"//定义变量并设置为空值"的含义.

i dont understand what "// define variables and set to empty values" means.

推荐答案

请勿使用本教程.我正在查看此test_input()函数,无法相信有人会发布此代码:

Don't use this tutorial. I'm looking at this test_input() function and can't believe that someone would publish this code:

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

在这里查看我的答案以获取有关为什么这是一个坏主意以及如何执行操作的更多信息: https://stackoverflow. com/a/7810880/362536

See my answer here for more information on why this is a bad idea, and what to do instead: https://stackoverflow.com/a/7810880/362536

现在,转到您的实际问题.在许多语言中,要求您在使用变量之前先声明要使用的变量. PHP不是这些语言之一.有人认为,如果您知道所有变量都在前面,那么您的代码就更容易阅读,并且会根据代码样式定义.也就是说,它们将列出变量并将其设置为某个值.您拥有的代码:

Now, on to your actual question. In many languages, you are required to declare what variables you are going to use before you use them. PHP is not one of these languages. Some people think that your code is easier to read if you know what all the variables are up front, and will define these variables as a matter of code style. That is, they will list variables and set them to some value. The code you have:

$nameErr = $emailErr = $genderErr = $websiteErr = "";

...在功能上等同于:

... is functionally equivalent to:

$nameErr = '';
$emailErr = '';
$genderErr = '';
$websiteErr = '';

在PHP中,您可以链接变量分配,这就是为什么这两个等效的原因.

In PHP, you can chain your variable assignments, which is why these two are equivalent.

如果选择声明样式是出于变量的考虑,则不应为它们分配空字符串.您应该改用null.假设我有一个变量,该变量是有条件地从用户输入字段分配的数据.如果我使用空字符串''初始化变量,则无法知道表单字段是否存在以及是否已提交.如果我使用null,则可以使用isset()来确定该变量是否具有一个空的表单字段(空字符串'')或null作为其值.

If you choose to declare your variables as a matter of style, you should not assign an empty string to them. You should use null instead. Suppose I have a variable that is conditionally assigned data from a user input field. If I use empty string '' to initialize variables, I have no way of knowing if the form field existed and was submitted or not. If I use null, I can then use isset() to determine if the variable has an empty form field (empty string '') or null as its value.

您可以很好地确定空字符串初始化适合您的应用程序.就像约翰·康德(John Conde)在上面的评论中说的那样,许多人都在使用它,因此他们可以自由地连接变量而不必担心变量中的内容,即使没有分配显式值,您也已经分配了一个空字符串.我会争辩说,很少有时间使用在整个生命周期中始终保持单一目的的数据,并且最好以最清晰的表示形式存储数据.如果要将变量指定为null,则其值的确应为null,而不是空字符串.选择什么取决于您和您的具体情况.

You may very well decide that empty string initialization is right for your application. As John Conde says in the comments above, many folks use this so they can freely concatenate variables without worrying about what's in them, as even if they don't get an explicit value assigned, you already assigned an empty string. I would argue that there is rarely a time when you will work with data that always maintains a single purpose throughout it's lifetime, and that it's best to have data in the cleanest representation as possible. If a variable is intended to be null, its value should indeed be null and not an empty string. What you choose is up to you and your specific circumstances.

这篇关于在定义变量后将其设置为空(“")的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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