Error in Loop-HELP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Error in Loop-HELP

Post by tecktalkcm0391 »

Code: Select all

$title = "Soft Drop Shadows";
	$x_size = 0;
	$x_length = strlen($title);
	for($i = 0, $i <= "$x_length"; $i++){
		$x_size = ($x_size + 18);
	}
Why do I get an error, I want it to be what ever the title length is to add 18 to $x_size.
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

Code: Select all

$title = "Soft Drop Shadows";
        $x_size = 0;
        $x_length = strlen($title);
        for($i = 0; $i <= $x_length; $i++){
                $x_size += 18;
        }
that should work. you had $i=0, instead of $i=0;
User avatar
phpmash
Forum Newbie
Posts: 24
Joined: Mon Oct 31, 2005 6:41 am
Location: Kerala,India
Contact:

Post by phpmash »

strlen returns an integer value
so it is not necessary to bound" in $x_length
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post by nathanr »

hmm..

Code: Select all

$title = "Soft Drop Shadows"; 
        $x_size = 0; 
        $x_length = strlen($title); 
        for($i = 0; $i <= $x_length; $i++){ 
                $x_size += 18; 
        }
this would give you an $x_size of 324 ( which is 18*18 ), however title legnth is only 17.. so

Code: Select all

$title = "Soft Drop Shadows"; 
$x_size = strlen($title)*18;
will give you the correct value of 306 ( 17*18 )

:oops:
Post Reply