Relation Between HTML, PHP and Server(XAMMP)


#1



Actually i want to ask, Right now i'm trying to use XAMPP to do php stuff, I want to ask some things that is not clear for me after finished PHP course

1.why my code not executed? I tried use echo and it's works, but it doesnt work with html tag(just like codeacademy's course do)

2.if i use html tag inside php, do i need to include every single important tag from html?(doctype, title, head,body and etc).

3.based on my code, server translates the echo $name to in server side, then pass the translated tag to client side later, am i right?


//this doest work
<?php 
     $name = "james";
     <table>
         <tr>
             <td>Name</td>
             <td><?php echo $name?></td>
         </tr>
     </table>
?>

//this is works
<?php
    $name= "james";
     echo "<table>";
     echo "<tr>";
     echo "<td>Name</td>";
     echo "<td>".$name."</td>";
     echo "</tr>";
     echo "</table>";
?>


#2

your code should execute inside xampp?

i don't understand, what do you mean?

Yes, your server is handling all the <?php ... ?>, the browser can't handle them, only a webserver with php installed can.

Your server converts the <?php ?> into something the clients browser (firefox, chrome and so on) can understand.

Lets do analogy, expecting <?php ?> to work in firefox/chrome is like trying to open a .docx file with vlc media player, it simple doesn't work. vlc isn't designed to open .docx (office word) files. Same for php, a browser isn't designed to handle php code


#3

I've updated my code, and you should understand the problem

my code executed, but the output is

Parse error: syntax error, unexpected '<', expecting end of file in C:\xampp\htdocs\new1.php on line 3


#4

here is a huge problem:

<?php 
     $name = "james";
     <table>
         <tr>
             <td>Name</td>
             <td><?php echo $name?></td>
         </tr>
     </table>
?>

you can't include html code directly inside <?php ?>, you would need to do:

echo "<table>"

i also wouldn't nest php tags inside each other:

<?php <?php ?> ?>

yes, you did this. i would personally change this:

 <?php 
     $name = "james";
     <table>
         <tr>
             <td>Name</td>
             <td><?php echo $name?></td>
         </tr>
     </table>
?>

into:

 <?php 
     $name = "james";
?>
     <table>
         <tr>
             <td>Name</td>
             <td><?php echo $name?></td>
         </tr>
     </table>

see what i did? i moved the closing ?> which was after the closing table tag to after you finished writing php code


#5

Ok, now I get it, actually it is possible to do //first only if I put it inside .html file, right?

   //first only work inside.html file
    <?php 
         $name = "james";
         <table>
             <tr>
                 <td>Name</td>
                 <td><?php echo $name?></td>
             </tr>
         </table>
    ?>

1.so actually, php usually itself contains html tag + php itself, am i right?and it is a common thing for .php to contain html tag, right?
2.another question, example lets say I made a aboutMe.html, how actually i pass my aboutMe.html to user browser? do i need to make aboutMe.php first?


#6

you don't seem to get it, the code you just posted still has html tags inside php tags.

php code only works in .php files. this code:

</table>
    ?>

the ?> should be before the table opening tag:

<?php 
         $name = "james";
?>
<table>

if the file contains php tags, the file should be .php. I will have to change my analogy, a browser can open a .php file after xampp converted the <?php ?> into readable code for the browser


#7

hmmmmm.....take a look at stack overflow link below
Do PHP inside HTMl

for .PHP itself, like you said we can't put html tag inside php tag, because server dont understand what is html tag is, am i right?correct me if I'm wrong :smiley:

//this is the right way to do so inside .PHP
<?php 
	$name = "james";
?>

<table>
    <tr>
        <td>Name</td>
		<td></td>
	</tr>
</table>

//this is wrong,because i nested php tag
    <?php 
         $name = "james";
         <table>
             <tr>
                 <td>Name</td>
                 <td><?php echo $name?></td>
             </tr>
         </table>
    ?>

    this is wrong because actually, server don't know what html tag is, so ended up with "error"
    <?php 
      <table>
        <tr>
            <td>Name</td>
    		<td></td>
    	</tr>
      </table>
    ?>

1.I actually try to figure out how to do PHP inside HTML(if it's possible)
2.How to send html from server?in form of .php?


#8

you already added php inside html:

<td><?php echo $name; ?></td>

you can also add html to php, but you would need echo:

<?php
  echo "<p>see?</p>";
?>

#9

1.no, i mean is it possible to do php inside .html extension, lets say i want to put my php code inside aboutMe.HTML, or is it impossible?

2.I tried to copy html tag to my php(from my html file), and moved all of php,css ,and js in same folder inside XAMMP htdocs, and it works!
so my question, in real life, is there any html we download from server, or only php(which contains html itself)?

sorry for stupid question from me :smiley:


#10

This is impossible.

  1. if the file only contains html you could have .html, but i doubt this would be the case, so i think pretty much all your files would .php

#11

how about the 2nd question?:smiley:


#12

what is the codecademy forum doing? answer to first question: that is impossible

to second question:
if the file only contains html you could have .html, but i doubt this would be the case, so i think pretty much all your files would .php

the codecademy forum changed my 2. into 1.


#13

thank you very much
I'm sorry to put you through this
again, thank you very much to help me figured it out :smiley: