w3resource

PHP Exercises : Display string, values within a table


13. Display Values in Table

Write a e PHP script to display string, values within a table.

Note : Use HTML table elements into echo.

Sample Solution:

PHP Code

<?php
// Assign salary values to variables
$a=1000;
$b=1200;
$c=1400;
// Display a table with salary information using echo
echo "<table border=1 cellspacing=0 cellpading=0>
<tr> <td><font color=blue>Salary of Mr. A is</td> <td>$a$</font></td></tr> 
<tr> <td><font color=blue>Salary of Mr. B is</td> <td>$b$</font></td></tr>
<tr> <td><font color=blue>Salary of Mr. C is</td> <td>$c$</font></td></tr>
</table>";
?>

Explanation:

  • Assign Salary Values:
    • Variables $a, $b, and $c are assigned salary values: 1000, 1200, and 1400 respectively.
  • Table Structure with echo:
    • The echo statement is used to output HTML code to create a table displaying the salary information.
  • Table Properties:
    • The table has a border of 1, no cell spacing (cellspacing=0), and no cell padding (cellpading=0).
  • Table Rows for Each Person:
    • Each row (<tr>) represents the salary information for one person (Mr. A, Mr. B, and Mr. C).
    • Each row contains two cells (<td> tags):
      • The first cell displays the label (e.g., "Salary of Mr. A is") in blue text (font color=blue).
      • The second cell displays the corresponding salary value, followed by a dollar sign ($).
  • Output Example:
    • The resulting table displays each person’s salary in a visually organized format.

Output:

php table

View the output in the browser

Flowchart:

Flowchart: Display string, values within a table

For more Practice: Solve these Related Problems:

  • Write a PHP script to generate a dynamic HTML table from a multidimensional array with styled headers.
  • Write a PHP script to display key-value pairs in an HTML table while sorting keys alphabetically.
  • Write a PHP script to merge two arrays and output the combined data in a responsive HTML table.
  • Write a PHP script to create an HTML table that displays string lengths alongside the strings.


Go to:


PREV : Email Validation.
NEXT : Display Webpage Source Code.

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.