Now that you have your PHP development environment set up, it's time to dive into the syntax and basics of the language.

 

In this section, we'll cover the following topics:

  • Writing PHP Code
  • Variables
  • Data Types
  • Operators
  • Control Structures
  • Functions

Writing PHP Code

PHP code is enclosed within special start and end tags that allow you to jump into and out of "PHP mode":

 <?php
  // This is PHP code
?>

Anything outside of the  tags is considered regular HTML markup or content.

PHP files have a .php extension, and you can mix PHP code with HTML in the same file. Here's a simple example:

<!DOCTYPE html>
<html>
<body>

<?php
  echo "Hello, World!"; // Outputs: Hello, World!
?>

</body>
</html>

It's important to note that PHP is a case-sensitive language, meaning $variable is different from $Variable.

Variables

Variables in PHP are used to store data values. They start with a dollar sign $ followed by the variable name.

Here's how you declare and assign a value to a variable:

$name = "Alice";
$age = 25;

You can output the value of a variable using the echo statement:

echo $name; // Outputs: Alice
echo $age; // Outputs: 25

PHP variables are dynamically typed, meaning you don't need to declare the data type explicitly. The type is determined automatically based on the value assigned.

Data Types

PHP supports several data types, including:

  • Strings: Sequences of characters, enclosed in single or double quotes (e.g., "Hello", 'World').
  • Integers: Whole numbers, without decimal parts (e.g., 42, -15).
  • Floats (or doubles): Floating-point numbers (e.g., 3.14, -0.75).
  • Booleans: Logical values, either true or false.
  • Arrays: Collections of values, indexed numerically or associatively.
  • Objects: Instances of classes, used in Object-Oriented Programming (OOP).
  • NULL: Special value representing a non-existent or invalid value.

Operators

PHP supports various operators for performing operations on values and variables. Here are some common operators:

  • Arithmetic Operators: +, -, *, /, % (modulus)
  • Assignment Operators: =, +=, -=, *=, /=
  • String Operators: . (concatenation)
  • Comparison Operators: ==, !=, >, <, >=, <=
  • Logical Operators: && (and), || (or), ! (not)

Control Structures

Control structures in PHP allow you to control the flow of execution in your code. The most common control structures are:

  1. Conditional Statements:
    • if, else, elseif
    • switch
  2. Loops:
    • while
    • do...while
    • for
    • foreach (for arrays and objects)

Here's an example of an if statement:

$age = 18;

if ($age >= 18) {
    echo "You are an adult.";
} else {
    echo "You are a minor.";
}

 

And here's an example of a for loop:

for ($i = 0; $i < 5; $i++) {
    echo "The value of i is: " . $i . "
";
}

Functions

Functions in PHP allow you to define reusable blocks of code that can be called from different parts of your program. You can define your own custom functions, as well as use built-in PHP functions.

Here's how you define a simple function:

function greetUser($name) {
    echo "Hello, $name!";
}

greetUser("Alice"); // Outputs: Hello, Alice!

 

PHP comes with a vast library of built-in functions for various purposes, such as working with strings, arrays, files, dates, and more. For example, strlen() returns the length of a string:

$text = "Hello, World!";
echo strlen($text); // Outputs: 13

 

In the next sections, we'll continue exploring more advanced PHP concepts, including forms, databases, object-oriented programming, and more.