开发者

is this global variables? PHP

开发者 https://www.devze.com 2023-01-15 03:05 出处:网络
i dont know what is the name of this line of code.. and i don\'t even know if im doing the right structure of code.. i can\'t research to google because i don\'t know what to search.. :)

i dont know what is the name of this line of code.. and i don't even know if im doing the right structure of code.. i can't research to google because i don't know what to search.. :)

anyway, here's the code..

<?php
$hello = 1;

switch($hello)
{
  case 1:
    FOO:
    break;

  case 2:
    BAR:
    break;

}


FOO:
echo "hello world!";

BAR:
echo "hola world!";

?>

if the value of $hello == 1, does the output will be "hello开发者_JAVA技巧 world!"? if the value of $hello == 2, does the output will be "hola world!"?

i got this to work..

<?php
$hello = '1';

switch($hello)
{
  case '1':
    goto FOOO;
    break;

  case '2':
    goto BARR;
    break;

}


FOOO:
echo "hello world!";
goto LAST;

BARR:
echo "hola world!";

LAST:
?>

thanks for the suggestions and answers.. :)


<?php
$hello = 1;

switch($hello)
{
  case 1:
    FOO();
    break;

  case 2:
    BAR();
    break;

}


function FOO() {
  echo "hello world!";
}

function BAR() {
  echo "hola world!";
}
?>


The someText: syntax is used for goto control structure.

Actually, your code isn't doing what it is expected to do. I thought it would not run, but maybe it does.

In all cases, it will just output hello world!hola world!, since the switch does nothing, and two echos are executed then.

Probably, what was expected is to have two goto inside the switch. In this case, if $hello is equal to 1, the code will display hello world!hola world!, and if it is equal to 2, only hola world! will be displayed. Note that you should avoid using goto when possible, so here, the answer by Crayon Violent to this question contains a better alternative (even if the flow is modified and will give other results than with gotos).


You will get a fatal error because it will tell you that label FOO is already defined.

Crayon Violet has a good workaround for this. I would recommend his method, as you will not have to work with GOTOs. Not that GOTOs are bad when used correctly, but in this situation, I would say it wouldn't be a good use of them.


I believe you're asking what global scope is.

$hello = 1;

$hello = 'hello world!';
$hola = 'hola world!';

function globalvars($x) {
    global $hello,$hola;
    switch($x)
    {
      case 1:
        echo $hello;
        break;
      case 2:
        echo $hola;
        break;
    }
}

globalvars($hello); // "hello world!"
globalvars(2); // "hola world!"

http://php.net/manual/en/language.variables.scope.php

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号