if 语句在函数内部没有找到变量(Wordpress 插件)

该变量在函数内不可访问。当我在函数外回显变量时,我得到了正确的结果。当我在函数内回显/使用变量时,我得到一个“注意:未定义变量:/setup.php 中的 crLocalIncompleteStatus 35”


有任何想法吗?


<?php

    global $wpdb;

    $table_name = $wpdb->prefix . 'crLocal';


    $crLocalIncompleteStatus = '';

    $crLocalCompleteStatus = '';


    $crLocal_DBChecked = $wpdb->get_var("SELECT COUNT(*) FROM $table_name");


    function crLocal_CheckDB() {

        global $crLocal_DBChecked,  $crLocalIncompleteStatus, $crLocalCompleteStatus;


        if ($crLocal_DBChecked == '0') { 

            $crLocalIncompleteStatus = 'block' ;

            $crLocalCompleteStatus = 'none' ;

        } elseif ($crLocal_DBChecked != '0') { 

            $crLocalIncompleteStatus = 'none' ;

            $crLocalCompleteStatus = 'block' ;

        } else {

            // Broken

            // More Code to follow

        }


        echo $crLocal_DBChecked;

    }


    crLocal_CheckDB();


    $crLocalInstallIncomplete = '<div id="crLocalInstallIncomplete" style="display: '.$crLocalIncompleteStatus.';">' ;


    $crLocalInstallComplete = '<div id="crLocalInstallComplete" style="display: '.$crLocalCompleteStatus.';">' ;


?>

所以我需要:


当数据库中的计数为 0 时,$crLocalIncompleteStatus 为 'block',大于 0 时为 'none'。


当数据库中的计数为 0 时,$crLocalCompleteStatus 为 'none',大于 0 时为 'block'。


杨魅力
浏览 145回答 1
1回答

一只斗牛犬

这是因为您的变量 $crLocalIncompleteStatus 超出了您的函数范围,您可以通过将该变量作为参数添加到函数中来更正此问题function crLocal_CheckDB($crLocalIncompleteStatus) {&nbsp; &nbsp; global $crLocal_DBChecked,&nbsp; $crLocalIncompleteStatus, $crLocalCompleteStatus;&nbsp; &nbsp; if ($crLocal_DBChecked == '0') {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $crLocalIncompleteStatus = 'block' ;&nbsp; &nbsp; &nbsp; &nbsp; $crLocalCompleteStatus = 'none' ;&nbsp; &nbsp; } elseif ($crLocal_DBChecked != '0') {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $crLocalIncompleteStatus = 'none' ;&nbsp; &nbsp; &nbsp; &nbsp; $crLocalCompleteStatus = 'block' ;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; // Broken&nbsp; &nbsp; &nbsp; &nbsp; // More Code to follow&nbsp; &nbsp; }&nbsp; &nbsp; echo $crLocal_DBChecked;}crLocal_CheckDB($crLocalIncompleteStatus);
打开App,查看更多内容
随时随地看视频慕课网APP