3.1 PHP echo and print
- echo:
- Outputs one or more strings.
- Example:
echo "Hello, World!";
- print:
- Outputs a string and returns a value (1 for success).
- Example:
print "Hello, PHP!";
3.2 Comments in PHP
- Single-line comments:
- Multi-line comments:
// This is a single-line comment
/*
This is a multi-line comment.
Used for detailed explanations.
*/
3.3 Variables and Data Types
- Variables:
- Start with
$
and are case-sensitive. - Example:
$name = "John";
echo $name; // Outputs: John - Start with
- Data Types:
- String: Text data. Example:
"Hello"
- Integer: Whole numbers. Example:
42
- Float: Decimal numbers. Example:
3.14
- Boolean:
true
orfalse
. - Array: Collection of values. Example:
- Object: Instances of classes.
- NULL: Represents no value.
- Resource: Special data type for file and database handlers.
$colors = array("Red", "Green", "Blue");
- String: Text data. Example:
Example: Combining Concepts
// Variables and Data Types
$name = "Alice";
$age = 25;
$is_student = true;
// Output
echo "Name: $name
";
echo "Age: $age
";
echo "Is a student: " . ($is_student ? "Yes" : "No");
Output:
Name: Alice
Age: 25
Is a student: Yes
Activities and Exercises
- Quiz: Multiple-choice questions on PHP basics and features.
- Assignment: Create a PHP file that displays a short introduction about yourself, including your name, age, and hobbies.
- Hands-On:
- Create a script that calculates the area of a rectangle.
- Write a script that outputs the current date and time using the
date()
function.
Leave a Reply