1. PHP Syntax Overview:
Tags:
- PHP code is enclosed within
tags.<?php ?>
- Alternatively,
short tags can be used, but this is less common and may be disabled on some servers for security reasons.<? ?>
Example:
<?php
echo "Hello, World!";
?>
echo "Hello, World!";
?>
Comments:
- Comments in PHP can be single-line (
//
) or multi-line (/* */
). - Comments are not displayed in the output and are used for code documentation and readability.
Example:
<?php// This is a single-line comment
/*
This is a
multi-line comment
*/
?>
/*
This is a
multi-line comment
*/
?>
2. Variables and Data Types:
Variables:
- Variables in PHP start with a dollar sign (
$
) followed by the variable name. - Variable names are case-sensitive and must start with a letter or underscore.
Example:
<?php
$name = "John";
$age = 25;
?>
$name = "John";
$age = 25;
?>
Data Types:
- PHP supports various data types including strings, integers, floats, booleans, arrays, and objects.
- Data types are dynamically assigned based on the value assigned to a variable.
Example:
<?php
$name = "John"; // String
$age = 25; // Integer
$height = 5.11; // Float
$isStudent = true; // Boolean
?>
$name = "John"; // String
$age = 25; // Integer
$height = 5.11; // Float
$isStudent = true; // Boolean
?>
3. Control Structures:
If-Else Statements:
- If-else statements are used for conditional execution of code blocks based on certain conditions.
Example:
<?php
$grade = 85;
if ($grade >= 60) {
echo "Pass";
} else {
echo "Fail";
}
?>
$grade = 85;
if ($grade >= 60) {
echo "Pass";
} else {
echo "Fail";
}
?>
Loops:
- PHP supports different types of loops including
for
,while
,do-while
, andforeach
loops.
Example (for loop):
<?php
for ($i = 1; $i <= 5; $i++) {
echo $i;
}
?>
for ($i = 1; $i <= 5; $i++) {
echo $i;
}
?>
4. Functions:
Declaring Functions:
- Functions are defined using the
function
keyword followed by the function name and parameters. - Functions can return values using the
return
statement.
Example:
<?php
function greet($name) {
return "Hello, $name!";
}
echo greet("John");
?>
function greet($name) {
return "Hello, $name!";
}
echo greet("John");
?>
Built-in Functions:
- PHP provides a wide range of built-in functions for common tasks such as string manipulation, array operations, date and time handling, etc.
Example:
<?php
$str = "Hello, World!";
echo strtoupper($str); // Outputs: HELLO, WORLD!
?>
$str = "Hello, World!";
echo strtoupper($str); // Outputs: HELLO, WORLD!
?>
5. PHP in HTML:
Embedding PHP in HTML:
- PHP code can be embedded within HTML code using the
tags.<?php ?>
- This allows dynamic generation of HTML content based on server-side logic.
Example:
<!DOCTYPE html>
<html>
<head>
<title>PHP Example</title>
</head>
<body>
<h1><?php echo "Hello, World!"; ?></h1>
</body>
</html>
Using PHP Variables in HTML:
- PHP variables can be directly used within HTML code by placing them within the PHP tags.
Example:
<!DOCTYPE html>
<html>
<head>
<title>PHP Example</title>
</head>
<body>
<p>Welcome, <?php echo $name; ?>!</p>
</body>
</html>
Practice Excercise Practice now