Is it possible to call PHP in html?

2019-09-14 09:40发布

问题:

I have a PHP that returns some HTML code, for example, if I execute this in the browser,

http://www.myserver.com/myscript.php?size=300

this will return some html code like this,

'<div><a href="..."><img src="..." /></a></div>'

I am wondering if it's possible I can call this php script in my html code directly? For example, if I want to use the output from the PHP script in my Wordpress sidebar widget?

回答1:

You can use file_get_contents() like this:

<div class="sidebar">
    <?php
        $html = file_get_contents("http://www.myserver.com/myscript.php?size=300");
        echo $html;
    ?>
</div>


回答2:

If your Current page's extension is not php, you cannot call php in it directly. You have to use some javascript/jquery/ajax to run the php file externally and load the data into a class or id using one of those javascript based code. If your current page is already a php file than,you can use include(); or require(); functions and it does the job.



回答3:

above answers are correct, but that might be not concerned for wordpress, that are of core php functionality.

i have a different way for sidebar use, that's provide by wordpress already.

it is an inbuilt functionality, please take a look towards my answer.

Show active sidebar in Wordpress

How to make wordpress sidebar

you can found ::

try this one, it is different way of using sidebar

[1] put your all "left_bar"(sidebar) code in a new file named "sidebar-left_bar"

[2] save it with header.php, function.php and all files (saving location)

[3] now just use <?php get_sidebar( 'left_bar' ); ?> where you want to use

Thanks