Question

Why is PHP echoing post with string count?

For some reason when I use the POST method for a from lets says the value to name is Bob. When it echos the $_POST['name'] is says string(3) “Bob” rather then just printing Bob. Even if I do a var dump it still shows up the same way.

Subscribe
Share

My code for the form is

<form method="post" action="savePage.php">
<input type="text" name="name" value="Test">
<input type="submit" value="Save">
</form>

The only other code is my doctype and body and stuff like that but nothing in the head.

Here is the savePage.php

<?php
echo $_POST['name'];
?>

That’s it because I was having other problems so I tested it doing the simplest thing and my code come out like this:

string(4) “Test”

Can you share your code here.


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

The post method for the PHP echoing is as below -

<?php
   if( $_POST["name"] || $_POST["age"] ) {
      if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) {
         die ("invalid name and name should be alpha");
      }
      echo "Welcome ". $_POST['name']. "<br />";
      echo "You are ". $_POST['age']. " years old.";
      
      exit();
   }
?>
<html>
   <body>
   
      <form action = "<?php $_PHP_SELF ?>" method = "POST">
         Name: <input type = "text" name = "name" />
         Age: <input type = "text" name = "age" />
         <input type = "submit" />
      </form>
   
   </body>
</html>

The output will be Name: Age: Submit (tab)