Hi, I be new to this, can anyone explain whats wrong?

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
newbieprogger
Forum Newbie
Posts: 9
Joined: Fri Dec 05, 2008 9:41 am

Hi, I be new to this, can anyone explain whats wrong?

Post 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;
}
 
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Hi, I be new to this, can anyone explain whats wrong?

Post by onion2k »

Is there a reason why you're not using the wordwrap() function?
newbieprogger
Forum Newbie
Posts: 9
Joined: Fri Dec 05, 2008 9:41 am

Post 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?
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: Hi, I be new to this, can anyone explain whats wrong?

Post 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 :-D
- 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.
newbieprogger
Forum Newbie
Posts: 9
Joined: Fri Dec 05, 2008 9:41 am

Re: Hi, I be new to this, can anyone explain whats wrong?

Post 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 :P and it gets fun when people help out :D

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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Hi, I be new to this, can anyone explain whats wrong?

Post 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.
Post Reply