【问题标题】:value of variable vanishes变量值消失
【发布时间】:2017-12-10 23:28:55
【问题描述】:

我不明白我的变量发生了什么,函数中的值全部消失/变为空。我在函数中没有任何东西。我相信我正确地进行了“检查”'==',并且正确地进行了分配'='。无论我尝试什么,我都会继续得到“文件不存在结果”。如果我var_dump 来查看$cID$check 的值,它们会返回为空''。

function gt_masthead($cID='3', $check=false, $str=''){ //assign value to var

$check == file_exists('./' . $cID . 'bg-userProfile.jpg');  //file exists?

    //echo '<h1> / ' . $cID . ' / ' . $check . ' </h1>';

    dumpVar($cID);  //test

    if($check == 1){
        // file exists, show
        $str = '<img class=" img-fluid" style="width: 100%;" src="./3bg-userProfile.jpg" alt="Card image cap">';
    }else{
        // file does not exist, show default
        $str = '<img class=" img-fluid" style="width: 100%;" src="./img_logoMarvel.gif" alt="Card image cap">';
    }
    return $str;
}

【问题讨论】:

  • 你是怎么调用函数的?你的论点是可选的
  • 如果你不向函数传递参数,为什么要将它们添加到括号中而不是函数内?
  • $check == file_exists('./' . $cID . 'bg-userProfile.jpg'); 为什么你使用== 而不是=?你正在做一个比较,但没有对比较的结果做任何事情
  • 你的赋值是正确的(除了$check变量,应该只是一个=),但是我们需要看看你是如何调用函数的
  • dumpVar() 函数未定义。还是应该是 var_dump()?

标签: php var


【解决方案1】:

试试这个代码:

<?php
    function gt_masthead($cID, $image = "bg-userProfile.jpg", $default = "img_logoMarvel.gif") { //assign value to var
        //check if given image file exists
        if(file_exists("./{$cID}{$image}")) {
            //if it does exist, set the displayImage variable to the path of the image.
            $displayImage = "./{$cID}{$image}";
        } else {
            //if it does not exist, set the displayImage variable to the path of the default image
            $displayImage = "./{$default}";
        }
        //Add the displayImage path to an image element, and return that element.
        $str = "<img class='img-fluid' style='width:100%;' src='{$displayImage}' alt='Card image cap'>";
        return $str;
    }
?>

这段代码的使用非常简单。你这样称呼它:

gt_masthead("cID number", "image name"); - 你也可以在这里使用变量,只需去掉引号。

或者,如果搜索到的图像不存在,您还可以使用第三个变量动态设置默认图像。

gt_masthead("cID number", "image name", "default image name");

我评论了代码以试图解释发生了什么。有什么问题请追问


这里有一个更标准的函数写法,我知道花括号有时会让人迷惑,但代码基本相同。

<?php
    function gt_masthead($cID, $image = "bg-userProfile.jpg", $default = "img_logoMarvel.gif") { //assign value to var
        //check if given image file exists
        if(file_exists("./".$cID.$image)) {
            //if it does exist, set the displayImage variable to the path of the image.
            $displayImage = "./".$cID.$image;
        } else {
            //if it does not exist, set the displayImage variable to the path of the default image
            $displayImage = "./".$default;
        }
        //Add the displayImage path to an image element, and return that element.
        $str = "<img class='img-fluid' style='width:100%;' src='".$displayImage."' alt='Card image cap'>";
        return $str;
    }
?>

【讨论】:

  • 我发现了问题 - 我正在测试虚拟路径而不是物理路径。谢谢你的笔记 - 他们都很感激。
猜你喜欢
  • 2012-11-13
  • 2012-06-11
  • 2015-02-23
  • 2012-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多