Lesson 3: Basic Syntax and PHP Tags

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:
  • // This is a single-line comment
  • Multi-line comments:
  • /*
    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
  • Data Types:
    1. String: Text data. Example: "Hello"
    2. Integer: Whole numbers. Example: 42
    3. Float: Decimal numbers. Example: 3.14
    4. Boolean: true or false.
    5. Array: Collection of values. Example:
    6. $colors = array("Red", "Green", "Blue");
    7. Object: Instances of classes.
    8. NULL: Represents no value.
    9. Resource: Special data type for file and database handlers.

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

  1. Quiz: Multiple-choice questions on PHP basics and features.
  2. Assignment: Create a PHP file that displays a short introduction about yourself, including your name, age, and hobbies.
  3. 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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *