How to get all data from a server variable in PHP? - php

How would I get all the data from fields that have been posted or requested using get in PHP?
e.g
echo $_GET[*];

var_dump ($_GET);
or
print_r ($_GET);
or
echo (json_encode ($_GET));
or
foreach ($_GET as $key => $val)
{
echo ($key . ', ' . $val);
}
or any number of other methods

You can use print_r:
<?php
...
print_r($_GET)
...
?>

If you want to display all the values of an array or an object, you can use print_r. You'll want to use it inside <pre> tags to get all the indentation and new lines.
<pre>
<?php print_r($_GET); ?>
</pre>

extract($_GET);
fun with extract!

Related

Loop through each element under "Data"

I'm using Kucoin's API to get a list of coins.
Here is the endpoint: https://api.kucoin.com/v1/market/open/coins
And here's my code:
$kucoin_coins = file_get_contents('https://api.kucoin.com/v1/market/open/coins');
$kucoin_coins = json_decode($kucoin_coins, true);
print_r($kucoin_coins);
I can see how to target one coin like so:
echo "name: " . $kucoin_coins['data'][0]['name'];
But I can't see how to loop through them.
How can I loop through each of the "coins" returned here? They are under the "data" part that is returned. I'm sorry, I'm just not seeing how to do it right now. Thank you!
You can loop through the decoded elements using the foreach command:
foreach ($kucoin_coins['data'] as $coin) {
//do your magic here.
}
But I usually prefer using json_decode($kucoin_coins) rather than the one for arrays. I believe this:
$item->attribute;
Is easier to write than this one:
$item['attribute'];
foreach($kucoin_coins['data'] as $data) {
echo $data['name']."\n";
}
You can loop through your data using foreach() like this
<?php
$kucoin_coins = file_get_contents('https://api.kucoin.com/v1/market/open/coins');
$kucoin_coins = json_decode($kucoin_coins, true);
print '<pre>';
print_r($kucoin_coins);
print '</pre>';
foreach($kucoin_coins['data'] as $key=>$value){
echo $value['name']. "<br/>";
}
?>
See DEMO: http://phpfiddle.org/main/code/q6kt-dctg

Syntax error for printing an array

I'm attempting to print an array of php elements embedded in html
If I input
echo '<strong>'.$s[firstname].' '.$s[lastname].'</strong><div class="moreinfo"><p><small>'.$s[role].' of '.$parent'.</small></p></div></li>';
I get a result that says something like "Chris James Parent of Array
but if I attempt to print the array with a foreach as so
echo '<strong>'.$s[firstname].' '.$s[lastname].'</strong><div class="moreinfo"><p><small>'.$s[role].' of '.
foreach($parent as $p){
echo $p.' ';
}
.'</small></p></div></li>';
The program crashes completely. I would assume that I'm doing something syntactically incorrect, but I can't spot the issue. Is there a simply way to print the elements in thearray that would avoid the crash?
Thanks in advance!
You concatenate output with . not additional PHP statements:
echo '<strong>'.$s[firstname].' '.$s[lastname].'</strong><div class="moreinfo"><p><small>'.$s[role].' of ';
foreach($parent as $p){
echo $p.' ';
}
echo '</small></p></div></li>';
However you can just implode $parent:
echo '<strong>'.$s[firstname].' '.$s[lastname].'</strong><div class="moreinfo"><p><small>'.$s[role].' of '.implode(' ', $parent).'.</small></p></div></li>';

How do you output several lines of the $_POST variable

How do I output several lines of the $_POST variable ?
When I keep outputting the result I only get the last $_POST variable
Thanks for helping
If you want more detailed information about what's being stored in $_POST you can use
var_dump($_POST);
This will return the key, contents and type of each entry
print_r($_POST);
This will display the key and contents of each entry.
If you want to cycle through the contents of $_POST and format the output you can use.
foreach($_POST as $key => $value){
print $key.' is '.$value.'<br />';
}
What do you mean by "the last $_POST variable"? Please provide the code snipped and the desired output format.
echo $_POST; -> "Array"
print_r ($_POST); -> detailed output of the array's contents.
You mean something like this?
foreach ($_POST as $key => $value)
{
echo $key . ': ' . $value . '<br />';
}

How to grab all variables in a post (PHP)

How to grab all variables in a post (PHP)?
I don't want to deal with $_POST['var1']; $_POST['var2']; $_POST['var3']; ...
I want to echo all of them in one shot.
If you really just want to print them, you could do something like:
print_r($_POST);
Alternatively, you could interact with them individually doing something like:
foreach ($_POST as $key => $value) {
//do something
echo $key . ' has the value of ' . $value;
}
but whatever you do.. please filter the input. SQL Injection gives everyone sleepless nights.
If you want the POST values as variables in your script, you can use the extract function, e.g.
extract ( $_GET );
or
extract ( $_GET, EXTR_IF_EXISTS );
There are several flags you can use (check the manual); this one restricts extraction to variables already defined.
You can also use the import_request_variables function.
Cheers
Jeff
Use:
var_dump($_POST);
echo '<pre>';
print_r($_POST);
echo '</pre>';
Use this function in a loop.
extract ( $_GET );

How to print out the keys of an array like $_POST in PHP?

Maybe the code looks like something like this:
foreach(...$POST){
echo $key."<br/>;
}
var_dump($_POST);
or
print_r($_POST);
You might insert a pre tag before and after for the clearer output in your browser:
echo '<pre>';
var_dump($_POST);
echo '</pre>';
And I suggest to use Xdebug. It provides an enchanted var_dump that works without pre's as well.
See the PHP documentation on foreach:
http://php.net/manual/en/control-structures.foreach.php
Your code would look something like this:
foreach ($_POST as $key=>$element) {
echo $key."<br/>";
}
Tested one liner:
echo join('<br />',array_keys($_POST));
If you want to do something with them programmatically (eg turn them into a list or table), just loop:
foreach ($_POST as $k => $v) {
echo $k . "<br>";
}
For debugging purposes:
print_r($_POST);
or
var_dump($_POST);
And if you want full coverage of the whole array, print_r or even more detailed var_dump
$array = array_flip($array);
echo implode('any glue between array keys',$array);
Or you could just print out the array keys:
foreach (array_keys($_POST) as $key) {
echo "$key<br/>\n";
}
Normally I would use print_r($_POST).
If using within an HTML page, it's probably worth wrapping in a <pre> tag to get a better looking output, otherwise the useful tabs and line breaks only appear in source view.
print_r() on PHP.net

Categories