I would like to know how to avoid the OUTPUT of var_dump($abc); from showing in the output screen and instead use it to feed a variable. Well, I am trying to put the output of <form><select name="study_Class"><option value="word1 (space) word2">word1 word2</option></form> into php program as an array as shown below (as its multiple choices) and want to use the words seperated by space as in the <value> attribute. But don't want the output of var_dump($); showing in the screen. Is there a way? (Below is my code)

<form action="abc.php" method="post">
    <select name="study_class[]" multiple>
    <option value="Class 1">CLASS 1</option>
    <option value="Class 2">CLASS 2</option>
    <option value="Class 3">CLASS 1</option>
    <option value="Class 4">CLASS 2</option>
    </select>
    </form>
    abc.php
    <?php
    #get data
    $study_class=implode(', ', $_POST['study_class']);
    var_dump($study_class);
    ?>
    <html>
    .
    .
    <P> Study Classes Chosen are: <?php echo $study_class; ?></P>
    .
    </html>

So on top of the OUTPUT screen first the
string(31) Class 1, Class 2
comes.
Then my echo response comes:
Study Classes Chosen are: Class 1, Class2
HOW to avoid the string.....
Also I will be feeding the $study_class as output in mail.

Thanks.

Dani AI

Generated

var_dump is a debugging helper that writes information straight to the output stream; it does not return a string. That is why the page showed something like string(31) Class 1, Class 2 before the normal output. As pointed out, use echo/printf for user-facing output. ’s idea of assigning the result of var_dump to a variable will not work because var_dump does not return the dumped text.

Two practical ways to get a text representation that can be stored or mailed:

// returns a human-readable string (no browser output)
$dumpText = print_r($_POST['study_class'], true);

// returns a parsable PHP-style representation
$exportText = var_export($_POST['study_class'], true);

If the exact var_dump format is required for logging, wrap the dump call with output buffering (ob_start()/ob_get_clean()) to capture its output rather than letting it print to the page.

Other relevant notes and quick checks

  • Option values containing spaces are sent intact (e.g. "Class 1" stays "Class 1"); no special quoting is needed inside the value attribute.
  • For a compact CSV-like string, join/implode the array (this was already working in the original code) and use that in mail bodies.
  • Escape HTML context with htmlspecialchars(..., ENT_QUOTES, 'UTF-8') before echoing to a page to avoid XSS. For emails, sanitize/validate as appropriate.
  • Remove or disable debugging output on production (display_errors off) and prefer logging (error_log or a file) for diagnostics.

These points tie back to the thread: replacing var_dump with direct output (echo), capturing a textual representation with print_r/var_export, or using buffering to capture var_dump will avoid the unwanted debug string appearing on the page.

Recommended Answers

All 3 Replies

Confused..

By using var_dump, it just dumps whatever is stored in a particular varialbe and then outputs it to the screen (or whatever)! In order to display the output, you should just use echo/printf?

<form action="abc.php" method="post">
    <select name="study_class[]" multiple>
    <option value="Class 1">CLASS 1</option>
    <option value="Class 2">CLASS 2</option>
    <option value="Class 3">CLASS 1</option>
    <option value="Class 4">CLASS 2</option>
    </select>
    </form>
    abc.php
    <?php
    #get data
    $study_class=implode(', ', $_POST['study_class']);
    //var_dump($study_class);
    echo $study_class; // this would output it.
    ?>
    <html>
    .
    .
    <P> Study Classes Chosen are: <?php echo $study_class; ?></P>
    .
    </html>

I don't know if that's what you mean! Or whether you want to actually store the result of var_dump inside a variable.

commented: understood question better +5

$myvar = var_dump($study_class);?

to have better control use foreach()

foreach($_POST['study_class'] as $key=>$value){//for each variable in the array $study_class
    //do the following ($key == the key of this array entry, $value == value of entry in array)
    echo $value."<br/>\r\n";
}

$key will just be 0,1,2,3 etc in your code though

Phorce...thanks for the UnConfusion..
i have done away with var_dump($study_class); and i am getting output as desired 'word (space) word' eg. 'Class 1'. I thought output will neglect characters after space.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.