Solved

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

Posted on 2012-04-04
7
226 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 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
test if query has no results 2 19
html input type 3 22
datetime in sql 6 28
Before I get too far.. best way to save dates data 4 16
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 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