php 如果在 if 语句内创建变量,它是否可以在 if 语句外使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6243991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:45:39  来源:igfitidea点击:

If you create a variable inside a if statement is it available outside the if statement?

phpvariables

提问by Harigntka

If you have an if statement like this:

如果您有这样的 if 语句:

<?php
$a = 1;
$b = 2;
if ($a < $b) {
$c = $a+$b;
}
?>

Would you be able to access the $c variable outside of the if statement like so:

您能否像这样在 if 语句之外访问 $c 变量:

<?php
$a = 1;
$b = 2;
if ($a < $b) {
$c = $a+$b;
}
echo $c
?>

回答by rid

In PHP, ifdoesn't have its own scope. So yes, if you define something inside the ifstatement or inside the block, then it will be available just as if you defined it outside (assuming, of course, the code inside the block or inside the ifstatement gets to run).

在 PHP 中,if没有自己的作用域。所以是的,如果你在if语句内部或块内部定义了一些东西,那么它就像你在外部定义一样可用(当然,假设块内部或if语句内部的代码可以运行)。

To illustrate:

为了显示:

if (true)  { $a = 5; }    var_dump($a == 5);   // true

The condition evaluates to true, so the code inside the block gets run. The variable $agets defined.

条件评估为true,因此块内的代码得到运行。变量$a被定义。

if (false) { $b = 5; }    var_dump(isset($b)); // false

The condition evaluates to false, so the code inside the block doesn't get to run. The variable $bwill not be defined.

条件评估为false,因此块内的代码不会运行。$b将不会定义该变量。

if ($c = 5) { }           var_dump($c == 5);   // true

The code inside the condition gets to run and $cgets defined as 5($c = 5). Even though the assignment happens inside the ifstatement, the value does survive outside, because ifhas no scope. Same thing happens with for, just like in, for example, for ($i = 0, $i < 5; ++$i). The $iwill survive outside the forloop, because forhas no scope either.

条件中的代码开始运行$c并被定义为5( $c = 5)。即使赋值发生在if语句内部,该值也确实存在于外部,因为if它没有作用域。同样的事情发生在for,就像在,例如,for ($i = 0, $i < 5; ++$i)。该$i会外生存的for循环,因为for无论有没有范围。

if (false && $d = 5) { }  var_dump(isset($d)); // false

falseshort circuits and the execution does not arrive at $d = 5, so the $dvariable will not be defined.

false短路并且执行未到达$d = 5,因此$d不会定义该变量。

For more about the PHP scope, read the variable scopemanual page.

有关 PHP 范围的更多信息,请阅读变量范围手册页。

回答by RétroX

PHP's scope is completely function-based. It's not the same as C or Java where it's local to what block that variables are nested in.

PHP 的范围完全基于函数。它与 C 或 Java 不同,在 C 或 Java 中,变量嵌套在哪个块中。

For PHP's scope:

对于 PHP 的范围:

// Global variable
$a = 0;

function f()
{
  // Cannot be accessed outside of f()
  if (true)
    $b = 0;

  // However, it can still be accessed anywhere in f()
  $b += 1;
}

If you want a variable to be global, simply use the global keyword:

如果您希望变量是全局变量,只需使用 global 关键字:

// Global variable
$a = 0;

function f()
{
  // Use $a from global scope
  global $a;

  // Modifies global $a
  $a += 1;
}

function g()
{
  // Use $b from global scope, even though it hasn't been defined yet
  global $b;

  // Can be accessed outside of g()
  $b = 0;

  // Cannot be accessed outside of g(); this $a "shadows" the global version
  // The global $a is still 0
  $a = 1;
}

回答by user3413723

If the ifstatement containing the variable was executed, then yes, you can access the variable outside of the if statement. Here's a thought on why it works that way. In many programming languages you can "declare" a variable before you use it, just to let the compiler know that it's there. For example in Java you can declare an 'int', then use it like so:

如果执行了if包含变量的语句,那么是的,您可以在 if 语句之外访问该变量。这里有一个关于为什么它会这样工作的想法。在许多编程语言中,您可以在使用之前“声明”一个变量,只是为了让编译器知道它在那里。例如,在 Java 中,您可以声明一个“int”,然后像这样使用它:

int number;
if(true)
    number = 5;

In Java, you have to declare a variable like this before using it in an if-then statement. In php, however, there isn't really a way to do that. Since php is dynamically typed, you can't write int $number. In Java, the computer allocates a 32 bit block of memory (the size of an int) when the variable is declared. In php, I believe, the memory is not allocated until something is actually stored in the variable. The best equivalent to 'declaring' a php variable I could think of would just be to write:

在 Java 中,您必须在 if-then 语句中使用它之前声明一个像这样的变量。然而,在 php 中,并没有真正的方法可以做到这一点。由于 php 是动态类型的,因此您不能编写int $number. 在 Java 中,当声明变量时,计算机会分配一个 32 位的内存块(一个 int 的大小)。在 php 中,我相信,直到某些东西实际存储在变量中时才会分配内存。我能想到的“声明”一个 php 变量的最佳等价物就是这样写:

$number;    //This is NOT needed
if(true)
    $number = 5;

But when you look at the code, it seems kind of strange to just write $numberlike that. I think the computer would think it was equally strange because as I said before, it is a dynamically typed language, and so it doesn't need to allocate a whole chunk of memory for the number. So you can just leave it like this:

但是当您查看代码时,就这样写似乎有点奇怪$number。我认为计算机会认为它同样奇怪,因为正如我之前所说,它是一种动态类型语言,因此不需要为数字分配整个内存块。所以你可以像这样离开它:

if(true)
    $number = 5; 

回答by BugFinder

Depends.

要看。

In PHP chances are yes it would, although of course, if a isnt < b, then c wont exist when you get to the echo c line and your code will complain.

在 PHP 中,它可能是肯定的,当然,如果 a 不是 < b,那么当您到达 echo c 行时 c 将不存在并且您的代码会抱怨。

However, in most languages this wouldnt compile for that reason

然而,在大多数语言中,这不会因为这个原因编译