PHP (Hypertext Preprocessor) is a widely-used, open-source scripting language designed for web development. It allows you to create dynamic and interactive websites by embedding code within HTML documents. This tutorial will introduce you to the basics of PHP, including its syntax, variables, data types, operators, control structures, functions, and file handling.
Before diving into PHP, ensure your system is set up with a local development environment. Here are the steps:
Create a new file named index.php in your web server's root directory (e.g., htdocs for XAMPP).
<?php
echo "Hello, World!";
?>
Explanation:
<?php ?> tags enclose the PHP code.echo is used to output text or variables.To run this script, start your web server and navigate to http://localhost/index.php in your browser. You should see "Hello, World!" displayed.
Variables are used to store data. In PHP, variables start with a $ sign followed by the variable name.
<?php
$name = "Alice";
$age = 30;
echo "Name: $name, Age: $age";
?>
PHP supports several data types:
true or false.<?php
$name = "Alice"; // String
$age = 30; // Integer
$height = 5.9; // Float
$isStudent = false; // Boolean
$courses = array("Math", "Science"); // Array
?>
PHP supports various operators for arithmetic, comparison, logical, and string operations.
<?php
$a = 10;
$b = 5;
echo $a + $b; // Addition: 15
echo $a - $b; // Subtraction: 5
echo $a * $b; // Multiplication: 50
echo $a / $b; // Division: 2
echo $a % $b; // Modulus: 0
?>
<?php
$a = 10;
$b = 5;
var_dump($a == $b); // Equal: false
var_dump($a != $b); // Not equal: true
var_dump($a > $b); // Greater than: true
var_dump($a < $b); // Less than: false
var_dump($a >= $b); // Greater than or equal to: true
var_dump($a <= $b); // Less than or equal to: false
?>
<?php
$a = true;
$b = false;
var_dump($a && $b); // AND: false
var_dump($a || $b); // OR: true
var_dump(!$a); // NOT: false
?>
Control structures allow you to control the flow of your program.
<?php
$age = 18;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are a minor.";
}
?>
<?php
for ($i = 0; $i < 5; $i++) {
echo "Iteration: $i<br>";
}
?>
<?php
$i = 0;
while ($i < 5) {
echo "Iteration: $i<br>";
$i++;
}
?>
<?php
$i = 0;
do {
echo "Iteration: $i<br>";
$i++;
} while ($i < 5);
?>
Functions are reusable blocks of code that perform a specific task.
<?php
function greet($name) {
return "Hello, $name!";
}
echo greet("Alice");
?>
PHP provides numerous built-in functions for various tasks. For example:
strlen(): Returns the length of a string.array_push(): Adds one or more elements to the end of an array.<?php
$name = "Alice";
echo strlen($name); // Outputs: 5
$courses = array("Math", "Science");
array_push($courses, "History");
print_r($courses); // Outputs: Array ( [0] => Math [1] => Science [2] => History )
?>
PHP allows you to read from and write to files.
<?php
$file = fopen("example.txt", "r");
if ($file) {
while (($line = fgets($file)) !== false) {
echo $line;
}
fclose($file);
} else {
echo "Unable to open file.";
}
?>
<?php
$file = fopen("example.txt", "w");
if ($file) {
fwrite($file, "Hello, World!");
fclose($file);
} else {
echo "Unable to open file.";
}
?>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
This tutorial has provided a comprehensive introduction to PHP, covering its basics including syntax, variables, data types, operators, control structures, functions, and file handling. By mastering these fundamentals, you'll be well-equipped to start building dynamic web applications with PHP. Continue exploring more advanced topics like object-oriented programming, databases, and frameworks for further growth in your PHP development journey.