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

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;
?>
evibesmusicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kiran SonawaneProject LeadCommented:
Try printing like this

echo $_POST['test'] . "  ===> " . strlen($_POST['test']);
0
Ray PaseurCommented:
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
Dave BaldwinFixer of ProblemsCommented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

gnemiCommented:
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
Ray PaseurCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evibesmusicAuthor Commented:
@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
Ray PaseurCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.