Word & Character Counting - First PHP Script

Written something you are proud of, post it here.
Post Reply
User avatar
TerryHarvey
Posts: 20
Joined: Thu Mar 01, 2012 11:12 am

Word & Character Counting - First PHP Script

Post by TerryHarvey »

Hi! :)

This is my first PHP script that I created on my own without the aid of tutorials, so please do not be too harsh with the feedback. :)

This script allows you to count either characters or words depending on which one you select from the drop down list. The link contains v2 of the script. In v1, it only counted characters and it didn't save your words in the text area once you clicked submit.

You can modify and share all you want, the only thing I don't ask is that you don't redistribute it as your own.

I also added a nifty little design. :)

Please inform me if there is anything wrong with my coding. I expect there will be. :lol:

I would be extremely grateful if Jacek would offer some feedback. :)

Thanks a bunch! Download in the attachment below.

PS: I do know that there is an error in the title. I noticed it after I uploaded it. Doesn't really affect the script though. :P

WordCounter.zip
(1.69 KiB) Downloaded 218 times
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Word & Character Counting - First PHP Script

Post by jacek »

TerryHarvey wrote:This is my first PHP script that I created on my own without the aid of tutorials

I approve of that ! I always say that just having a go is the best way to learn, and nobody ever does !

As for your code, overall it's pretty good ! A couple of bad habits you might wan to try and stop before they get to engrained though.

[syntax=php]if (isset($_POST['counter']) && !empty($_POST['counter'])) {[/syntax]
Can just be

[syntax=php]if (!empty($_POST['counter'])) {[/syntax]
since empty() wont thrown an error if the variable is not defined.

[syntax=php]$check = $_POST['check'];[/syntax]
Personally this kind of variable renaming really bothers me. Most people don’t see it as a big deal. All you are doing is creating another variable for the sake of it really. You may as well just use $_POST['check'] where you use $check. The same goes for $_POST['counter'].

[syntax=php]<?php echo @$_POST['counter']; ?>[/syntax]
The way php handles the @ makes it pretty slow, more importantly it's the sign of a lazy developer ! You can one line an if statement here instead

[syntax=php]<?php if (isset($_POST['counter'])) echo $_POST['counter']; ?>[/syntax]
bit longer, but better in my opinion at least.

Also, I didn’t know the str_word_count() function existed and I've always counted words in a really stupid way. So you taught me somethign ;)
Image
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Word & Character Counting - First PHP Script

Post by bowersbros »

Aha, i have always counted words by counting the number of spaces.

Oh dear. Moderately embaressed now ;D

Anyhow, I like how the code looks.

Same points as jacek really, (except I didnt know about empty() not returning an error)

But never use @, it can always be replicated with quicker and better code, as all it does is hide errors. But you can code to detect errors, such as how jacek posted above.
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
TerryHarvey
Posts: 20
Joined: Thu Mar 01, 2012 11:12 am

Re: Word & Character Counting - First PHP Script

Post by TerryHarvey »

jacek wrote:I approve of that ! I always say that just having a go is the best way to learn, and nobody ever does !

Haha, thanks! I try. :lol:

Thanks a bunch for all your code advice, it really helped me out. :) I'll definitely try to snap out of those habits. :D

jacek wrote:Also, I didn’t know the str_word_count() function existed and I've always counted words in a really stupid way. So you taught me somethign ;)

Aha, i have always counted words by counting the number of spaces.

Oh dear. Moderately embaressed now ;D

Haha, no problem! :) It was pure coincidence that I found out about it. I use Sublime Text 2 which gives you suggestions as you type. For example, if you type "str", it'll come up with a drop down list with "strlen", "str_word_count" and a few others that I can't remember. I just experimented, found out what it did and decided to use it to make a word counter. :D

But never use @, it can always be replicated with quicker and better code, as all it does is hide errors. But you can code to detect errors, such as how jacek posted above.

Thanks for the tip! I'll certainly bear that in mind next time. :)

Also, I recently found out about the trim() function which can be used to remove whitespace (eg double lines, double spaces) I'll be using that if I decide to make another version. :)
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Word & Character Counting - First PHP Script

Post by bowersbros »

you should also use trim() around login details, incase somebody types a space by accident.

A space shouldnt count as a character in a password before or after the word(s).
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
TerryHarvey
Posts: 20
Joined: Thu Mar 01, 2012 11:12 am

Re: Word & Character Counting - First PHP Script

Post by TerryHarvey »

bowersbros wrote:you should also use trim() around login details, incase somebody types a space by accident.

A space shouldnt count as a character in a password before or after the word(s).

Good idea! :)
User avatar
JoshRoche
Posts: 2
Joined: Tue May 14, 2013 6:16 pm
Contact:

Re: Word & Character Counting - First PHP Script

Post by JoshRoche »

Very cool, I noticed it does not count numbers (I know it's called word counter) was this your intention or was it something you missed?

nice work
Post Reply