php within html

how can i make the php print within an html?

<?php $filename = 'data/'.'cities.txt'; $fp=fopen($filename, 'r'); for ($ii = 1; $ii <= $lines_in_file; $ii++) { $line = fgets($fp); $city = trim($line); print 'City:
'; select name="$filename" selected="-" size="1" } fclose($fp); ?>

I’m trying to do this–> " When a user clicks the link to open up the first page,
assignment_3_register.php, the SELECT-OPTION list should
create the options with PHP code by reading the cities.txt file and
displaying the cities in the list, one city per "

You can pretty freely mix php and html code together. You need to make sure that any php code is within <?php and ?> tags. The thing to remember is to make sure the file always has a .php extension, or your php code will not execute.

Here is an example based on what you appear to be trying to do:[php]

City Selector <?php $cities = array('New York','Hong Kong','Munich','Moscow','London','Paris'); ?> <?php foreach($cities as $city) { echo "$city"; } ?> [/php]

You will notice that I am using “echo” instead of “print.” There are a couple of differences between the two, but you can use either one in nearly every situation you are likely to run into. Most php programmers use echo. Tests have shown echo to be slightly faster (so slight that I have never seen a situation where it was relevant). Also, print returns a value, where echo doesn’t. The code above will work equally well with either echo or print.

There are many different ways to do the same thing with php. I tried to mix a few different approaches in the example so that you can see a couple of options. In the end, it comes down as much to personal taste as anything.

Sponsor our Newsletter | Privacy Policy | Terms of Service