Solved

How to count the string length of a form variable using PHP?

Posted on 2012-04-04
7
227 Views
Last Modified: 2012-06-27
I have a simple form field <input type="text" name="test"> that submits a text string. The string is a 7 character combination of letters and numbers.  Using count() and strlen() contiuously produces '0' as a result.  What am I doing wrong?

Code at the top of my form processor:

<?php
$test= $_POST['test'];
$show_test_length = strlen ($test );
echo $show_test_length;

//I KNOW TEST IS BEING PASSED PROPERLY BECAUSE  THE FOLLOWING PRINTS THE VARIABLE BEING PASSED BY THE FORM

echo $test;
?>
0
Comment
Question by:evibesmusic
7 Comments
 
LVL 17

Assisted Solution

by:sonawanekiran
sonawanekiran earned 166 total points
ID: 37809563
Try printing like this

echo $_POST['test'] . "  ===> " . strlen($_POST['test']);
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 334 total points
ID: 37809582
Use this, and please post the output here so we can see what we are dealing with.  One step at a time!
error_reporting(E_ALL);
var_dump($_POST);

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37809613
The main problem was that you had a space between 'strlen' and '('.  That is not a valid way to do it, must not have a space between the function name and the '('.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHPstrlen</title>
</head>
<body>
<h1>PHP strlen</h1>
<?php
error_reporting(E_ALL);
var_dump($_POST);
if(isset($_POST['submit'])) {
$test= $_POST['test'];
$show_test_length = strlen($test );
echo "<br>".$show_test_length;

//I KNOW TEST IS BEING PASSED PROPERLY BECAUSE  THE FOLLOWING PRINTS THE VARIABLE BEING PASSED BY THE FORM

echo "<br>".$test;
}
?>
<br>
<form action="#" method="post">
<input type="text" name="test" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

Open in new window

0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 3

Expert Comment

by:gnemi
ID: 37809627
Whitespace doesnt matter in php.
<?php
	$_POST['test'] = "this is a test";
	$test= $_POST['test'];
	$show_test_length = strlen ($test );
	echo $show_test_length;
?>

Open in new window

The code above returns 14.

The code works, check your logs for php errors.

By the way this code can be shortened to just 1 line with:
<?php echo strlen($_POST['test']); ?>

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 334 total points
ID: 37809691
I expect what we will find here is that the output of var_dump() will show both the length and data type of the elements of the $_POST array.  Most likely the error is in another place, perhaps because of an incorrect variable assignment.  The use of error_reporting(E_ALL) will help us catch typographical errors and misspellings.

This sort of error can arise because the form used the GET method instead of the POST method.  And the PHP installation has "register_globals" set to "on" so the external variables are injected into the local scope. Thus $_GET['test'] (or $_COOKIE['test'] for that matter) causes $test to appear in the namespace and local scope.  When you couple that with the unfortunate fact that PHP suppresses Notice messages by default, you get a recipe for confusion.  Reliance on an undefined variable or array index is only a Notice-level condition.  So PHP may be (1) injecting variables into your script without notice and (2) letting your script rely on an undefined variable without notice.  Pretty thick-headed implementation, if you ask me.  But that is PHP!
0
 

Author Closing Comment

by:evibesmusic
ID: 37812570
@all:

Thank you for your assistance.  You suggestions to use var_dump() and Ray's last comment definitely taught me a few things. Dave's comment also brings to light the importance of clean PHP syntax which I need to improve upon.

The error was occurring due to where the variable call was placed in my script. The variable declaration was placed before a PHP redirect. The resulting redirect made my script pass over the variable call so it was technically not registering.  Using var_dump() made me see this.

I'm sorry to waste your time but, it was truly a learning experience and I do appreciate your coding examples and further explanation.

Cheers!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37812645
Thanks for the points. It's not a waste of time at all.  I sometimes think var_dump() is my best friend and I'm glad to share that friend with you ;-)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question