-2

I'm a newbie in PHP programming and I'd like to know if it's possible to change an entire body section of an HTML page to PHP. If yes, how can I proceed? For example, let's consider this piece of code:

 <body>
        <div class="headertitle">
             <a href="https://www.google.com">Google</a>
        </div>
             <br/><br/>

              <div class="row">

                <div class="large-post">

                  <center><h3>Dev Zone</h3></center>

                  // Here I want to add some PHP code
                  // $dev = "Hello";
                  // echo $dev;

                  <div class="separator"></div>
                  <br/>
                  <h6>Hello!</h6>

                </div>

            <script src="js/vendor/jquery.js"></script>

</body>

Thank you very much.

Kevin
  • 203
  • 1
  • 8
  • 3
    use `` tags and use a `.php` extension if you don't want to instruct your server to treat `.html` files as php. and make sure you''re running this off a webserver with PHP installed. – Funk Forty Niner Feb 09 '16 at 16:23
  • What do you mean by "convert HTML to PHP"? Based on the "comments" in the code posted it sounds like what you're looking for is basically any introductory tutorial to PHP. – David Feb 09 '16 at 16:25
  • PHP and HTML are two different things that work togheter. HTML is used to show, while PHP is executed server side and elaborates in the backgroud *before* the visualization of the HTML. – phaberest Feb 09 '16 at 16:25
  • 2
    A similar question was asked here. http://stackoverflow.com/questions/14572647/php-with-html-versus-html-with-php – BryanT Feb 09 '16 at 16:28
  • Take a look at [this](http://php.net/manual/en/getting-started.php). – Don't Panic Feb 09 '16 at 16:28
  • *"I'd like to know if it's possible to change an entire body section of an HTML page"* - ***Entire*** means from `` to `` or from `
    ` to ``.
    – Funk Forty Niner Feb 09 '16 at 16:34

3 Answers3

0

You can do something like this :

<?php
    $html = "";
    $variable = "Hello World";
    $html .= <<<HTML
    <!-- Your html content -->
    {$variable}
HTML;//Need to be always on the left
    $variable2 = "Hi";
    $html .= <<<HTML
    <h2>{$variable2}</h2>
HTML;
    echo $html;
?>

Your file need to have the .php extension to work fine.

Thomas Rollet
  • 1,573
  • 4
  • 19
  • 33
0

There are many ways to do it, you can either inline PHP code in your html file and use the .php extension. Or use only PHP code in your .php file and echo the html code instead. The most important is that your file needs the .php extension to be parsed correctly.

Example: inline_php.php

<html>
  <body>
   <?php 
     $dev = "Hello";
     echo "something";
   ?>
  </body>
<html>

Or you can echo HTML inside a complete PHP file

Example: index.php

<?php
echo "<html>\n";
echo "<body>\n";
$dev = "hello";
$somevariable = $dev."42";
dosomething(); //comment
// the \n is only for readability with inspect element.
echo "something else\n";
echo " </body>\n";
echo "<html>\n";

So in your specific case I would do:

name file as index.php

<body>
        <div class="headertitle">
             <a href="https://www.google.com">Google</a>
        </div>
             <br/><br/>

              <div class="row">

                <div class="large-post">
                  <center><h3>Dev Zone</h3></center>
                  <?php // Here I want to add some PHP code
                    $dev = "Hello";
                    echo $dev;
                  ?>
                  <div class="separator"></div>
                  <br/>
                  <h6>Hello!</h6>
                </div>
            <script src="js/vendor/jquery.js"></script>
</body>
Community
  • 1
  • 1
Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
0

Now that I see your edits I better understand your question.

There are many ways of showing off your PHP into your HTML, anyway this is a good point to start from.

Where you wrote

// Here I want to add some PHP code
// $dev = "Hello";
// echo $dev;

You have to wrap the code inside a opening <?php and a closing ?> to let your web server understand that that part of your page needs to be read as PHP.

That means that the previous part needs to become:

<?php
    // Here I want to add some PHP code
    $dev = "Hello world";

    echo $dev;
?>

As others already told you, in addition, your page must have a .php extension and you need a web server running.

You may use php direclty (you need to have it installed on your machine, obviously) with the command php -S localhost:8000 launched from your project folder, or install something like XAMPP which is probably more newcomer-friendly.

phaberest
  • 3,140
  • 3
  • 32
  • 40