-1

很抱歉打扰所有忙碌的人,我只是无法弄清楚为什么该功能不起作用。反复试验花了 50 分钟。给出的错误是针对第 15 行“case 'smug': smug_1(); break;”

<?php
include "globals.php";

if ($ir['jailtime'] > 0) {
    print "<center>The docks are closed to jail birds come back when thay release you</center>";
    exit;
}
if ($ir['hospital'] > 0) {
    print "<center>You cant go sailing while in hospital</center>";
    exit;
}
switch ($_GET['action']) {
case 'smug':
    smug_1();
    break;
case 'smugsub':
    smug_1_sub();
    break;
case 'smug2':
    smug_2();
    break;
case 'smugsub':
    smug_2_sub();
    break;
case 'smug3':
    smug_3();
    break;
case 'smug3sub':
    smug_3_sub();
    break;
case 'smug4':
    smug_4();
    break;
case 'smug4sub':
    smug_4_sub();
    break;
case 'smug5':
    smug_5();
    break;
case 'smug5sub':
    smug_5_sub();
    break;
case 'smug6':
    smug_6();
    break;
case 'smug6sub':
    smug_6_sub();
    break;
default:
    smug_index('action');
    break;
}

function smug_index()
{
    global $ir, $c, $userid;
    print "<center><h3>Crystal Smuggling</h3></center>
    <center>You have {$ir['csmuggling']} Smuggling Turns Left.<br />
    Welcome to the crystal smuggling docks. You can go to these location, more will be available when you are a higher level:</center>
    [<a href='csmuggling.php?action=smug'>&gt; France</a>]";
    if ($ir['level'] > 5) {
        print "[<a href='csmuggling.php?action=smug2'>&gt; Holland</a>]";
        if ($ir['level'] > 10) {
            print "[<a href='csmuggling.php?action=smug3'>&gt; Germany</a>]";
            if ($ir['level'] > 20) {
                print "[<a href='csmuggling.php?action=smug4'>&gt; Portugal</a>]";
                if ($ir['level'] > 30) {
                    print "[<a href='csmuggling.php?action=smug5'>&gt; Spain</a>]";
                    if ($ir['level'] > 70) {
                        print "[<a href='csmuggling.php?action=smug6'>&gt; Switzerland</a>]</center>";
                    }

                    function smug_1()
                    {
                        global $db, $ir, $c, $userid;
                        $rand_gems = rand(1, 3);
                        echo "<center>You get over to France And Managed To Smuggle $rand_gems crystals back.</center>";
                        mysql_query("UPDATE users SET crystals=crystals+$rand_gems WHERE userid=$userid");
                        mysql_query("UPDATE users SET csmuggling=smuggling-1 WHERE userid=$userid");
                        print "<center>[<a href='explore.php'>&gt; Back</a>]</center>";
                    }

                    function smug_2()
                    {
                        global $db, $ir, $c, $userid;
                        $rand_gems = rand(2, 6);
                        echo "<center>You get over to Holland And Managed To Smuggle $rand_gems crystals back.</center>
                        ";
                        mysql_query("UPDATE users SET crystals=crystals+$rand_gems WHERE userid=$userid");
                        mysql_query("UPDATE users SET csmuggling=smuggling-1 WHERE userid=$userid");
                        print "<center>[<a href='explore.php'>&gt; Back</a>]</center>";
                    }

                    function smug_3()
                    {
                        global $db, $ir, $c, $userid;
                        $rand_gems = rand(4, 9);
                        echo "<center>You get over to Germany And Managed To Smuggle $rand_gems crystals back.</center>
                        ";
                        mysql_query("UPDATE users SET crystals=crystals+$rand_gems WHERE userid=$userid");
                        mysql_query("UPDATE users SET csmuggling=smuggling-1 WHERE userid=$userid");
                        print "<center>[<a href='explore.php'>&gt; Back</a>]</center>";
                    }

                    function smug_4()
                    {
                        global $db, $ir, $c, $userid;
                        $rand_gems = rand(9, 12);
                        echo "<center>You get over to Portugal And Managed To Smuggle $rand_gems crystals back.</center>
                        ";
                        mysql_query("UPDATE users SET crystals=crystals+$rand_gems WHERE userid=$userid");
                        mysql_query("UPDATE users SET csmuggling=smuggling-1 WHERE userid=$userid");
                        print "<center>[<a href='explore.php'>&gt; Back</a>]</center>";
                    }

                    function smug_5()
                    {
                        global $db, $ir, $c, $userid;
                        $rand_gems = rand(11, 15);
                        print "<center>You get over to Spain And Managed To Smuggle $rand_gems crystals back.</center>
                        ";
                        mysql_query("UPDATE users SET crystals=crystals+$rand_gems WHERE userid=$userid");
                        mysql_query("UPDATE users SET csmuggling=smuggling-1 WHERE userid=$userid");
                        print "<center>[<a href='explore.php'>&gt; Back</a>]</center>";
                    }

                    function smug_6()
                    {
                        global $db, $ir, $c, $userid;
                        $rand_gems = rand(13, 17);
                        echo "<center>You get over to Switzerland And Managed To Smuggle $rand_gems crystals back.</center>
                        ";
                        mysql_query("UPDATE users SET crystals=crystals+$rand_gems WHERE userid=$userid");
                        mysql_query("UPDATE users SET csmuggling=smuggling-1 WHERE userid=$userid");
                        print "<center>[<a href='explore.php'>&gt; Back</a>]</center>";
                    }
                }
            }
        }
    }
}

感谢您花时间帮助我。:)

4

2 回答 2

3

您的函数是在if() ...块内定义的,因此在匹配 if 块之前它们不可用于解析器。if()在你的块之外定义它们!

于 2012-12-29T13:18:31.387 回答
1

您必须在输出 .php 文档开始之前加载/包含所有功能(功能代码)...

所以

  1. 必须首先包含/加载函数(并由 PHP 读取)
  2. 然后你可以在一个文件中调用它们!

显然,您的“输出”代码是在函数代码之前设置的(因此 PHP 无法执行函数,因为它不知道此时存在函数)。因此,您必须以其他方式编写代码。

<?php

include "globals.php";


//first functions

function smug_1()
{
global $db,$ir,$c,$userid;
$rand_gems = rand(1,3);
echo "<center>You get over to France And Managed To Smuggle $rand_gems crystals back.</center>
";
mysql_query("UPDATE users SET crystals=crystals+$rand_gems WHERE userid=$userid");
mysql_query("UPDATE users SET csmuggling=smuggling-1 WHERE userid=$userid");
print "<center>[<a href='explore.php'>&gt; Back</a>]</center>
";
}
function smug_2()
{
global $db,$ir,$c,$userid;
$rand_gems = rand(2,6);
echo "<center>You get over to Holland And Managed To Smuggle $rand_gems crystals back.</center>
";
mysql_query("UPDATE users SET crystals=crystals+$rand_gems WHERE userid=$userid");
mysql_query("UPDATE users SET csmuggling=smuggling-1 WHERE userid=$userid");
print "<center>[<a href='explore.php'>&gt; Back</a>]</center>
";
}
function smug_3()
{
global $db,$ir,$c,$userid;
$rand_gems = rand(4,9);
echo "<center>You get over to Germany And Managed To Smuggle $rand_gems crystals back.</center>
";
mysql_query("UPDATE users SET crystals=crystals+$rand_gems WHERE userid=$userid");
mysql_query("UPDATE users SET csmuggling=smuggling-1 WHERE userid=$userid");
print "<center>[<a href='explore.php'>&gt; Back</a>]</center>
";
}
function smug_4()
{
global $db,$ir,$c,$userid;
$rand_gems = rand(9,12);
echo "<center>You get over to Portugal And Managed To Smuggle $rand_gems crystals back.</center>
";
mysql_query("UPDATE users SET crystals=crystals+$rand_gems WHERE userid=$userid");
mysql_query("UPDATE users SET csmuggling=smuggling-1 WHERE userid=$userid");
print "<center>[<a href='explore.php'>&gt; Back</a>]</center>
";
}
function smug_5()
{
global $db,$ir,$c,$userid;
$rand_gems = rand(11,15);
print "<center>You get over to Spain And Managed To Smuggle $rand_gems crystals back.</center>
";
mysql_query("UPDATE users SET crystals=crystals+$rand_gems WHERE userid=$userid");
mysql_query("UPDATE users SET csmuggling=smuggling-1 WHERE userid=$userid");
print "<center>[<a href='explore.php'>&gt; Back</a>]</center>
";
}
function smug_6()
{
global $db,$ir,$c,$userid;
$rand_gems = rand(13,17);
echo "<center>You get over to Switzerland And Managed To Smuggle $rand_gems crystals back.</center>
";
mysql_query("UPDATE users SET crystals=crystals+$rand_gems WHERE userid=$userid");
mysql_query("UPDATE users SET csmuggling=smuggling-1 WHERE userid=$userid");
print "<center>[<a href='explore.php'>&gt; Back</a>]</center>
";
}


function smug_index()
{
global $ir,$c,$userid;
print "<center><h3>Crystal Smuggling</h3></center>
<center>You have {$ir['csmuggling']} Smuggling Turns Left.<br />

Welcome to the crystal smuggling docks. You can go to these location, more will be available when you are a higher level:
  </center>
<center>
[<a href='csmuggling.php?action=smug'>&gt; France</a>]
";
if ($ir['level'] > 5) {
print "[<a href='csmuggling.php?action=smug2'>&gt; Holland</a>]
";
if ($ir['level'] > 10) {
print "[<a href='csmuggling.php?action=smug3'>&gt; Germany</a>]
";
if ($ir['level'] > 20) {
print "[<a href='csmuggling.php?action=smug4'>&gt; Portugal</a>]
";
if ($ir['level'] > 30) {
print "[<a href='csmuggling.php?action=smug5'>&gt; Spain</a>]
";
if ($ir['level'] > 70) {
print "[<a href='csmuggling.php?action=smug6'>&gt; Switzerland</a>]

</center>";
}

}
}
}
}
}


 //then output codes

if ($ir['jailtime'] > 0){
print "<center>The docks are closed to jail birds come back when thay release you</center>";
exit;
}
if ($ir['hospital'] > 0){
print "<center>You cant go sailing while in hospital</center>";
exit;
}
switch($_GET['action'])
{
case 'smug': smug_1(); break;
case 'smugsub': smug_1_sub(); break;
case 'smug2': smug_2(); break;
case 'smugsub': smug_2_sub(); break;
case 'smug3': smug_3(); break;
case 'smug3sub': smug_3_sub(); break;
case 'smug4': smug_4(); break;
case 'smug4sub': smug_4_sub(); break;
case 'smug5': smug_5(); break;
case 'smug5sub': smug_5_sub(); break;
case 'smug6': smug_6(); break;
case 'smug6sub': smug_6_sub(); break;
default: smug_index('action'); break;
}

?>

试试看。唯一的问题是 globals.php 可以在函数之前或之后(因为我不知道 globals.php 做了什么).centersmugsub

于 2012-12-29T13:27:00.067 回答