Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2012-04-04
7
Medium Priority
?
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 17

Assisted Solution

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

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

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1336 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 84

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 111

Accepted Solution

by:
Ray Paseur earned 1336 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 111

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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.
Suggested Courses

610 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