简体   繁体   中英

php strlen range function

Im trying to write an if function which checks the width of an image, and then apply a css class.

I want the function to work like this

if image is in an range from 150px to 189px, apply css class "span-4"

190px to 229px: css class "span-5"

230px to 269px: css class "span-6"

I have tried like this:

list($width, $height, $type, $attr) = getimagesize($article_image);

if(strlen($width) < 189 && strlen($width) > 150) { $cssClass = "span-4"; }
if(strlen($width) < 229 && strlen($width) > 190) { $cssClass = "span-5"; }
if(strlen($width) < 269 && strlen($width) > 230) { $cssClass = "span-6"; }

That does not work. Do anyone see what Im doing wrong?

Edit: Added the function to explaine where Im getteing the $width variable from

It's probably because you're treating $width like a string, and getting its length. Try treating it like a number instead.

if(($width < 189) && ($width > 150)) { $cssClass = "span-4"; }

strlen gets the string length of the $width variable. I don't think you want to do that. You most probably want to lose strlen() from every $width.

first of all calculate the string's length only once and put it into a variable. Secondly use else if on the second and third statements.

strlen is, like the function name indicate, for strings.

I'm not sure I correctly understood what you want to do, but if you have an image file and you want to check it's size, you can use getimagesize .

If you already have the image's width in the $width variable, you must do your checks like this :

if($width > 150 && $width < 190) { $cssClass = "span-4"; }
else if($width < 230) { $cssClass = "span-5"; }
else if($width < 270) { $cssClass = "span-6"; }

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