Page 1 of 1
Word & Character Counting - First PHP Script
Posted: Fri Mar 02, 2012 7:51 pm
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.
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.
Re: Word & Character Counting - First PHP Script
Posted: Sat Mar 03, 2012 1:08 am
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.
if (isset($_POST['counter']) && !empty($_POST['counter'])) {
Can just be
if (!empty($_POST['counter'])) {
since empty() wont thrown an error if the variable is not defined.
$check = $_POST['check'];
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'].
<?php echo @$_POST['counter']; ?>
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
<?php if (isset($_POST['counter'])) echo $_POST['counter']; ?>
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
Re: Word & Character Counting - First PHP Script
Posted: Sat Mar 03, 2012 9:34 am
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.
Re: Word & Character Counting - First PHP Script
Posted: Sat Mar 03, 2012 11:45 am
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.
Thanks a bunch for all your code advice, it really helped me out.
I'll definitely try to snap out of those habits.
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.
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.
Re: Word & Character Counting - First PHP Script
Posted: Sat Mar 03, 2012 12:10 pm
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).
Re: Word & Character Counting - First PHP Script
Posted: Sat Mar 03, 2012 12:50 pm
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!
Re: Word & Character Counting - First PHP Script
Posted: Tue May 14, 2013 6:41 pm
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