Chapter 2: PHP Syntax and Variables: Understanding the Basics
In this chapter, we will delve into the fundamentals of PHP syntax and variables. We will cover different data types, operators and expressions, conditional statements, and working with loops.
2.1 Variables and data types in PHP:
Variables in PHP are used to store values that can be manipulated and accessed throughout the program.
PHP supports various data types such as strings, integers, floats, booleans, arrays, and objects.
Example:
php
""
<?php
$name = "John Doe"; // string data type
$age = 25; // integer data type
$height = 1.75; // float data type
$isStudent = true; // boolean data type
$fruits = array("apple", "banana", "orange"); // array data type
echo $name; // Output: John Doe
echo $age; // Output: 25
echo $height; // Output: 1.75
echo $isStudent; // Output: 1 (true)
echo $fruits[0]; // Output: apple
?>
2.2 Operators and expressions:
PHP supports various operators, including arithmetic, assignment, comparison, logical, and string concatenation operators.
Expressions are formed by combining variables, values, and operators to perform operations and produce results.
Example:
php
""
<?php
$num1 = 10;
$num2 = 5;
$sum = $num1 + $num2; // Addition operator
echo $sum; // Output: 15
$result = ($num1 > $num2) ? "Greater" : "Smaller"; // Ternary operator
echo $result; // Output: Greater
$concatenated = "Hello" . " " . "World!"; // String concatenation
echo $concatenated; // Output: Hello World!
?>
2.3 Conditional statements: if, else, and switch:
Conditional statements allow you to make decisions based on certain conditions.
The if statement executes a block of code if a condition is true. The else statement allows an alternate block of code to be executed if the condition is false.
The switch statement provides a way to select one of many blocks of code to be executed based on different cases.
Example:
php
""
<?php
$marks = 85;
if ($marks >= 90) {
echo "Excellent!";
} elseif ($marks >= 70) {
echo "Good!";
} else {
echo "Average!";
}
// Output: Good!
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the week";
break;
case "Friday":
echo "End of the week";
break;
default:
echo "Somewhere in between";
break;
}
// Output: Start of the week
?>
2.4 Working with loops: for, while, and foreach:
Loops enable you to repeatedly execute a block of code as long as a certain condition is met.
The for loop allows you to specify a starting point, a condition, and an increment or decrement value.
The while loop executes a block of code as long as a condition is true.
The foreach loop is specifically designed for iterating over arrays.
Example:
php
""
<?php
// For loop
for ($i = 1; $i <= 5; $i++) {
echo $i . " ";
}
// Output: 1 2 3 4 5
// While loop
$count = 1;
while ($count <= 5) {
echo $count . " ";
$count++;
}
// Output: 1 2 3 4 5
// Foreach loop
$fruits = array("apple", "banana", "orange");
foreach ($fruits as $fruit) {
echo $fruit . " ";
}
// Output: apple banana orange
?>
By following the explanations and examples in this chapter, you will gain a solid understanding of PHP syntax and variables, allowing you to manipulate and work with data effectively.
0 comments:
Post a Comment