nplyley
By:
nplyley

Why is PHP echoing post with string count?

March 7, 2016 718 views
Apache PHP

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.

2 comments
  • Can you share your code here.

  • 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"

1 Answer

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)

Have another answer? Share your knowledge.