tags:

views:

79

answers:

2

UPDATE:

I know I can use <ol> directky in the output but I remember using something like:

<?php echo $i++; ?> when I worked on a wordpress blog once. Every time I inserted that tag a number greater than the previous appeared so I basically did:

<?php echo $i++; ?> Text
<?php echo $i++; ?> Text
<?php echo $i++; ?> Text

I'm a front end guy (HTML/CSS) so please excuse this basic question. I just need to know what code in PHP I can use to number some text.

Text

Text

Text

into:

  1. Text

  2. Text

  3. Text

Kind of like what <ol> does in html but in PHP.

+6  A: 

Updated answer:

You can use a variable as you already do (the example you are posting should already work). Just initialize it using $i = 0;

Old answer:


You have a fundamental misunderstanding here. PHP is a scripting language, not a markup language. PHP does operations like connecting to data sources, calculating, making additions, changing entries in databases, and so on. PHP code, in short, is a series of commands that are executed. PHP has no design elements, tags and formatting options in itself.

PHP can (and usually does) output HTML (Where you have <ol>) to display things.

You can have an array of arbitrary data in PHP, coming from a file or data source:

$array = array("First chapter", "Second chapter", "Third chapter"); 

you can output this data as HTML:

echo "<ol>";

foreach ($array as $element)  // Go through each array element and output an <li>
 echo "<li>$element</li>";

echo "</ol>";

the result being (roughly)

<ol>
<li>First chapter</li>
<li>Second chapter</li>
<li>Third chapter</li>
</ol>
Pekka
+1  A: 

It depends on what type of file you are trying to write. Most often, PHP is writing a webpage in HTML, but not always. In HTML, if you want a numbered list, you should use an ordered list (<ol>).

If you're just writing a text file of some kind, incrementing and outputting a variable (like $i in your example) should work.

You mention Wordpress, so it's worth noting that if you worked on a Wordpress template before, you were using dozens of special functions in the Wordpress library, even though you may not have been completely aware that was what you were doing. A lot of the PHP heavy lifting is hidden and simplified for the templating engine, and if your current project is not built on that engine, you will have to do that logic yourself.

keithjgrant