Lesson 3: Basic Syntax and PHP Tags

3.1 PHP echo and print

          • Outputs one or more strings.
          • Example:
          • <?php
            echo("Hello World!");
            ?>
  • print:
      • Outputs a string and returns a value (1 for success).
      • Example:
      • <?php
        print “Hello, PHP!”;
        ?>

3.2 Comments in PHP

    • Single-line comments:
<?php
// This is a single-line comment
?>
    • Multi-line comments:
<?php
/*
This is a multi-line comment.
Used for detailed explanations.
*/
?>

3.3 Variables and Data Types

  • Variables:
      • Start with $ and are case-sensitive.
      • Example:
    <?php
    $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:
    <?php
    $colors = array(“Red”, “Green”, “Blue”);
    ?>
    1. Object: Instances of classes.
    2. NULL: Represents no value.
    3. Resource: Special data type for file and database handlers.

Example: Combining Concepts

<?php
“;
echo “Age: $age
“;
echo “Is a student: ” . ($is_student ? “Yes” : “No”);
?>
?>

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 *