Jump to content


Photo

php and html, css tags


  • Please log in to reply
5 replies to this topic

#1 HEki_

HEki_

    Young Padawan

  • Members
  • Pip
  • 167 posts

Posted 26 January 2012 - 02:50 PM

I know there is a way to use html and css tags inside php code.
I just dont know the right way of doing it. I remember it goes something like using / before or after the "

<?php
echo "<table>";
echo "<tr>";
echo "<td style=/"width: 50px"/>some text</td>";
echo "</tr>";
echo "</table>";
?>

Can someone tell me the right way please?

#2 derek.sullivan

derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 343 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 26 January 2012 - 08:07 PM

could try:
<?php

echo <<<EOF
<html>
<!-- more html -->
</html>
EOF;
?>

  • 1P0 likes this

#3 Hayden

Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 717 posts
  • Gender:Male
  • Location:Texas

Posted 26 January 2012 - 11:34 PM

There's several ways to handle it, depending on what the rest of the page looks like.

You can use the heredoc like what Derek posted.

You can flip it around and do an HTML page with embedded PHP:

<table>
  <tr>
   <td <?php echo 'style="width:50px" ?>>some text</td>
  </tr>
</table>

You can do what you did but use single quotes instead of double, so you do not have to escape your double quotes in the HTML:

<?php
  echo '<table>';
  echo '<tr>';
  echo '<td style="width: 50px">some text</td>';
  echo '</tr>';
  echo '</table>';
?>

If you wondering how to do it exactly like you have, you escape the double quotes with a backslash, not a forward slash.

<?php
  echo "<table>";
  echo "<tr>";
  echo "<td style=\"width: 50px\">some text</td>";
  echo "</tr>";
  echo "</table>";
?>

Edited by Hayden, 26 January 2012 - 11:39 PM.

  • 1P0 likes this

#4 1P0

1P0

    Young Padawan

  • Members
  • Pip
  • 49 posts
  • Gender:Male

Posted 27 January 2012 - 01:09 PM

PHP is a templating technology initially designed to add programmatic code inside html.
So you can just start your file with html or close the php code with a ?> even inside a php function write your html+css and then resume php code with <?php as shown here:

<table>
<tr>
<td style="width: 50px"/><?php echo $myStringVarible; ?></td>
</tr>
</table>


and if you have a pure html file called mypurehtml.php with or without some little php inside you can always do like

function LoadHtml() {
require('mypurehtml.php');
}

and inside that mypurehtml.php file, you will have access to LoadHtml function local variables.

#5 JimmyJames

JimmyJames

    Young Padawan

  • Members
  • Pip
  • 39 posts
  • Gender:Male
  • Location:Calgary, AB

Posted 28 January 2012 - 12:46 PM

I often open and close my PHP as need in my HTML code, depending on the situation. I sometimes end up with something like:

<table>

<?php
while($c < 5){
?>

<tr><td>Hello world</td></tr>

<?php
    $c++
  }
?>
</table>

This will effectively output a table with several rows saying "Hello world".
There is a super negligible performance decrease when opening and closing tags like that.
[/code]

#6 Hayden

Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 717 posts
  • Gender:Male
  • Location:Texas

Posted 29 January 2012 - 03:55 PM

I often open and close my PHP as need in my HTML code, depending on the situation. I sometimes end up with something like:


<table>

<?php
while($c < 5){
?>

<tr><td>Hello world</td></tr>

<?php
	$c++
  }
?>
</table>

This will effectively output a table with several rows saying "Hello world".
There is a super negligible performance decrease when opening and closing tags like that.


You could do something like this as well.
<table>
<?php while($c < 5): ?>
	<tr><td>Hello world</td></tr>
<?php $c++; ?>
<?php endwhile; ?>
</table>


I do not believe there are any performance differences but IMO it's good code formatting.
  • 1P0 likes this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users