Echo an $_POST in PHP - php

How can I print a $_POST?
Example:
echo $_POST['data'];
This returns nothing...

You can also wrap your code with <pre> tags to make your array prints out nicer instead of just 1 continuous line. A trick that was shown by a member on this site.
<pre>
<?php var_dump($_POST); ?>
</pre>

Your code is correct.
You can use either:
var_dump($_POST);
or
print_r($_POST);
to print out the entire POST array for debugging.

You can only show the values of keys that exist. array_keys() returns an array containing the keys that exist in the array. If there is no output for a key despite the fact that the key exists then the array may contain an empty value for that key.

Related

print_r values of an Array in $_POST

I have got a question regarding printing an array of checkboxes in the $_POST
When I do:
echo '<pre>';
print_r($_POST);
echo '</pre>';
I can see the Checkboxes which I send through the $_POST
How am I able to read only the checkboxes?
This is what I currently have:
print_r($_POST['checkboxes[]']);
But this doesn't seem to work
If your checkboxes actually have their name='checkboxes[]', then you can access their POST value by doing print_r($_POST['checkboxes']);. You do not have to include the square brackets when referencing the name of the POST value, the brackets are only there make all of the same named elements post as an array.

Dont display varaibile if it contains no value (PHP|

I am using SOAP to get some data from other service, i get XML and convert and display it with PHP. The thing is sometimes there is no value for it in XML file and i dont know how to display some message like "This thing was not set", instead of that i am just getting notice with
Notice: Array to string conversion in \data2.php on line 636
Array
What i tried:
if(isset($claim ['vehicle']['engine-cc']))
echo var_dump($claim ['vehicle']['engine-cc']);
output: array(0) { }
After that i try something like:
if(isset($claim ['vehicle']['engine-cc']))
echo var_dump(isset($claim ['vehicle']['engine-cc']));
Output: bool(true)
So it looks i am doing things bad, can you gimme some advise how can i fix it?
p.s. I know i can stop displaying errors and notices but its not the way how i want to "fix" it
you no need to echo after var_dump already have var_dump(used for printing array) or may be you have not an array for $claim ['vehicle']['engine-cc'] it's a string
var_dump($claim ['vehicle']['engine-cc']); // for array
echo $claim ['vehicle']['engine-cc'] // for string
returning bool(true) cause you have isset()(will return boolean true/false)
var_dump(isset($claim ['vehicle']['engine-cc']));
For printing array values you only need :-
var_dump($claim ['vehicle']['engine-cc']); or var_dump($claim);
will return you complete array output
for any check empty values try
if(!empty($claim ['vehicle']['engine-cc']))
var_dump($claim ['vehicle']['engine-cc']);
You can do it by simply echo ing the message.
`if( ! isset($claim ['vehicle']['engine-cc']))
echo 'engine-cc - This thing was not set';`
You need to use option "SOAP_SINGLE_ELEMENT_ARRAYS" most likely.
By default SOAP returns empty or a single value for a SOAP attribute as a string, not an array. Adding option SOAP_SINGLE_ELEMENT_ARRAYS into SOAP client request would force to return the value as an empty ARRAY rather than empty string. See example here.

PHP - output values of a variable

I am a newbie to php. I am working on a existing wordpress site. I want to print the values of a variable $post as output. What is the equivalent of Java's System.out.print in php.
While googling i found the same, system.out.print for php but its not working.
--
Thanks
You can use echo to print something: docs
$_POST is an array, if you want to examine its value for debug purposes use var_dump (docs):
var_dump($_POST);
To access array's elements, use square brackets: $_POST['index']. PHP arrays may be associative, which means they are indexed not only by numbers, but also by strings. $_POST is a pure associative array, indexed only with strings.
To print POST parameter called username use this code:
echo $_POST['username'];
or
echo($_POST['username']);
Both will work because echo is not a function, but a language construct (see docs linked above).
printf("var = %s", $var);
or
echo $var;
or (for objects, arrays, etc)
print_r($_POST);
or
var_dump($_POST);
Check php.net for more!
PHP's echo construct is probably what you want.
<?php echo $_POST['variable']; ?>
where variable is the name of the form element.

can not call array values from php ini file using parse_ini_file

I have tried reading parameters from php ini files using parse_ini_file
Here is my code
$param_array=parse_ini_file("test.ini");
print $param_array[0];
print $param_array[1];
Above code returns nothing
test.ini file contains,
[names]
me = Robert
you = Peter
[urls]
first = "http://www.example.com"
second = "http://www.w3schools.com"
Now i need to call the Robert & Peter
If you'd have rather consulted the Manual Pages instead of w3fools, you'd have seen a reliable example where it's shown that the returned array is associative, that is, the names of the properties are the keys of the array.
So instead of
$param_array[0]
you should use
$param_array['me']
to access "Robert"
parse_ini_file returns an associative array. You are trying to access the fields in your array using numerical indices, but they aren't defined.
Try accessing those fields using the names you gave them in the ini-file as the key:
echo $param_array['me']; // will yield Robert
Helpful tip: investigate the structure of arrays and variables to find out what they look like using print_r or var_dump
Following code worked!
print $param_array["me"];
print $param_array["you"];

Variable in $_POST returns as string instead of array

In my form i have fields with name photoid[] so that when sent they will automatically be in an array when php accesses them.
The script has been working fine for quite some time until a couple days ago. And as far as i can remember i havent changed any php settings in the ini file and havent changed the script at all.
when i try to retrieve the array using $_POST['photoid'] it returns a string with the contents 'ARRAY', but if i access it using $_REQUEST['photoid'] it returns it correctly as an array. Is there some php setting that would make this occur? As i said i dont remember changing any php settings lately to cause this but i might be mistaken, or is there something else i am missing.
I had the same problem. When I should recieve array via $_POST, but var_dump sad: 'string(5) "Array"'. I found this happens, when you try use trim() on that array!
Double check your code, be sure you're not doing anything else with $_POST!
Raise your error_reporting level to find any potential source. It's most likely that you are just using it wrong in your code. But it's also possible that your $_POST array was mangled, but $_REQUEST left untouched.
// for example an escaping feature like this might bork it
$_POST = array_map("htmlentities", $_POST);
// your case looks like "strtoupper" even
To determine if your $_POST array really just contains a string where you expected an array, execute following at the beginning of your script:
var_dump($_POST);
And following for a comparison:
var_dump(array_diff($_REQUEST, $_POST));
Then verifiy that you are really using foreach on both arrays:
foreach ($_POST["photoid"] as $id) { print $id; }
If you use an array in a string context then you'll get "Array". Use it in an array context instead.
$arr = Array('foo', 42);
echo $arr."\n";
echo $arr[0]."\n";
​
Array
foo
$_POST['photoid'] is still an array. Just assign it to a variable, and then treat it like an array. ie: $array = $_POST['photoid'];
echo $array[0];

Categories