简体   繁体   中英

PHP if statement with strlen (not working as expected)

<?php if(strlen($r['body'] <= '74')): ?>
<?php echo $r['body']; ?>
<?php else: ?>
<?php echo substr($r['body'], 0, 74) ."..."; ?>
<?php endif; ?>

when $r['body'] == more than 74 chars everything works as expected but as soon as $r['body'] == less than 74 chars it still attaches "..."

I can work out what i have been doing wrong?

您没有关闭正确的strlen,也是74是一个不应在引号内的整数试试:

if(strlen($r['body']) <= 74):

Try this

<?php if(strlen($r['body'])  <= 74 ): ?>

strlen() return the length of string which you can test in if

you have the parenthesis in the wrong place. try this:

<?php if(strlen($r['body']) <= '74'): ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM