Home > database >  PHP not echoing variables when print_r does
PHP not echoing variables when print_r does

Time:05-10

I'm not sure exactly what is going on but when a user clicks a submit button on an html page and the php script runs I get the print out from the print_r statement, but if I print a variable out with the echo statement, the value isn't returned. I've just ported the webpage and code over to a new computer, would that have something to do with it? Here is my sample php code:

<?php
print_r($_POST);

$x = $_POST[minAcres];
echo $x;

?>

Everything was working fine until I moved the code. Any thoughts why this would be happening? Here is a screenshot of the output from the print_r command.

enter image description here

CodePudding user response:

Here is an example I regress back to the original answer of needing just quotes around minAcres. As a proof of concept i filled in post array with the beginning few values from your screen shot. minAcres = 0. Output = '0'. Maybe your output has been formatted to not show the 0 but show blank/empty

<?php

$_POST = array(
'selectCountries' => array('Columbia'),
'startDate' => '2001-01-01',
'minAcres' => 0
);
print_r($_POST);

$x = $_POST['minAcres'];
echo $x;

?>

Output:

Array
(
[selectCountries] => Array
    (
        [0] => Columbia
    )

[startDate] => 2001-01-01
[minAcres] => 0
)
0

Simple syntax error

$x = $_POST['minAcres'];

quotes needed around array's key.

  •  Tags:  
  • php
  • Related