php检查变量长度是否等于一个值
需要检查$message长度是否小于等于7个字符,如果是,则执行操作A,如果不是,则执行操作B。此语法正确吗?我想我做错了什么php检查变量长度是否等于一个值,php,Php,需要检查$message长度是否小于等于7个字符,如果是,则执行操作A,如果不是,则执行操作B。此语法正确吗?我想我做错了什么 <?php if (strlen($message) <= 7) { echo $actiona; } else { echo $actionb; } ?> 很好。例如,让我们运行以下命令: <?php $message = "Hello there!"; if (strlen($message) <= 7){
<?php
if (strlen($message) <= 7) {
echo $actiona;
} else {
echo $actionb;
}
?>
很好。例如,让我们运行以下命令:
<?php
$message = "Hello there!";
if (strlen($message) <= 7){
echo "It is less than or equal to 7 characters.";
}
else
{
echo "It is greater than 7 characters.";
}
?>
它将打印:“大于7个字符。”您收到了什么错误消息 我会检查一下,当您事先设置
$message
时,您没有拼写错误或使用了不正确的大写字母(请记住php是区分大小写的)。没关系
但是您应该使用长php标记(短标记可以被禁用;通常是这样):
(如果文件只包含PHP,则结束标记是可选的)如果使用三元运算符(?:),则可能还需要使用PHP速记 例如,而不是:
你可以这样写:
有关三元运算符的信息,请参阅。我在www上找到了这个设置的“技巧”,我不记得在哪里。 首先尝试像这样运行脚本,然后取消注释第二行$message=。。写另一个字符串长度
<?php
$actiona="ACTIONA";
$actionb="ACTIONB";
$message="1234567";
//$message="12345678";//try to comment /uncomment these
if (!isset($message{7})) {
echo $actiona;
} else {
echo $actionb;
}
?>
当然
<?php
$actiona="ACTIONA";
$actionb="ACTIONB";
$message="1234567";//try to comment /uncomment these
//$message="12345678";
echo !isset($message{7}) ? $actiona : $actionb;
?>
祝你好运!我们都喜欢Php php块是否需要以“为什么你认为自己做错了什么”开头?它不工作吗?@seth'我粘贴了脱机文件中的工作代码,我以为我在修复分号问题之前上传了该文件。谢谢大家的帮助。这是我在stackoverflow上看到的所有推荐代码中最好的代码。资料来源:对它们进行了测试
<?php
$actiona="ACTIONA";
$actionb="ACTIONB";
$message="1234567";//try to comment /uncomment these
//$message="12345678";
echo !isset($message{7}) ? $actiona : $actionb;
?>