I have been given this php code
echo "<pre>";
print_r(htmlentities($data));
echo "</pre>";
I am really not into php programming and I really don't know where am I gonna see the output of the print_r function. Such a petty question but I hope I could get a decent answer. Thanks!
As per the PHP manual: print_r: 'Prints human-readable information about a variable'. Assuming that $data is an array, you will have an output of that data as plain text - it is basically var_dump (the main difference is that var_dump will also output types).
$data = array('item', 'name', 'form');
echo "<pre>";
print_r($data);
echo "</pre>";
Output:
Array
(
[0] => item
[1] => name
[2] => form
)
However, keep in mind htmlentities is looking for a string; and if you input an array you will get an error. So if you do have a string it would be okay to use htmlentities
<?php
$data = 'Hello World!';
echo "<pre>";
print_r(htmlentities($data));
echo "</pre>";
?>
Output:
Hello World!
It goes to PHP's output buffer. If you are running the script from a command line client the output will display on the terminal but typically PHP is run on a web server upon the request of a user. In the latter case, if your script was named 'example.php' and accessible at http://example.com/example.php visiting that URL would show the output in the web browser.
Should display right at the top of your page you're running. print_r displays an arrays contents. You're better off doing this:
$array = ("cat","dog");
echo "<xmp>";
print_r($data);
echo "</xmp>";
So in the browser navigate to page.php, it'll display your array. Now if you're in command line doing this it'll come to the output buffer but that depends on your setup.
Related
I'm trying to retrieve a URL string from some json code.
Here is the json code
{"files":["www.example1.com"],"previews":["www.example1preview.com"],"meta":{},"userId":"guest","product":{"id":"2335","name":"standard"},"type":"u"}
Looking at what I've seen in the PHP manual I'm trying to retrieve previews like this.
<?php
ob_start();
include('getjson.php');
$meta_value_json = ob_get_clean();
echo $meta_value_json;
$meta_value_json = json_decode($meta_value_json);
print $meta_value_json->{'previews'};
?>
This doesn't seem to output the value however.
By experimenting with php -a command on terminal, I've put your json into json_decode and managed to get your link by just doing:
print $meta_value_json->previews[0];
The only reason to use print $meta_value_json->{'previews'}; at least according to php documentation is if you want an object as output and the key trying to retrieve is numerical or of a type that is not supported by php.
By experimenting a bit further, the reason that print $meta_value_json->{'previews'}; fails is because print expects a string, in our case here previews is an array. Therefore if you do print $meta_value_json->{'previews'}[0]; it will also work as expected.
You need to get the value from your decoded json like this: $class->parameter.
Knowing that previews is an array, you will also need to choose a specific element from it to print ( I got the first one ):
<?php
ob_start();
include('getjson.php');
$meta_value_json = ob_get_clean();
echo $meta_value_json;
$meta_value_json = json_decode($meta_value_json);
print $meta_value_json->previews[0]; /// get the specific value
?>
How can I in an API echo results in a new array?
example:
echo implode('[]',$resultArray);
result:
$resultArray
requested result:
[$resultArray]
Since you're implementing an API the best pracrice will be to output your results in a format that's readable and understandable by multiple programming languages. JSON is the way to go. Read about it here and here
To echo an array, or anything not just an array, in PHP in a JSON format use:
echo json_encode($data);
where $data holds the output
echo json_encode($resultArray);
Assuming you want to keep the data in the response an array to the requester.
If not, your example will work but I'd suggest something easy to parse on the requester's side like a CSV value using commas: echo implode(',', $resultArray);
if you're looking to output the string '[$resultArray]', it'll be something like echo '[$'.print_var_name($resultArray).']'. Not sure why you'd need it like this, but your question is a little bit vague/confusing on exactly what you're looking for.
You could use:
echo "<pre>"; var_dump($array); echo "</pre>";
This way you will get a nice preformatted result of var_dump.
try this
$array = json_decode(json_encode($resultArray), true);
or
Print_r();
I'm using image uploader plugin which is posting data in multi dimension array like shown below
Array
(
[file] => {"input":{"name":"KE6Cc2ea2b584.jpg",
"type":"image/jpeg",
"size":61224,
"width":800,
"height":643},
"output":{"width":320,"height":180,
"image"
:"data:image/jpeg;base64,/9j/just an example of base 64 encoded image"},
"actions":{"crop":{"x":0,"y":96.5,"height":450,"width":800 },
"size":{"width":320,"height":320}}}
)
Now i tried lot to echo these post values like shown below
echo $_POST['file']; worked but brought all the data
echo $_POST['file']['output']; didn't worked
echo $_POST['file']['output']['image']; didn't worked
How can i get the value of these post
When trying to echo out any sort of values while debugging, it's a good idea to see exactly what you're getting via var_dump. For example, instead of your echo lines, I'd simply put:
echo "<pre>";
var_dump($_POST['file']);
Check your output there and it should show you exactly what keys you have to work with.
Also, depending on how your plugin is sending the data, you may not get any actual keys. According to your comment about $_POST['file'] printing all the output, it seems as though it's still a string. Try putting the JSON into an object, like so:
$test = json_decode($_POST['file']);
Or, if you'd prefer an associative array as mentioned:
$test = json_decode($_POST['file'], true);
Then get the output of $test:
echo "<pre>";
var_dump($test);
This question already has answers here:
Is there a pretty print for PHP?
(31 answers)
Closed 7 months ago.
How do i print an Array readable? print_r($array); and var_dump($array); both produce some very ugly clutter of that $array. Ok it is what it says, it prints that array, but i'd like to have some well formated print of that array, how do i do that?
echo '<pre>';
var_dump($array);
echo '</pre>';
Or to a better yet performance, use xDebug with
html_colors = on.
html_colors can be found on php.ini file.
xdebug is from http://xdebug.org/download.php
With xDebug and Colors you don't need to use
echo '<pre>';
echo '</pre>';
it's beautiful as-is.
Actually, it is well-formatted but HTML ignores line breaks and double spaces.
You just have to view the page's source code (CTRL+U or right-click > view source code)
You can wrap your var_dump() output in <pre> to preserve the monospacing:
echo "<pre>" . var_dump($array) . "</pre>";
echo "<pre>".print_r($array, true)."</pre>";
try this code
$myArray=array("a","b","c");
foreach($myArray as $A)
{
echo $A."<br/>"; // you can use any style here like table, div span etc...
}
I like:
highlight_string(var_export($array, true));
var_export() is usable (copy/paste) PHP code as well.
if you want to see it well formatted, you can encode it to json object and print it pretty :)
You have to do is:
echo json_encode($array,JSON_PRETTY_PRINT); //This needs PHP 5.3 at least.
Hello I want to decode json. My code below:
<?php
$json = '{"response":{"count":1,"items":[{"id":165983743,"owner_id":170785079,"title":"Ke$ha - Blow","duration":253,"description":"","date":1379017507,"views":1,"comments":0,"photo_130":"http:\/\/cs518121.vk.me\/u170785079\/video\/s_5e5f6f2c.jpg","photo_320":"http:\/\/cs518121.vk.me\/u170785079\/video\/l_dd4ec237.jpg","files":{"mp4_240":"http:\/\/cs518121v4.vk.me\/u170785079\/videos\/500770e51c.240.mp4","mp4_360":"http:\/\/cs518121v4.vk.me\/u170785079\/videos\/500770e51c.360.mp4","mp4_480":"http:\/\/cs1-46v4.vk.me\/p13\/483502b20c4f.480.mp4","mp4_720":"http:\/\/cs518121v4.vk.me\/u170785079\/videos\/500770e51c.720.mp4"},"player":"http:\/\/vk.com\/video_ext.php?oid=170785079&id=165983743&hash=1e417a266e9a3f00"}]}}';
$obj = json_decode($json);
print_r ($obj);
print $obj->{'response'}->{'items'}->{'files'}->{'mp4_240'};
But I get a blank page
print_r should actually print something - your json is correct.
You should do it like this:
print $obj->response->items[0]->files->mp4_240;
Here's a code working on ideone: http://ideone.com/4xXfOl
EDIT: Please whoever downvoted these answers, explain why you do so in comments...
at first u must enable display errors at yours php interpreter
ini_set('display_errors',1);
error_reporting(E_ALL);
and then u have to read more intently the structure of json which u want to travers, the
items as an array, actually the object keys says it to you: the plural form of item
so the solve is:
print $obj->{'response'}->{'items'}[0]->{'files'}->{'mp4_240'};
of course I dislike such syntax, it would be better using
print $obj->response->items[0]->files->mp4_240;
use $obj->{'prop_name'} form when the programm selects accessing attributes dynamicly
I think you need this
echo $obj->response->items[0]->files->mp4_240;
instead of print $obj->{'response'}->{'items'}->{'files'}->{'mp4_240'};