简体   繁体   English

PHP,解析错误:语法错误,意外的T_IF,预期为T_FUNCTION

[英]PHP, Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION

Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION in C:\\wamp\\www\\Exam Creator\\cls_FillInTheBlank.php on line 31

I received this error while trying to run the cls_FillInTheBlank.php which includes cls_template.php. 我在尝试运行包含cls_template.php的cls_FillInTheBlank.php时收到此错误。 I have the two files posted at the for review. 我有两个文件张贴在进行审查。

cls_FillInTheBlank.php

<?php

    include("cls_template.php");

class fillintheblank{

    function _construct(){
        $cls_blankInput = new template;
        $cls_questionString = new template;
        $cls_fillInTheBlank = new template;
    }

    if(class_exists($cls_blankInput)){
        $cls_blankInput->fillString("<input type='text' name='[qInputName]' id='[qInputId]' value='[qInputValue]' />");
        $cls_blankInput->fillPlaceholderStrings(array("[qInputName]","[qInputId]","[qInputValue]"));
    } else {
        print("\$cls_blankInput not defined.<br>");
    }

    if(class_exists($cls_questionString)){
        $cls_questionString->fillString("");    // to be set later
        $cls_questionString->fillPlaceholderStrings(array("[fib_blank]"));
    } else {
        print("\$cls_questionString not defined.<br>");
    }

    if(class_exists($cls_fillInTheBlank)){
        $cls_fillInTheBlank->fillString("<li>[qFillInTheBlankString]</li>");
        $cls_fillInTheBlank->fillPlaceholderStrings(array("[qFillInTheBlankString]"));
    } else {
        print("\$cls_fillInTheBlank not defined.<br>");
    }

    public function q_fib_buildString($cls_question){
        $i_qID = "";            // @type INTEGER
        $s_html = "";           // @type STRING
        $a_inputs = "";         // @type ARRAY
        $s_innerHTML = "";      // @type STRING
        $s_outerHTML = "";      // @type STRING

        $i_qID = $cls_question->qo_i_id;

        // build the HTML for the blank input
        foreach($cls_question->qo_a_answerSet as $answer){
            $this->cls_blankInput->fillPlaceholderValues(array("qID_".$i_qID,"qID_".$i_qID,$answer["value"]));
            $a_inputs[count($a_inputs)] = $this->cls_blankInput->buildTemplate();
        }

        // build the inner HTML
        $this->cls_questionString->fillString($cls_question->qo_s_string);
        $this->cls_questionString->fillPlaceholderValues($a_inputs);
        $s_innerHTML = $this->cls_blankInput->buildTemplate();


        // build the outer HTML
        $this->cls_fillInTheBlank->fillPlaceholderValues($s_innerHTML);
        $outerHTML =  $this->cls_fillInTheBlank->buildTemplate();

        return $outerHTML;

    }

    public function q_fib_buildString($s_template){
        $s_fib_patterns = array();
        $s_input_blank = "";
        $s_fib_patterns_blank = "[\[fib_blank\]]";

        // array of placeholders 
        $a_fib_patterns['input'] = array();
            // place holder for the name attribute of the input tag
            $a_fib_patterns['attribute']['name'] ="[\[qInputName\]]";
            // place holder for the id attribute of the input tag
            $a_fib_patterns['attribute']['id'] = "[\[qInputId\]]";

        // array of values
        $a_fib_replace['value'] = array();
            // value for the name attribute
            $a_fib_replace['value']['name'] = "qId_".$i_id."[]";
            // value fo the id attribute
            $a_fib_replace['value']['id'] = "qId_".$i_id."[]";

        // build blank input
        $s_input_blank = preg_replace($a_fib_patterns['input'],$a_fib_replace['value'],fillintheblank::$s_blankInput);
        // build question string
        $s_string = preg_replace($s_fib_patterns_blank,$s_input_blank,$s_string);

        /* START CODE FOR TESTING */
//      echo($s_input_blank);   // echo code for the blank inputs
        /* END CODE FOR TESTING */

        // return question string
        return $s_string;

    }

}

?>

You've basically got this: 您基本上已经有了:

class fillintheblank {
   if (...) {
   }
   if (...) {
   }
}

That's not permitted. 那是不允许的。 A class definition has member variables and method definitions (ie: functions). 类定义具有成员变量和方法定义(即:函数)。 You can't put "naked" PHP code in a class definition. 您不能将“裸” PHP代码放在类定义中。 YOu'll have to wrap it in a method, probably a constructor. 您必须将其包装在方法中,可能是构造函数。

Your problem is you started a class, but then you started writing what would usually be found in a function body in it. 您的问题是您开始了一个类,但随后开始编写通常可以在其中的函数体中找到的内容。 You need to enclose lines 27-56 in a function definition. 您需要在函数定义中包含第27-56行。

Also, unrelatedly, you named a method _construct when you probably meant __construct . 另外,与此无关,当您可能用__construct时,您为方法_construct命名。 Two underscores, not just one. 两个下划线,而不仅仅是一个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Yii解析错误:语法错误,意外的&#39;if&#39;(T_IF),期望的函数(T_FUNCTION) - Yii Parse error: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION) 解析错误:语法错误,意外\\&#39;?&gt; \\&#39;,期待函数(T_FUNCTION) - Parse error: syntax error, unexpected \'?>\', expecting function (T_FUNCTION) 语法错误,意外的T_FUNCTION,预期为&#39;)&#39; - syntax error, unexpected T_FUNCTION, expecting ')' php解析错误:语法错误,意外的T_STRING,在构造上需要T_FUNCTION - php Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION on construct PHP解析错误:语法错误,意外的T_STRING,预期的T_FUNCTION - PHP Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION PHP版本5.2.14 /解析错误:语法错误,意外的T_FUNCTION,期望&#39;)&#39; - PHP Version 5.2.14 / Parse error: syntax error, unexpected T_FUNCTION, expecting ')' 解析错误:语法错误,意外的&#39;=&#39;,期望在第33行的DB_driver.php中出现T_FUNCTION - Parse error: syntax error, unexpected '=', expecting T_FUNCTION in DB_driver.php on line 33 Wordpress-解析错误:语法错误,意外的“函数”(T_FUNCTION),期望的标识符(T_STRING) - Wordpress - Parse error: syntax error, unexpected 'function' (T_FUNCTION), expecting identifier (T_STRING) 解析错误:语法错误,意外的T_ELSE,在1834行的/home/filename.html.php中预期为T_FUNCTION - Parse error: syntax error, unexpected T_ELSE, expecting T_FUNCTION in /home/filename.html.php on line 1834 解析错误:语法错误,意外的T_VARIABLE,预期为T_FUNCTION - Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM