PHP Syntax

Welcome to The Coding College, where we simplify programming for learners at all levels. In this guide, we’ll cover the basics of PHP syntax, laying the foundation for you to write your own PHP scripts and build dynamic websites.

PHP is known for its simplicity, making it one of the easiest server-side languages to learn. Let’s dive into the essential syntax rules and examples to get you started!

PHP Basics

1. PHP Tags

PHP code is embedded within HTML using special tags:

<?php
  // PHP code goes here
?>

Alternatively, you can use the shorthand:

<?= "Hello, World!" ?>

Note: Always use <?php ?> for better compatibility, as shorthand tags may not be enabled on all servers.

2. Case Sensitivity

  • PHP keywords (e.g., if, else, echo) are not case-sensitive.
  • Variable names are case-sensitive.

For example:

<?php
  $Name = "John";
  echo $Name; // Works
  echo $name; // Error: Undefined variable
?>

Writing Your First PHP Script

Create a file named hello.php:

<?php
  echo "Welcome to The Coding College!";
?>

Save it in your server directory (e.g., htdocs for XAMPP) and open it in a browser by navigating to http://localhost/hello.php.

PHP Syntax Elements

1. Comments

Comments help you annotate your code without affecting execution.

  • Single-line comment:
// This is a single-line comment
  • Multi-line comment:
/*
  This is a
  multi-line comment
*/

2. Variables

Variables store data and are declared using the $ symbol.

<?php
  $name = "The Coding College";
  echo $name;
?>

Rules for Variables:

  • Must start with $.
  • Can contain letters, numbers, and underscores.
  • Cannot start with a number.

3. Data Types

PHP supports multiple data types:

  • String: Textual data, e.g., "Hello, World!".
  • Integer: Whole numbers, e.g., 42.
  • Float: Decimal numbers, e.g., 3.14.
  • Boolean: true or false.
  • Array: A collection of values.
  • Object: Instance of a class.

Example:

<?php
  $string = "Hello, PHP!";
  $integer = 25;
  $float = 3.14;
  $boolean = true;

  echo $string . " I am " . $integer . " years old.";
?>

4. Operators

PHP supports various operators:

Arithmetic Operators

<?php
  $a = 10;
  $b = 2;
  echo $a + $b; // Addition: 12
  echo $a - $b; // Subtraction: 8
  echo $a * $b; // Multiplication: 20
  echo $a / $b; // Division: 5
?>

Comparison Operators

<?php
  $a = 10;
  $b = 20;
  echo $a == $b; // false
  echo $a < $b;  // true
?>

Logical Operators

<?php
  $a = true;
  $b = false;
  echo $a && $b; // false
  echo $a || $b; // true
?>

5. Control Structures

Conditional Statements

<?php
  $age = 18;
  if ($age >= 18) {
      echo "You are an adult.";
  } else {
      echo "You are not an adult.";
  }
?>

Loops

For Loop:

<?php
  for ($i = 1; $i <= 5; $i++) {
      echo $i . " ";
  }
?>

While Loop:

<?php
  $i = 1;
  while ($i <= 5) {
      echo $i . " ";
      $i++;
  }
?>

6. Functions

Functions are reusable blocks of code.

Defining and Calling Functions:

<?php
  function greet($name) {
      return "Hello, " . $name . "!";
  }

  echo greet("The Coding College");
?>

7. PHP and HTML

PHP can be embedded within HTML to create dynamic content.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>Welcome</title>
</head>
<body>
  <h1><?php echo "Welcome to The Coding College!"; ?></h1>
</body>
</html>

Best Practices for Writing PHP Code

  1. Indentation and Formatting: Use proper indentation to make your code readable.
  2. Use Descriptive Variable Names: Avoid vague names like $x or $y.
  3. Comment Your Code: Add comments to explain complex logic.
  4. Error Reporting: Enable error reporting during development:
ini_set('display_errors', 1);
error_reporting(E_ALL);

Conclusion

Understanding PHP syntax is the first step toward creating dynamic and interactive websites. By mastering variables, operators, control structures, and functions, you’ll be equipped to handle real-world development tasks.

Start coding today, and let The Coding College be your partner in success!

Leave a Comment