PHP 8 Free Course
PHP 8 Free Course

PHP 8 Free Course – Lesson 2: Basic Syntax

Introduction

Welcome to Lesson 2 of our PHP 8 Free Course! This lesson serves as your gateway into the world of PHP programming. Having acquainted ourselves with the introduction to PHP 8 in the previous lesson, it’s time to dive into the basic syntax, which forms the crux of the PHP language. We’ll explore variables, constants, data types, operators, and control structures, each with illustrative examples to ensure a solid understanding. For a recap of previous lessons or to navigate through the course, visit the PHP 8 Free Course index.

Variables, Constants, and Data Types

Variables and constants are fundamental in PHP, as they allow us to store information. The data stored can be of various types. Here’s a breakdown:

  1. Variables:
  • Variables in PHP start with a $ sign, followed by the name of the variable.
  • Example:
    php $name = "John"; echo $name; // Outputs: John
  1. Constants:
  • Constants are defined using the define() function or the const keyword and unlike variables, they do not start with a $ sign.
  • Example:
    php define("GREETING", "Hello World!"); echo GREETING; // Outputs: Hello World!
  1. Data Types:
  • PHP supports several data types. Let’s delve into some of them with examples:
    • String:
      php $string = "Hello, World!";
    • Integer:
      php $int = 100;
    • Float (floating point numbers, also known as double):
      php $float = 10.365;
    • Boolean:
      php $bool = true;
    • Array:
      php $arr = array("PHP", "Python", "JavaScript");
    • Object:
      php class Car { function Car() { $this->model = "VW"; } } $herbie = new Car(); echo $herbie->model; // Outputs: VW

Operators

Operators are the building blocks for programming logic in PHP. They allow you to perform operations on variables and values. Here are some common operators:

  1. Arithmetic Operators:
   $num1 = 10;
   $num2 = 20;
   echo $num1 + $num2; // Outputs: 30
  1. Assignment Operators:
   $num1 = 10;
   $num1 += 20;
   echo $num1; // Outputs: 30
  1. Comparison Operators:
   $num1 = 10;
   $num2 = 20;
   var_dump($num1 == $num2);  // Outputs: bool(false)
  1. Logical Operators:
   $x = 100;
   $y = 50;
   if ($x == 100 and $y == 50) {
       echo "Hello world!";
   }

Control Structures

Control structures help dictate the flow of execution in a program. Let’s delve into conditional and looping statements:

  1. Conditional Statements:
  • if Statement:
$x = 10;
if ($x > 5) {
    echo "x is greater than 5";
}
  • else and else if Statements:
$x = 10;
if ($x > 20) {
    echo "x is greater than 20";
} elseif ($x > 10) {
    echo "x is greater than 10 but less than 20";
} else {
    echo "x is 10 or less";
}
  • switch Statement:
$day = "Monday";
switch ($day) {
    case "Monday":
        echo "The first day of the week";
        break;
    case "Tuesday":
        echo "The second day of the week";
        break;
    // other cases...
    default:
        echo "Invalid day";
}
  1. Looping Statements:
  • for Loop:
for ($i = 0; $i < 5; $i++) {
    echo $i;
}
  • foreach Loop:
$colors = array("red", "green", "blue");
foreach ($colors as $color) {
    echo $color . " ";
}
// Outputs: red green blue 
  • while and do...while Loops:
$x = 0;
while($x < 5) {
    echo $x;
    $x++;
}

By mastering these fundamental concepts, you’ll be paving the way towards becoming proficient in PHP programming.

Exercises

Exercise 1: Variables and Constants

  1. Create a variable to hold your name and another one to hold your age. Display these variables using echo.
  2. Define a constant to hold the value of pi (3.14159) and display it.

Exercise 2: Data Types

  1. Create variables of each data type (String, Integer, Float, Boolean, and Array) and display them using echo.
  2. Perform an arithmetic operation using an Integer and a Float variable and display the result.

Exercise 3: Operators

  1. Create two variables and perform different arithmetic operations (addition, subtraction, multiplication, and division) on these variables.
  2. Use comparison operators to compare these variables and display the result using echo.

Exercise 4: Control Structures

  1. Create a PHP script that uses an if-else statement to display whether a number is positive, negative, or zero.
  2. Create a switch statement that displays the day of the week based on a number (e.g., 1 for Monday, 2 for Tuesday, etc.).
  3. Create a for loop that displays the numbers 1 to 10.
  4. Create a while loop that displays the numbers 10 to 1.

Exercise 5: Extra Challenge

  1. Create a script that displays the multiplication table up to 10 using nested loops.
  2. Create an array of your favorite fruits. Use a foreach loop to display each fruit on a new line.

External Sources

In wrapping up this lesson, you’ve laid a strong foundation on the basic syntax of PHP which will be instrumental as we delve deeper into more complex topics in the subsequent lessons. For a more thorough understanding, you might want to explore further on Variables, Constants, Data Types, Operators, and Control Structures from the official PHP manual. Your engagement in practicing and reading beyond these basics will be rewarding as you progress through this PHP 8 Free Course. Ready for more? Head over to Lesson 3: Functions to continue your PHP journey. For a recap of previous lessons or to navigate through the course, visit the PHP 8 Free Course index.

This Post Has One Comment

Leave a Reply