Skip to main content

Dynamic Web Development with PHP - Powering Interactive Websites

Dynamic Web Development with PHP - Powering Interactive Websites

1. Introduction to PHP

PHP (Hypertext Preprocessor) is a powerful server-side scripting language used to build dynamic websites and web applications.

  • Embedded directly within HTML.
  • Executes on the server before the page is sent to the browser.
  • Often used with MySQL databases.
Note: PHP files typically have a .php extension and can contain HTML, CSS, and PHP code together.

2. Setting Up PHP

  • Install XAMPP, WAMP, or Laragon (bundles Apache, PHP, and MySQL).
  • Save your files in the htdocs folder (for XAMPP).
  • Access them via http://localhost/filename.php.
<?php
  echo "Hello, PHP!";
?>
Tip: Use echo to output text or variables to the browser.

3. Basic Syntax

  • PHP code is wrapped inside <?php ... ?> tags.
  • Statements end with a semicolon (;).
<?php
$name = "Alice";
echo "Welcome, $name!";
?>

4. Variables and Data Types

<?php
$age = 25;               // Integer
$price = 19.99;          // Float
$name = "John";          // String
$isAdmin = true;         // Boolean

echo $name;
?>
Note: All PHP variables start with a $ sign.

5. Operators

<?php
$a = 10;
$b = 5;
echo $a + $b;   // 15
echo $a * $b;   // 50
echo $a == $b;  // false
?>

6. Conditionals and Loops

<?php
if ($a > $b) {
  echo "A is greater";
} else {
  echo "B is greater";
}

for ($i = 1; $i <= 5; $i++) {
  echo $i . "<br>";
}
?>

7. Functions

<?php
function greet($name) {
  return "Hello, $name!";
}

echo greet("Alice");
?>

8. Arrays

<?php
$colors = array("Red", "Green", "Blue");
echo $colors[0]; // Red

foreach ($colors as $color) {
  echo "$color <br>";
}
?>

9. Handling Forms

<form method="post" action="welcome.php">
  Name: <input type="text" name="name">
  <input type="submit">
</form>
<?php
$name = $_POST['name'];
echo "Welcome, $name!";
?>
Warning: Always validate and sanitize user input to prevent security vulnerabilities like XSS and SQL Injection.

10. Connecting to MySQL

<?php
$conn = mysqli_connect("localhost", "root", "", "testdb");

if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);

while ($row = mysqli_fetch_assoc($result)) {
  echo $row['username'] . "<br>";
}

mysqli_close($conn);
?>

11. Best Practices

  • Always use prepared statements for database queries.
  • Keep HTML and PHP logic separate where possible.
  • Comment your code clearly.
  • Use consistent naming conventions.

12. Mini Project: Simple Contact Form

<form method="post">
  Name: <input type="text" name="name"><br>
  Message: <textarea name="message"></textarea><br>
  <input type="submit" value="Send">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = htmlspecialchars($_POST["name"]);
  $message = htmlspecialchars($_POST["message"]);
  echo "Thanks, $name! Your message has been received.";
}
?>

13. Conclusion

PHP remains a cornerstone of web development, powering WordPress, Laravel, and millions of websites worldwide. Its simplicity, versatility, and strong community make it a great choice for building dynamic web apps.

Next, explore topics like OOP in PHP, sessions, and frameworks like Laravel or CodeIgniter.

Comments