Solved

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

Posted on 2012-04-04
7
225 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
Comment Utility
Try printing like this

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

Assisted Solution

by:Ray Paseur
Ray Paseur earned 334 total points
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:gnemi
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 334 total points
Comment Utility
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
Comment Utility
@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 108

Expert Comment

by:Ray Paseur
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now