A PHP script is embedded within an HTML document, typically within <?php
and ?>
tags. Here’s a basic example:
<!DOCTYPE html>
<html>
<head>
<title>My PHP Page</title>
</head>
<body>
<h1>Welcome to my PHP Page</h1>
<?php
// PHP code goes here
echo "Hello, World!";
?>
</body>
</html>
In this example, PHP code is enclosed within <?php
and ?>
tags and can be inserted anywhere within the HTML structure.
In PHP, you can declare variables using the $
symbol followed by the variable name. Variable names are case-sensitive and can include letters, numbers, and underscores. They must start with a letter or underscore. Here’s an example:
$name = "John";
$age = 30;
PHP supports various data types, including:
Strings: Used for text. Enclosed in single or double quotes. Example: $name = "John";
Integers: Used for whole numbers. Example: $age = 30;
Floats: Used for decimal numbers. Example: $price = 19.99;
Booleans: Used for true or false values. Example: $isStudent = true;
Arrays: Used to store multiple values. Example: $colors = array("red", "green", "blue");
Objects: Instances of user-defined classes.
NULL: Represents the absence of a value.
PHP supports various operators for performing operations on variables and values. These include:
Conditional statements allow you to execute different code blocks based on specified conditions. PHP supports if
, else if
, else
, and switch
statements.
if
Statement$age = 18;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are not yet an adult.";
}
In this example, if the condition $age >= 18
is true, it executes the first block of code; otherwise, it executes the second block.
switch
Statement$day = "Monday";
switch ($day) {
case "Monday":
echo "It's the start of the week.";
break;
case "Friday":
echo "TGIF!";
break;
default:
echo "It's just another day.";
}
The switch
statement allows you to evaluate a variable against multiple possible values.
Loops in PHP are used to execute a block of code repeatedly. Commonly used loops include for
, while
, and foreach
.
for
Loopfor ($i = 0; $i < 5; $i++) {
echo "Iteration: $i <br>";
}
The for
loop is used when you know how many times you want to repeat a block of code.
while
Loop$num = 1;
while ($num <= 5) {
echo "Number: $num <br>";
$num++;
}
The while
loop is used when you want to repeat a block of code as long as a condition is true.
foreach
Loop$fruits = array("apple", "banana", "cherry");
foreach ($fruits as $fruit) {
echo "Fruit: $fruit <br>";
}
The foreach
loop is specifically designed for iterating over arrays and objects.
Functions in PHP allow you to encapsulate a block of code into a reusable component. You can declare and call functions as follows:
function greet($name) {
echo "Hello, $name!";
}
greet("John");
Functions can have parameters and return values, making them versatile for performing various tasks within your code.
PHP’s syntax is relatively easy to grasp, making it a popular choice for web development. Whether you’re working with variables, operators, conditional statements, loops, or functions, mastering these fundamentals is essential for becoming proficient in PHP programming. With this knowledge, you can start building dynamic and interactive web applications.