Introduction

In this section, we will cover the basic syntax of PHP and how to work with variables. Understanding these fundamentals is crucial as they form the foundation for writing PHP scripts.

PHP Syntax

Basic PHP Tags

PHP code is embedded within HTML using special tags. The most common tag is <?php ... ?>.

<?php
  // PHP code goes here
?>

Echo Statement

The echo statement is used to output text or variables to the browser.

<?php
  echo "Hello, World!";
?>

Comments

Comments are used to explain code and are ignored by the PHP engine. PHP supports single-line and multi-line comments.

<?php
  // This is a single-line comment

  /*
   * This is a multi-line comment
   * that spans multiple lines.
   */
?>

Variables

Declaring Variables

Variables in PHP are declared using the $ symbol followed by the variable name. Variable names are case-sensitive and must start with a letter or an underscore.

<?php
  $variableName = "value";
?>

Variable Types

PHP is a loosely typed language, meaning you do not need to declare the data type of a variable. PHP automatically converts the variable to the correct data type based on its value.

Examples:

  • String: A sequence of characters.

    <?php
      $stringVar = "Hello, World!";
    ?>
    
  • Integer: A non-decimal number.

    <?php
      $intVar = 42;
    ?>
    
  • Float: A number with a decimal point.

    <?php
      $floatVar = 3.14;
    ?>
    
  • Boolean: Represents two possible states: true or false.

    <?php
      $boolVar = true;
    ?>
    
  • Array: A collection of values.

    <?php
      $arrayVar = array("apple", "banana", "cherry");
    ?>
    
  • Object: An instance of a class.

    <?php
      class Car {
        function Car() {
          $this->model = "VW";
        }
      }
      $carVar = new Car();
    ?>
    

Variable Scope

The scope of a variable determines where it can be accessed within the code. PHP has three main types of variable scope:

  • Local Scope: Variables declared within a function are local to that function.

    <?php
      function myFunction() {
        $localVar = "I'm local";
        echo $localVar;
      }
      myFunction();
      // echo $localVar; // This will cause an error
    ?>
    
  • Global Scope: Variables declared outside of any function have a global scope and can be accessed anywhere in the script.

    <?php
      $globalVar = "I'm global";
    
      function myFunction() {
        global $globalVar;
        echo $globalVar;
      }
      myFunction();
    ?>
    
  • Static Scope: Static variables retain their value even after the function has completed.

    <?php
      function myFunction() {
        static $staticVar = 0;
        echo $staticVar;
        $staticVar++;
      }
      myFunction(); // Outputs: 0
      myFunction(); // Outputs: 1
      myFunction(); // Outputs: 2
    ?>
    

Practical Examples

Example 1: Basic Variable Usage

<?php
  $name = "John";
  $age = 30;
  $isStudent = true;

  echo "Name: " . $name . "<br>";
  echo "Age: " . $age . "<br>";
  echo "Is Student: " . ($isStudent ? "Yes" : "No") . "<br>";
?>

Example 2: Using Arrays

<?php
  $fruits = array("Apple", "Banana", "Cherry");

  echo "I like " . $fruits[0] . ", " . $fruits[1] . " and " . $fruits[2] . ".";
?>

Exercises

Exercise 1: Variable Declaration and Output

Declare variables for your name, age, and favorite color. Output these variables in a sentence.

Solution:

<?php
  $name = "Alice";
  $age = 25;
  $favoriteColor = "blue";

  echo "My name is " . $name . ", I am " . $age . " years old, and my favorite color is " . $favoriteColor . ".";
?>

Exercise 2: Array Manipulation

Create an array of three of your favorite movies. Output the list in a sentence.

Solution:

<?php
  $movies = array("Inception", "The Matrix", "Interstellar");

  echo "My favorite movies are " . $movies[0] . ", " . $movies[1] . ", and " . $movies[2] . ".";
?>

Common Mistakes and Tips

  • Uninitialized Variables: Always initialize your variables before using them to avoid unexpected results.
  • Case Sensitivity: Remember that variable names are case-sensitive. $Var and $var are different variables.
  • Concatenation: Use the . operator to concatenate strings in PHP.

Conclusion

In this section, we covered the basics of PHP syntax and variables. You learned how to declare variables, understand their types, and use them within your scripts. These fundamentals are essential as you progress to more complex topics in PHP. Next, we will explore control structures, which allow you to control the flow of your PHP scripts.

PHP Programming Course

Module 1: Introduction to PHP

Module 2: Control Structures

Module 3: Functions

Module 4: Arrays

Module 5: Working with Forms

Module 6: Working with Files

Module 7: Object-Oriented Programming (OOP)

Module 8: Working with Databases

Module 9: Advanced PHP Techniques

Module 10: PHP Frameworks and Best Practices

Module 11: Project: Building a Web Application

© Copyright 2024. All rights reserved