无法在 PHP 中将变量设置为 true
我正在制作一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 preg_match 不匹配,因此该变量永远不会设置为 true。
Your preg_match is not matching, so the variable never gets set to true.
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.