Introduction to PHP: The Language Behind the Web

PHP (Hypertext Preprocessor) is a powerful, open-source, server-side scripting language designed for web development. It is widely used to build dynamic websites and web applications and can be embedded within HTML. PHP is especially known for its efficiency, ease of learning, and strong integration with databases like MySQL.


🚀 Why Learn PHP?

  • Easy to learn and use

  • Open-source and free

  • Fast execution on the server

  • Great support for databases (MySQL, PostgreSQL, etc.)

  • Massive community support

  • Supported on almost all web servers (Apache, Nginx, IIS)


🔧 Basic PHP Syntax

PHP scripts are executed on the server, and the result is returned to the browser as plain HTML.

<?php
// This is a single-line comment
echo "Hello, World!";
?>
  • PHP code starts with <?php and ends with ?>.

  • Echo is used to output text to the browser.


🌐 Embedding PHP in HTML

<!DOCTYPE html>
<html>
<head><title>My First PHP Page</title></head>
<body>
<h1>Welcome!</h1>
<p>
<?php echo "This page is powered by PHP."; ?>
</p></body
>
</html>

🧮 Variables in PHP

Variables in PHP are declared using the $ symbol.

<?php
 $name = "Ajeet";
 $age = 25;
 echo "My name is $name and I am $age years old.";
?>

🔢 Data Types

PHP supports the following data types:

  • String

  • Integer

  • Float (double)

  • Boolean

  • Array

  • Object

  • NULL

  • Resource

<?php
$integer = 10;
$float = 10.5;
$string = "Hello";
$boolean = true;
?>

📐 Operators

Arithmetic Operators

<?php
$x = 10;
$y = 5;
echo $x + $y; // 15
?>

Comparison Operators

<?php
var_dump(5 == "5"); // true
var_dump(5 === "5"); // false
?>

🔄 Control Structures

If-Else

<?php
$score = 85;
if ($score > 90) {
echo "Excellent";
} elseif ($score > 75) {
echo "Good";
} else {
echo "Needs Improvement";
}
?>

Switch Statement

<?php
$day = "Monday";
switch ($day) {
case “Monday”:
echo “Start of the week”;
break;
case “Friday”:
echo “Weekend is near!”;
break;
default:
echo “Just another day”;
}
?>

Loops

For Loop

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

While Loop

<?php
$i = 1;
while ($i <= 3) {
echo "Count: $i <br>";
$i++;
}
?>

📦 Arrays

Indexed Array

<?php
$fruits = ["Apple", "Banana", "Cherry"];
echo $fruits[1]; // Banana
?>

Associative Array

<?php
$user = [
"name" => "Ajeet",
"age" => 25
];
echo $user["name"];
?>

Foreach Loop

<?php
foreach ($user as $key => $value) {
echo "$key: $value <br>";
}
?>

🧰 Functions

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

🗃️ Working with Forms

<form method="POST">
Name: <input type="text" name="name">
<input type="submit">
</form>
<?php
if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
$name = $_POST[‘name’];
echo “Welcome, $name!”;
}
?>

🛡️ Conclusion

PHP remains a top choice for developers building dynamic websites and APIs. Its simple syntax, vast ecosystem, and community support make it ideal for beginners and professionals alike. In future tutorials, we’ll dive into Object-Oriented PHP, Laravel Framework, RESTful APIs, Sessions, Authentication, and much more!