PHP

Solutions

PHP Solutions

1.

<?php echo "<h1>Hello World!</h1>"?>

2.

  $num1 = 10;
  $num2 = 15;
  echo $num1 + $num2;

3.

<?php
  $names = array('John', 'Mary', 'Jane');
  foreach ($names as $name) {
    echo $name;
    echo ' ';    
  }
?>

4.

<?php
  $people = array(
    array('name' => 'John', 'job' => 'Builder'),
    array('name' => 'Mary', 'job' => 'Astronaut'),
    array('name' => 'Jane', 'job' => 'Nurse'),      
  );
  foreach ($people as $person) {
    echo $person['name'];
    echo ' is a ';
    echo $person['job'];    
  }
?>

5.

<!DOCTYPE html>
<html>
  <head>
    <title>People</title>
    <meta charset="utf-8">
  </head>
  <body>
    <table>
      <tr><th>Name</th><th>Job</th></tr>
<?php
  $people = array(
    array('name' => 'John', 'job' => 'Builder'),
    array('name' => 'Mary', 'job' => 'Astronaut'),
    array('name' => 'Jane', 'job' => 'Nurse'),      
  );
  foreach ($people as $person)
    echo '<tr><td>' . $person['name'] . '</td><td>' . $person['job'] . '</td></tr>';
?>
    </table>
  </body>
</html>

6.

<!DOCTYPE html>
<html>
  <head>
    <title>People</title>
    <meta charset="utf-8">
  </head>
  <body>
    <table>
      <tr><th>Name</th><th>Job</th></tr>
<?php
  $people = array(
    array('name' => 'John', 'job' => 'Builder'),
    array('name' => 'Mary', 'job' => 'Astronaut'),
    array('name' => 'Jane', 'job' => 'Nurse'),      
  );
  foreach ($people as $person)
    echo '<tr><td><a href="hello.php?name=' . $person['name'] . '">' . $person['name'] . '</a></td><td>' . $person['job'] . '</td></tr>';
?>
    </table>
  </body>
</html>
<!DOCTYPE html>
<html>
  <head>
    <title>People</title>
    <meta charset="utf-8">
  </head>
  <body>
<?php
  echo '<p>Hello ' . $_GET['name'] . '</p>';
?>
    <a href="list.php">Go back</a>
  </body>
</html>

7.

<!DOCTYPE html>
<html>
  <head>
    <title>People</title>
    <meta charset="utf-8">
  </head>
  <body>
    <form method="POST" action="hello.php">
      <input type="text" name="name" placeholder="Name">
      <input type="submit" value="Say Hello">
    </form>
  </body>
</html>
<!DOCTYPE html>
<html>
  <head>
    <title>People</title>
    <meta charset="utf-8">
  </head>
  <body>
<?php
  echo '<p>Hello ' . $_POST['name'] . '</p>';
?>
    <a href="form.php">Go back</a>
  </body>
</html>