无法在 PHP 中将变量设置为 true

发布于 2024-12-26 18:52:43 字数 640 浏览 1 评论 0原文

我正在制作一个 PHP 脚本,其中使用类布尔变量,但由于某种原因,当我想将其设置为 true 时,它不起作用:/

<?php


class TinyFram {



    private $urlMatched;


    public function get($url, $method){

        $urlR = str_replace('/', '\/', $url);
        $urlR = '^' . $urlR . '\/?$';

        if (preg_match("/$urlR/i", $reqURI, $rMatch)) {
            $this->urlMatched = true; // I SET IT TO TRUE HERE
            $method($rMatch);
        } 
        if(!$this->urlMatched) {
            echo var_dump($this->urlMatched); // BUT HERE IT SHOWS AS FALSE
            notFound($rMatch);
        }
    }

}

?>

我做错了什么?谢谢!

I'm making a PHP script where I use a class boolean variable, but for some reason, when I want to set it to true, it doesn't work :/

<?php


class TinyFram {



    private $urlMatched;


    public function get($url, $method){

        $urlR = str_replace('/', '\/', $url);
        $urlR = '^' . $urlR . '\/?

What I'm doing wrong? thanks!

; if (preg_match("/$urlR/i", $reqURI, $rMatch)) { $this->urlMatched = true; // I SET IT TO TRUE HERE $method($rMatch); } if(!$this->urlMatched) { echo var_dump($this->urlMatched); // BUT HERE IT SHOWS AS FALSE notFound($rMatch); } } } ?>

What I'm doing wrong? thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天暗了我发光 2025-01-02 18:52:43

您的 preg_match 不匹配,因此该变量永远不会设置为 true。

Your preg_match is not matching, so the variable never gets set to true.

春风十里 2025-01-02 18:52:43

var_dump 位于一个条件中,用于检查 $this->urlMatched 是否为假值(false、0、''、null)。当然,此时它会显示为 false。

顺便说一句: var_dump 前面不需要有 echo。

The var_dump is in a conditional that check whether $this->urlMatched is a falsy value (false, 0, '', null). Of course it will show up as false then.

btw: var_dump does not need to have an echo in front of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文