如何判断是否在ASP中的变量已被声明 [英] How to tell whether a variable in ASP has been declared

查看:123
本文介绍了如何判断是否在ASP中的变量已被声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我先说我是一个PHP开发人员,而不是一个ASP之一启动了。 (我真的希望有ASP 使用isset())。而且我在实际环境中工作,所以我真的没有机会做任何测试。

Let me start off by saying I'm a PHP developer, not an ASP one. (And I really wish ASP had isset().) And I'm working in a live environment so I don't really have an opportunity to do any testing.

所有我发现的资源提出不同的方式来测试一个变量的存在。

All the resources I've found suggest different ways to test for the existence of a variable.

下面就是我想要做的:

在一些网页上,我设置它保存为一个机器人值的变量<&荟萃GT; 标签:

On SOME pages, I set a variable which holds a value for a robots <meta> tag:

dim dsep_robots
dsep_robots = "nofollow,noindex"

所有页面都包含 header.asp 。在我的头文件,我想测试是否 dsep_robots 有一个值,如果是这样,输出值,否则,什么都不输出

All pages include header.asp. In my header file, I want to test whether dsep_robots has a value and if so, output that value, otherwise, output nothing.

我觉得的说明测试是否 dsep_robots 有一个值可能是这样的:

I think that testing whether dsep_robots has a value might look like this:

if not dsep_robots = "" then
    '...
end if

在PHP状态的最佳做法,当你使用一个变量,可能会或可能不存在,你应该总是测试如果(使用isset($ VAR)){...} (不这样做会引发一个通知,如果变量不存在)。

Best practices in PHP state that when you're using a variable that may or may not exist, you should always test if (isset($var)) {...} (not doing so will trigger a Notice if the variable doesn't exist).

有没有ASP这样的事情 - 即我真的需要测试它是否存在,或可我只是测试它是否有一个值

Is there such a thing in ASP -- i.e. do I really need to test if it exists, or can I simply test if it has a value?

推荐答案

对了UST,你的问题是不是传统的ASP,它是一个VBScript问题。可以的VBScript ASP以外发生的脚本。和汇编并不在VBScript完成的,因为它是一个跨preTED语言。没关系。

ust by the way, your question isn't about classic ASP, it is a VBScript question. VBScript can occur in scripts outside of ASP. And compilation isn't done in VBScript, because it is an interpreted language. Nevermind.

我觉得这里有一些困惑 - 与你的问题似乎有更多做与比未声明的变量初始化的变量。对于未声明的变量,见下文。

I think there's some confusion here -- and your question seems to have more to do with uninitialized variables than undeclared variables. For undeclared variables, see below.

有关初始化,请尝试
对于检查空,尝试使用功能 ISNULL

For uninitialized, try the function IsEmpty. For checking for null, try the function IsNull.

dim x
x = 1
dim t
Response.write isempty(x)
Response.write "<br>"
Response.write isempty(t)   

将显示:

检测未声明的变量

如果您包括期权在头明确,使用非声明的变量会导致运行错误。如果你的脚本不显式的选项不会产生错误,并且没有函数,将告诉你,如果变量已宣战与否。这听起来很马虎,它是,但它是故意的。

If you include Option Explicit in your header, use of a non-declared variable will cause an runtime error. If your script is not Option Explicit it will not generate an error, and there is no function that will tell you if the variable has been declared or not. This sounds sloppy, and it is, but it was intentional.

您可以逃避这个问题的唯一办法就是实际设置选项显式,然后陷阱,当您尝试使用未声明的变量,你会得到错误。如果捕获此特定错误,你会发现,它具有的Err.Number = 500。所以,下面会做你想要什么:

The only way you can escape this is to actually set Option Explicit and then trap the error that you will get when you try to use the undeclared variable. If you trap this particular error you will find that it has Err.Number = 500. So, the following will do what you want:

Option Explicit

dim x

On Error Resume Next

Response.Write dsep_robots  
If Err.Number > 0 Then
    Response.Write Err.Number
end if

当然,如果设置选项显式和你的code是充斥着未声明的变量,那么你会得到被扔得到处都是错误的,所以你需要设置错误在顶部继续下一步您code这样你就可以成功地忽略它,只有陷阱它,当你想。

Of course, if you set Option Explicit and your code is rife with undeclared variables then you'll get errors being thrown all over the place, so you'd need to set On Error Resume Next at the top of your code so you can successfully ignore it, and only trap it when you want to.

顺便说一句,这里是微软的VBScript的在线参考:

By the way, here's Microsoft's online reference for VBScript:

<一个href=\"http://msdn.microsoft.com/en-us/library/d1wf56tt(v=VS.85).aspx\">http://msdn.microsoft.com/en-us/library/d1wf56tt(v=VS.85).aspx

这篇关于如何判断是否在ASP中的变量已被声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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