I've noticed that when I try to display the value of a variable with PHP, for example using print_r($array)/var_dump(), etc or even when using the Reflection API, I end up with output that is hard to read because there are no line breaks. Every screen shot I see everywhere else has these things displayed in a sort of tree looking format that is much easier to read. Does anyone know why this is?
It's not a native feature of Php. Try installing X-Debug for a better look and feel of your var_dump.
Try:
echo nl2br($output);
Or try viewing it with the "View source" option of your browser.
Hope this helps
EDIT
OR just use the pre tags, like this:
<?php
function my_print_r($var) {
$output = "";
$output .= "<pre>";
$output .= print_r($var, true);
$output .= "</pre>";
return $output;
}
echo my_print_r(array(1, 2, 3));
?>
If you would view it in a browser you could wrap it inside the <pre> HTML tag like so:
echo "<pre>";
print_r($output);
echo "</pre>\n";
Try adding echo '<pre>'; before printing the variable
Related
how can Dynamic this code with php .my address is variable
<?php
$pagecontents = file_get_contents("http://google.com");
$html = htmlentities($pagecontents);
echo $html;
?>
I'm not sure, I understood, what the goal is, but if you want to do the same thing, that you have shown in the question but with multiple sites, then you can do it with a simple loop:
$sites = ["aa.com/a", "aa.com/b"] // array(...) with earlier PHP versions
foreach($sites as $url) {
$pagecontents = file_get_contents($url);
echo htmlentities($pagecontents);
}
If this is not what you are looking for, then please refactor the question, so it clearly explains, what you want to do!
$m = 'this_is_m';
$this_is_m = 'this_is_m_not_full :(';
$this_is_m_full = 'this_is_m_FULL!! :D';
print ${$m};
(Hi first :P) Outputs:
this_is_m_not full :(
Any idea how to output this_is_m_FULL!! :D using $m??
What I've already tried:
print $"{$m}_full";
print $'{$m}_full';
print $'$m_full';
print ${$m}_full';
None worked... Thanks in advance,,
The solution would be:
print ${$m . '_full'};
But that feels very hackish.
To get your desired output, you need to do the following:
print ${$m . "_full"};
The following should work:
print ${$m.'_full'};
This is because the string inside the braces will get evaluated first, becoming
print ${'this_is_m' . '_full'}
-> print ${'this_is_m_full'}
-> print $this_is_m_full
Take a look at this manual page if you want more information on this.
I'm having issue working with json_decode() in PHP. Im using the json2.js library to convert a JSON to string. Then post it to PHP. That part seems fine.
Here is my PHP function :
public function SaveUser($json){
$json2 = json_decode($json,true);
print 'Intrant : <br />'.$json.'<br />';
print '<pre>VAR DUMP:<br />';
var_dump($json2);
print '</pre>';
// Do some things
}
The following returns the following :
Intrant :
{"user_id":"14","prenom":"prenom","nom":"nom","profil_heures_fixe":"0","nb_heures_fixe":"","is_userliste":"1","is_paye":"1","username":"username","password":"","telephone":"111-111-1111","cellulaire":"111-111-1111","extension":"30","courriel":"user#server.com","date_embauche":"2017-07-02","machine":"","profil_id":"4","status_id":"1","coordonnees":"","urgence":""}
VAR DUMP:
NULL
The follow works just fine for me:
<?php
function SaveUser($json){
$json2 = json_decode($json,true);
print 'Intrant : <br />'.$json.'<br />';
print '<pre>VAR DUMP:<br />';
var_dump($json2);
print '</pre>';
// Do some things
}
$t = '{"user_id":"14","prenom":"prenom","nom":"nom","profil_heures_fixe":"0","nb_heures_fixe":"","is_userliste":"1","is_paye":"1","username":"username","password":"","telephone":"111-111-1111","cellulaire":"111-111-1111","extension":"30","courriel":"user#server.com","date_embauche":"2017-07-02","machine":"","profil_id":"4","status_id":"1","coordonnees":"","urgence":""}';
SaveUser($t);
If that doesn't work for you (as a PHP script on its own), then it's possible you don't have PHP's json extension installed. Check using either "php -m | grep json" or function_exists("json_decode").
dont know what your doing ..
but works here perfectly
http://pastebin.com/DzSs8mNd
Your json string, as displayed by the browser, parses correctly. I think a few of us have seen that. But, what the browser displays and what is really there are often very different things. For example, you might have a whitespace character hidden in the wrong spot. Try moving the <pre> and see what happens:
print '<pre>Intrant : <br />'.$json.'<br />';
print 'VAR DUMP:<br />';
var_dump($json2);
print '</pre>';
Thanks Marc B,
Since our server is using the charset ISO-8859-1 the json_decode function does not work.
$json2 = json_decode(utf8_encode($json),true);
Thanks all
I have something like this in one field of my table(MySql):
$data = '<td>apple</td>';
echo $data;
I select this field and echo it into the page.I want to replace 'apple' word with a php function that return a word.So I thought
$data = '<td>myphp_function('fruit');</td>';
echo $data;
but what I see in the page is exactly the line above and not my function output.
how can I do it?
I am not sure if i could explain my mean clearly...
Edited.
According to your last edit, what you need is the following:
$data = '<td>' . myphp_function('fruit') . '</td>';
echo $data;
This is assuming your myphp_function() will return some kind of value.
If the function echoes the value, it will not work as expected!
You can only execute PHP when you open PHP tags. Other than that, it's just plain text/html.
<td>myphp_function('fruit');</td>
To execute your function you have to open PHP tags:
<td><?php myphp_function('fruit'); ?></td>
you have to insert some sort of placeholder into your text. Like this
<td>[fruit]</td>
and then do a replace before printing it out:
$fruit = 'apple';
$text = str_replace('[fruit]',$fruit,$text);
Of course, for the real life usage there will be more complex solution.
So, you will do yourself enormous favor, if you post here your real task with real data example, not oversimplified and useless abstract question.
Quite simple question i belive. How to print the whole page into variable and then use where i need.
For instance if the code is:
<?php
$arr = array('hello','mate','world');
foreach ($arr as $a) {print "<p>".$a."</p>"; }
?>
Now if we go to that page, we can see an array output, but i would prefer to print the whole page into variable and then generate static page for instance out of that.
Maybe file_get_content or <<<EOT, but the page will get more complicated later so not sure what is the best option.
Not sure about your exact needs but:
ob_start();
require('/path/to/templates/foo.php');
$template = ob_get_contents();
ob_get_clean();
ob_start();
// your code
$var = ob_get_clean();
print $var;
Why don't you use smarty ,
put all HTML in a template , and insert PHP code or variables into it . in the end , using $x=$smarty->fetch('template_name'); you put all the page in the $x variable ...