Page 1 of 1
Hi, I be new to this, can anyone explain whats wrong?
Posted: Fri Dec 05, 2008 9:47 am
by newbieprogger
Hi all, I started learning Php language a week ago so I wanted to test some string manipulation... but I don't know what i've done wrong, help please!
The whole purpose of this is to break at every 75th character in string $texttest
Please give constructive criticism, I know very little of arrays...
Code: Select all
$texttest = "blabla -> and 300 more bla:s";
$stringlenght = strlen($texttest);
$maxstringlenght = 75;
$rowbreakresult = array();
$amount = $stringlenght/$maxstringlenght;
$rows = round($amount);
$firstline = 0;
if( $stringlenght > $maxstringlenght ) {
for( $firstline; $firstline <= $rows; $firstline++) {
$fm = $firstline*$maxstringlenght;
$tempstr = substr($texttest, $fm, $maxstringlenght);
$tempstr = $tempstr + "<br>";
$rowbreakresult["$firstline"] = $tempstr;
}
}
$int = 0
while( $rowbreakresult[$int] == true ) {
echo $rowbreakresult[$int];
$int = $int+1;
}
Re: Hi, I be new to this, can anyone explain whats wrong?
Posted: Fri Dec 05, 2008 10:12 am
by onion2k
Is there a reason why you're not using the wordwrap() function?
Posted: Fri Dec 05, 2008 10:19 am
by newbieprogger
eh, what is that? oh, looked it up at php.net. So basicly I've been trying to create something that already exists, and all it really needs is one line saying wordwrap($testtext, $maxstringlenght, "<br />"); ?
In any case, do you see anything wrong with the arrays?
Re: Hi, I be new to this, can anyone explain whats wrong?
Posted: Fri Dec 05, 2008 10:24 am
by mintedjo
Hello!
I found a few things that don't look right to me:
- using round($amount) means that $amount could be rounded up or down depending on the value of $amount. I think here you should be using floor($amount) to make sure the number is rounded down.
- the whole chunk of code is only executed if the string is longer than 75 chars, if not then you dont put any values into the array and it breaks later on

- You assign values to the array using $rowbreakresult["$firstline"] = $tempstr; Putting the "" around $firstLine means that the array key is a string, which isnt the same as a number which you use later on to access it when you use echo $rowbreakresult[$int]; I'm not sure if this will cause an error in php but it's not a good idea.
- You missed a semi colon at the end of one of your lines just before the while loop
- a while loop is not really appropriate here. usually when iterating through an array its better to use a for loop or a foreach loop.
I'm sure somebody else will have something else to say but I hope some of that was useful.
EDIT: Just read teh follow up posts... A lot of things already exist and usually it's not worth writing your own version because the other ones are tried and tested and proven to work. This kind of thing is still good if you are just starting to learn because it helps you grasp some of the basics that you will be using often later on.
Re: Hi, I be new to this, can anyone explain whats wrong?
Posted: Fri Dec 05, 2008 4:47 pm
by newbieprogger
Thanks mintedjo! and to the rest of you!

We all have to start somewhere right? I find it easier to learn when doing things like this

and it gets fun when people help out
EDIT: Might I ask a small question:
I read somewhere that quotation "" marks means the variable is taken as its ... eh.. meaning as in what is assigned to be. if you instead use ' ' it will be taken literally, but if you use variables in array keys, is there a difference?
Re: Hi, I be new to this, can anyone explain whats wrong?
Posted: Fri Dec 05, 2008 5:27 pm
by requinix
newbieprogger wrote:I read somewhere that quotation "" marks means the variable is taken as its ... eh.. meaning as in what is assigned to be. if you instead use ' ' it will be taken literally, but if you use variables in array keys, is there a difference?
The
manual is a great place to look for answers.