PHP Wordpress: Where does print_r() output to in Safari? - php

If I execute
print_r('Hello');
on a Wordpress code snippet, where would this actually print? I'm using Safari.

print_r in php typically prints in the parent node of the node you placed the code within.

Since you didnt give more information i assume that
this is the php print_r.
So its most likely that the output will be in the middle of your HTML code or your snippet.
You should find it in the HTML source and maybe also see it on the website

Related

SMARTYPHP variable not outputting HTML code

Hi im using smarty and have declared variables which basically are meant to output the following code.
<img src="/images/port.png">
However when this is what it outputs <img src="/images/port.png">
The HTML tags are not working and it just shows text.
The code i used in my PHP file is {$configoption.optionname}
Any help would be great.
I'm not very active, my reputation isn't high enough to comment, but everyone will ask you to see more of your code. If you can put it up I can edit this answer with more helpful info.
Is there a reason you can't just set your smarty variable to the inner string "img src='/images/port.png'" and put content into your template like so
< {$imgStr} > if brackets are giving you trouble?

getting xml data as is(with tags, attributes and values) with php

Hello im struggling with a problem. I have an url that contains xml data...
when i'm using file_get_contents($url) or fopen($url,'r') it gives me only values:
Consider the xml:
<tag1 attrName="something">
<tag2>some Value</tag2>
<tag2>some Other Value</tag2>
...
...
</tag1>
what i get: some Value, some Other Value
But i need to get whole xml (with tags and attributes and its' values) and parse it with my own way because there's a restriction that i'm not allowed to use php 5.x practices.I mean i cant use any parser.. It shouldnt be so hard to get xml data as is.. should it??
what i get: some Value, some Other Value
Nope - my suspicion is that that is what you see in your browser, because it is swallowing all <tags>.
The XML source code will be there after a file_get_contents() operation.
You are using file_get_contents() which states
This function is similar to file(), except that file_get_contents()
returns the file in a string, starting at the specified offset up to
maxlen bytes. On failure, file_get_contents() will return FALSE.
Press Ctrl+u to see the source code in any of the major browsers(except IE where its F12 in IE9). I am sure that your code will be there. Your browser wont display the tags that's all.
The other longer(but better way) to display an XML file from your php file is to pass the content type as text/xml. Use the following way
<?php
header("Content-Type: text/xml");//SHOULD come before any output
// dynamically generate and output your xml here
?>

PHP stop closing tag being automatically added

When I add the string below to an array in PHP and output it,
$spineArray[$i]="<itemref idref='part-$i' linear='yes'/>";
It looks like this when outputted. Why is the closing tag automatically added and can I stop it? Thanks.
<itemref idref='part-$i' linear='yes'> </itemref>
I can assure you that PHP doesn't do this - at least, not by itself. Where are you looking when you see the output you describe?
Chances are that whatever you're using to view the output in is not showing the raw output, but the result of it parsing the XML you give it, but without some more information, I'm afraid your question cannot be answered.
Are you using FireBug to look at the HTML? Firebugs adds missing tags on it's own. Make sure that you look at the HTML with the browser's "View Source" feature
edit: Your replay to kander makes it obvious. If you look at the code as described above, I'm sure you won't see the tag added.
I think you used the firefox "view selected source". It shows the interpreted/corrected/parsed state of the html.
If you use "view source" (CONTROL + u) you will see the raw markup.
Try for yourself:
<p>lorem<br />ipsum</p>
In "view selected source <br /> transform to <br>":
<p>lorem<br>ipsum</p>
In "view source <br /> stays <br />":
<p>lorem<br />ipsum</p>
What do you use to process and output it? There must be some HTML/XML processing tool in the way because PHP itself doesn't do this.
The answer is to remove the HTML/XML processing or keep a copy of the string in another variable and output that one.
Based on your paste, I assume you are viewing this in some sort of XML viewer. It's possible for the XML to be displayed this way.

Why will this text in the browser source code not show on the screen?

I am modifying an existing source code viewer script in PHP that I found on the web. It is to store source code in a nice category fashion.
I am having a problem now though. Below is a screen shot of me viewing the source code in Firefox, you can see the part that is supposed to show on the screen but for some reason it is not showing on the screen, it is showing up in the source of the page though so I am really confused as to why I cannot view it in the browser? You will also notice that the text color is Pink/Purple color. The part inside the
Please not that it is not any CSS that is making it hidden or anything.
alt text http://img2.pict.com/cd/d2/74/2663869/0/screenshot2b209.png
<?php is considered as an opening tag, which is only closed by ?> ; and the browser doesn't display tags themselves.
The <td> tags, for instance, are not displayed by your browser : they are interpreted to create a table ; it's the same with the <?php tag... But it doesn't generate any output, as the browser doesn't know what to do with it.
If you want to actually display your portion of PHP code in the HTML page, you have to encode it to HTML entities :
< should be converted to <
> should be converted to >
& should be converted to &
" should be converted to "
This way, you'll get some valid HTML, and not "things looking as HTML tags".
But note that if you want that portion of PHP code to actually be interpreted (So the query to the database is executed, and generates some output), you'll have to re-configure your webserver, so PHP code is interpreted : you should not see the PHP code on the browser-side.
Processing Instructions are not shown in the browser.
That is not "browser source code". It is PHP source code that superficially looks like HTML. This is not a petty distinction, but actually fundamental if you want to understand how things work. If you are viewing that exact code in Firefox, it means you're viewing the PHP as text, not rendered HTML.
So something is wrong with your PHP server setup.

Printing $node vars in page.tpl.php

In Drupal's page.tpl.php
<?php foreach($node->og_groups as $test) {dpm($test);} ?>
Gives me
alt text http://img.skitch.com/20091229-ekf6xqg5dxq6cgjsgfty74umfx.jpg
But when I do
<?php foreach($node->og_groups as $test) {print($test);} ?>
The value doesn't show up. this is kinda frustating..
Any help is appreciated.
Cheers!
As Steve Michel suggested: Try doing a view source on the rendered page; it may be going to the top of the HTML and may not be visible in the browser output.
Drupal first executes all code, collecting output into a variable. At the very end, this variable is print out. If you print or var_dump something in between, this will be at the very top of the output (since that's done before any of the regular content is printed).
You even figured out the answer: use drupal_set_message (for which dpm is an abbreviation I guess?) to insert text in a nicely formatted way, somewhere in the content part of the page rather than before the tag.
If you need to print out arrays, you can use dpm(print_r($array, 1)) -- the 1 argument makes print_r return the formatted output (and pass it to dpm) rather than printing it out directly.
Try doing a view source on the rendered page; it may be going to the top of the HTML and may not be visible in the browser output.
You can use
<?php foreach($node->og_groups as $test) print_r($test) ?>
You can do this:
$node = $variables['node'];
and then use it like a normal node.

Categories