Beginner's Guide to PHP

  ·   20 min read

Introduction to PHP #

What is PHP? #

PHP (Hypertext Preprocessor) is a widely-used, open-source, server-side scripting language embedded in HTML. This means PHP code is executed on the web server, not the user’s computer. The server processes the PHP code and sends the resulting HTML to the user’s browser, dynamically generating web pages. Unlike client-side languages like JavaScript, which run in the user’s browser, PHP interacts directly with databases and server resources to create interactive and data-driven websites.

Why Use PHP? #

PHP offers several compelling reasons for its continued popularity:

  • Server-Side Scripting: PHP excels at handling server-side logic, enabling you to create dynamic web pages that interact with databases, process forms, and manage user sessions.
  • Large Community & Resources: A vast and active community supports PHP, offering ample resources, tutorials, and frameworks. Finding solutions to problems and getting help is relatively easy.
  • Ease of Use: PHP has a relatively straightforward syntax, making it easier to learn than some other server-side languages. Many readily available frameworks simplify development even further.
  • Open Source & Free: PHP is free to use and distribute, eliminating licensing costs.
  • Cross-Platform Compatibility: PHP runs on various operating systems, including Windows, Linux, and macOS, offering flexibility in deployment.
  • Extensive Libraries & Frameworks: Numerous libraries and frameworks (like Laravel, Symfony, CodeIgniter) extend PHP’s capabilities, providing pre-built components and structures for efficient development.

Setting up your environment #

To start programming in PHP, you’ll need a few things:

  1. A Web Server: You need a web server like Apache or Nginx to run PHP code. XAMPP or WAMP are popular all-in-one packages that include a web server, PHP interpreter, and MySQL database – perfect for beginners. These packages install easily on Windows, macOS, and Linux.

  2. PHP Interpreter: This is the software that translates your PHP code into instructions the web server can understand. It’s usually bundled with web server packages like XAMPP and WAMP.

  3. A Text Editor or IDE: You’ll need a text editor (like Notepad++, Sublime Text, VS Code) or an Integrated Development Environment (IDE, like PhpStorm) to write your PHP code. IDEs offer features like code highlighting, debugging, and autocompletion, greatly enhancing the development experience.

  4. Database (Optional but Recommended): While not strictly necessary for your first programs, a database (like MySQL, included in XAMPP/WAMP) is essential for most real-world web applications. It’s used to store and retrieve data.

After installing your chosen web server package (e.g., XAMPP), ensure the PHP interpreter is correctly configured. You might need to adjust settings in the web server’s configuration files depending on your setup, but often the default configuration works fine for beginners.

Hello World! Your first PHP program. #

The classic first program in any language is “Hello, World!”:

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

Save this code in a file named (for example) hello.php within your web server’s document root directory (usually htdocs in XAMPP). Then, access the file through your web browser by entering its URL (something like http://localhost/hello.php if you’re using XAMPP). You should see “Hello, World!” displayed in your browser. This simple program demonstrates the fundamental PHP syntax – the code is enclosed within <?php and ?> tags, and the echo statement displays output.

Basic Syntax and Data Types #

Variables and Data Types #

PHP is a loosely typed language, meaning you don’t explicitly declare the data type of a variable. The type is determined by the value assigned to it. Variable names start with a dollar sign ($) followed by a letter or underscore, and can contain letters, numbers, and underscores.

Common Data Types:

  • Integer (int): Whole numbers (e.g., 10, -5, 0).
  • Float (float/double): Floating-point numbers (e.g., 3.14, -2.5).
  • String (string): Sequences of characters (e.g., “Hello”, ‘PHP’). Strings can be enclosed in single (’ ‘) or double (" “) quotes.
  • Boolean (bool): Represents true or false values.
  • Array (array): Ordered collections of values.
  • Null (null): Represents the absence of a value.

Example:

<?php
  $name = "John Doe"; // String
  $age = 30;       // Integer
  $height = 5.10;   // Float
  $isAdult = true;  // Boolean
  $colors = array("red", "green", "blue"); // Array
  $emptyVar = null; // Null
?>

Operators #

PHP supports a wide range of operators:

  • Arithmetic Operators: +, -, *, /, % (modulo), ** (exponentiation).
  • Assignment Operators: =, +=, -=, *=, /=, %=, .=.
  • Comparison Operators: == (equal to), != (not equal to), === (identical to), !== (not identical to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to).
  • Logical Operators: && (AND), || (OR), ! (NOT).
  • Increment/Decrement Operators: ++, --.

Example:

<?php
  $x = 10;
  $y = 5;
  $sum = $x + $y;       // Addition
  $diff = $x - $y;      // Subtraction
  $product = $x * $y;    // Multiplication
  $quotient = $x / $y;   // Division
  $remainder = $x % $y;  // Modulo
  $isGreater = ($x > $y); // Comparison
?>

Comments #

Comments are used to explain code and make it more readable. PHP supports single-line and multi-line comments:

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

Example:

<?php
  // This is a single-line comment
  $name = "Alice";  // Assigning a value to the $name variable

  /* This is a
     multi-line comment */
  $age = 25;
?>

Control Structures (if, else, switch) #

Control structures determine the flow of execution in your code.

  • if statement: Executes a block of code only if a condition is true.
<?php
  $age = 20;
  if ($age >= 18) {
    echo "You are an adult.";
  }
?>
  • if-else statement: Executes one block of code if a condition is true, and another block if it’s false.
<?php
  $age = 15;
  if ($age >= 18) {
    echo "You are an adult.";
  } else {
    echo "You are a minor.";
  }
?>
  • if-elseif-else statement: Allows checking multiple conditions sequentially.
<?php
  $grade = 85;
  if ($grade >= 90) {
    echo "A";
  } elseif ($grade >= 80) {
    echo "B";
  } elseif ($grade >= 70) {
    echo "C";
  } else {
    echo "F";
  }
?>
  • switch statement: Provides a more concise way to handle multiple conditions based on the value of a single expression.
<?php
  $day = "Monday";
  switch ($day) {
    case "Monday":
      echo "It's the start of the week.";
      break;
    case "Friday":
      echo "Almost weekend!";
      break;
    default:
      echo "It's another day.";
  }
?>

The break statement is crucial in switch statements; it prevents the code from “falling through” to the next case. Without break, the code will execute all subsequent cases until a break or the end of the switch is reached.

Working with Data #

Arrays #

Arrays in PHP are ordered maps. They can hold multiple values of different data types. PHP offers both indexed and associative arrays.

  • Indexed Arrays: Elements are accessed by their numerical index (starting from 0).
<?php
  $numbers = [10, 20, 30, 40, 50]; //Using short array syntax (PHP 5.4+)
  echo $numbers[0]; // Output: 10
  echo $numbers[2]; // Output: 30

  $fruits = array("apple", "banana", "orange"); //Older array syntax
  echo $fruits[1]; // Output: banana
?>
  • Associative Arrays: Elements are accessed by their key (which can be a string).
<?php
  $person = [
    "name" => "Alice",
    "age" => 30,
    "city" => "New York"
  ];
  echo $person["name"]; // Output: Alice
  echo $person["age"];  // Output: 30
?>

Arrays can be multidimensional (arrays within arrays):

<?php
  $matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ];
  echo $matrix[1][2]; // Output: 6
?>

Many built-in functions are available for manipulating arrays (e.g., count(), array_push(), sort(), in_array()).

Strings #

Strings in PHP are sequences of characters. They can be defined using single quotes (’ ‘) or double quotes (” “). Double-quoted strings allow for variable interpolation (embedding variables directly within the string).

<?php
  $name = "Bob";
  $greeting = 'Hello, ' . $name . '!'; // Concatenation using the . operator
  $greeting2 = "Hello, $name!";       // Variable interpolation
  echo $greeting;  // Output: Hello, Bob!
  echo $greeting2; // Output: Hello, Bob!
?>

Many string manipulation functions are available (e.g., strlen(), strpos(), substr(), str_replace()).

Numbers #

PHP supports integers and floating-point numbers. Be mindful of potential precision issues with floating-point numbers due to how they’re represented internally.

<?php
  $integer = 123;
  $float = 3.14159;
  $sum = $integer + $float;
  echo $sum; // Output: 126.14159
?>

Mathematical functions are available (e.g., abs(), round(), sqrt(), pow()).

Type Juggling and Type Casting #

PHP’s loose typing allows for type juggling, where PHP automatically converts data types during operations. However, it’s often better to use type casting for explicit type conversion to avoid unexpected behavior.

Type Juggling:

<?php
  $x = "5";
  $y = 10;
  $sum = $x + $y; // $x is automatically converted to an integer (type juggling)
  echo $sum;     // Output: 15
?>

Type Casting:

<?php
  $x = "5";
  $y = 10;
  $sum = (int)$x + $y; // Explicit type casting of $x to an integer
  echo $sum;           // Output: 15

  $floatString = "3.14";
  $floatNumber = (float)$floatString; //Casting to float
  echo $floatNumber; //Output: 3.14

  $boolInt = (int) true; //Casting boolean to integer - true becomes 1
  echo $boolInt; //Output: 1
?>

Type casting ensures clarity and avoids potential errors arising from implicit type conversions. Common casting functions include (int), (float), (string), (bool), (array). Always cast to the intended type.

Functions #

Functions are reusable blocks of code that perform specific tasks. They improve code organization, readability, and maintainability.

Defining Functions #

Functions are defined using the function keyword, followed by the function name, parentheses (), and curly braces {} enclosing the function’s code.

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

  greet("Alice"); // Calling the function
?>

The function name should follow the same rules as variable names (start with a letter or underscore, contain letters, numbers, and underscores). It’s good practice to use descriptive names that clearly indicate the function’s purpose.

Function Arguments #

Arguments (or parameters) are values passed to a function. They are defined within the parentheses in the function definition.

<?php
  function add($x, $y) {
    return $x + $y;
  }

  $sum = add(5, 3); // Passing arguments to the function
  echo $sum;       // Output: 8
?>

You can have multiple arguments, separated by commas. Arguments can be of any data type.

Return Values #

Functions can return a value using the return statement. If a function doesn’t have an explicit return statement, it implicitly returns null.

<?php
  function getArea($width, $height) {
    return $width * $height;
  }

  $area = getArea(10, 5);
  echo $area; // Output: 50
?>

The return statement immediately exits the function. Any code after a return statement within the function will not be executed.

Function Scope #

Scope refers to the accessibility of variables within a program. Variables declared inside a function have local scope, meaning they are only accessible within that function. Variables declared outside functions have global scope, accessible from anywhere in the script.

<?php
  $globalVar = "I'm global"; // Global variable

  function myFunction() {
    $localVar = "I'm local"; // Local variable
    echo $globalVar; // Accessing global variable from inside the function
    echo $localVar;  // Accessing local variable
  }

  myFunction();
  // echo $localVar; // This would cause an error because $localVar is out of scope here
  echo $globalVar;  // Accessing global variable outside the function
?>

To access a global variable from within a function, you can use the global keyword:

<?php
  $globalVar = "I'm global";

  function accessGlobal() {
    global $globalVar;
    echo $globalVar;
  }

  accessGlobal(); // Outputs: I'm global
?>

Using global variables within functions can make code harder to understand and maintain; it’s generally better to pass data to functions as arguments and return values rather than relying heavily on global variables. This promotes better encapsulation and reduces the risk of unintended side effects.

Working with Files #

PHP provides a robust set of functions for working with files on the server’s file system. This allows you to read data from files, write data to files, and perform various other file operations. Remember that file operations require appropriate permissions on the server.

Reading Files #

The most common way to read a file is using file_get_contents(), which reads the entire file into a single string:

<?php
  $filename = "my_file.txt";
  $fileContent = file_get_contents($filename);
  if ($fileContent === false) {
    echo "Error reading file.";
  } else {
    echo $fileContent;
  }
?>

For larger files, reading line by line using fopen(), fgets(), and fclose() is more efficient:

<?php
  $filename = "my_large_file.txt";
  $fileHandle = fopen($filename, "r"); // "r" for reading
  if ($fileHandle === false) {
    echo "Error opening file.";
  } else {
    while (($line = fgets($fileHandle)) !== false) {
      echo $line; // Process each line individually
    }
    fclose($fileHandle); // Always close the file handle
  }
?>

fopen() opens the file, fgets() reads one line at a time, and fclose() closes the file (crucial to release resources).

Writing Files #

To write to a file, use file_put_contents():

<?php
  $filename = "output.txt";
  $data = "This is some text to write to the file.";
  $bytesWritten = file_put_contents($filename, $data);
  if ($bytesWritten === false) {
    echo "Error writing to file.";
  } else {
    echo "Successfully wrote $bytesWritten bytes to the file.";
  }
?>

This overwrites the file if it exists. To append data to an existing file, use the FILE_APPEND flag:

<?php
  $filename = "output.txt";
  $newData = "\nThis is additional text."; //Add a newline character at the beginning
  $bytesAppended = file_put_contents($filename, $newData, FILE_APPEND);
  if ($bytesAppended === false) {
    echo "Error appending to file.";
  } else {
    echo "Successfully appended $bytesAppended bytes to the file.";
  }
?>

For more controlled writing (e.g., writing line by line), use fopen() with "w" (write) or "a" (append) mode, fwrite(), and fclose():

<?php
  $filename = "another_output.txt";
  $fileHandle = fopen($filename, "w"); // "w" for writing (overwrites)
  if ($fileHandle === false) {
    echo "Error opening file.";
  } else {
    $lines = ["Line 1\n", "Line 2\n", "Line 3\n"];
    foreach ($lines as $line) {
      fwrite($fileHandle, $line);
    }
    fclose($fileHandle);
  }
?>

File Handling Functions #

PHP provides many functions for file manipulation:

  • fopen($filename, $mode): Opens a file. $mode specifies the access mode (e.g., “r” for reading, “w” for writing, “a” for appending, “x” for exclusive creation).
  • fclose($handle): Closes a file handle. Always close files when finished.
  • fgets($handle): Reads a single line from a file.
  • fread($handle, $length): Reads a specified number of bytes from a file.
  • fwrite($handle, $data): Writes data to a file.
  • file_exists($filename): Checks if a file exists.
  • filesize($filename): Gets the size of a file in bytes.
  • unlink($filename): Deletes a file.
  • rename($oldName, $newName): Renames a file.
  • is_readable($filename): Checks if a file is readable.
  • is_writable($filename): Checks if a file is writable.
  • file_get_contents($filename): Reads the entire file into a string.
  • file_put_contents($filename, $data): Writes a string to a file.

Remember to always handle potential errors (e.g., file not found, permission issues) using appropriate error checking (e.g., checking the return values of functions). Always close file handles using fclose() to prevent resource leaks.

Object-Oriented Programming (OOP) Basics #

Object-Oriented Programming (OOP) is a powerful programming paradigm that organizes code around “objects” rather than actions and data. PHP supports OOP, allowing you to create reusable and well-structured code.

Classes and Objects #

A class is a blueprint for creating objects. It defines the properties (data) and methods (functions) that objects of that class will have. An object is an instance of a class.

<?php
  class Dog {
    // Properties and methods will go here
  }

  $myDog = new Dog(); // Creating an object (instance) of the Dog class
?>

This code defines a Dog class and creates an object ($myDog) of that class. The object is an instance of the blueprint defined by the class.

Properties and Methods #

Properties are variables that hold data within an object. Methods are functions that operate on the object’s data.

<?php
  class Dog {
    public $name;       // Property: the dog's name
    public $breed;      // Property: the dog's breed
    public $age;        //Property: the dog's age

    public function bark() { // Method: makes the dog bark
      echo "Woof!";
    }

    public function introduce() { //Method: introduces the dog
      echo "My name is " . $this->name . ", I am a " . $this->breed . ", and I am " . $this->age . " years old.\n";
    }
  }

  $myDog = new Dog();
  $myDog->name = "Buddy";
  $myDog->breed = "Golden Retriever";
  $myDog->age = 3;
  $myDog->bark(); // Output: Woof!
  $myDog->introduce(); //Output: My name is Buddy, I am a Golden Retriever, and I am 3 years old.

?>

$this refers to the current object. Access properties and methods using the -> operator (object operator). The public access modifier makes the properties and methods accessible from anywhere. Other access modifiers include private (accessible only within the class) and protected (accessible within the class and its subclasses).

Constructors and Destructors #

A constructor is a special method that is automatically called when an object is created. It’s used to initialize the object’s properties. A destructor is a special method that is automatically called when an object is destroyed (when it’s no longer needed).

<?php
  class Dog {
    public $name;
    public $breed;

    public function __construct($name, $breed) { // Constructor
      $this->name = $name;
      $this->breed = $breed;
    }

    public function __destruct() { // Destructor
      echo "Dog " . $this->name . " is being destroyed.\n";
    }
  }

  $myDog = new Dog("Lucy", "Labrador");
  // ... use the object ...
  //When the script finishes, the destructor is called automatically.

?>

The constructor takes the dog’s name and breed as arguments and initializes the properties. The destructor prints a message when the object is destroyed. Constructor and destructor names are predefined (__construct and __destruct).

Inheritance #

Inheritance allows you to create new classes (child classes or subclasses) based on existing classes (parent classes or superclasses). The child class inherits the properties and methods of the parent class and can add its own properties and methods or override existing ones.

<?php
  class Animal { // Parent class
    public $name;
    public function makeSound() {
      echo "Generic animal sound\n";
    }
  }

  class Cat extends Animal { // Child class inheriting from Animal
    public function makeSound() { //Overriding the method
      echo "Meow!\n";
    }
  }

  $myCat = new Cat();
  $myCat->name = "Whiskers";
  $myCat->makeSound(); // Output: Meow!
?>

The Cat class inherits name and makeSound() from Animal but overrides makeSound() to provide cat-specific behavior. extends keyword indicates inheritance. Inheritance promotes code reusability and reduces redundancy.

Connecting to Databases #

This section covers connecting to and interacting with MySQL databases, a popular choice for web applications. Remember that database interaction requires appropriate server-side configuration and user credentials.

Introduction to MySQL #

MySQL is a widely used open-source Relational Database Management System (RDBMS). It stores data in tables with rows (records) and columns (fields). Data is organized and accessed efficiently using Structured Query Language (SQL). MySQL is often used alongside PHP for dynamic web applications, where PHP handles the application logic and MySQL manages the persistent data.

Connecting to a MySQL database #

To connect to a MySQL database from PHP, you use the mysqli extension (or the older mysql extension, though mysqli is recommended for improved security and features).

<?php
  // Database credentials
  $servername = "localhost";
  $username = "your_username";
  $password = "your_password";
  $dbname = "your_database_name";

  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);

  // Check connection
  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  }
  echo "Connected successfully";

  //Remember to close the connection when finished:
  $conn->close();
?>

Replace "your_username", "your_password", and "your_database_name" with your actual credentials. Error handling is crucial to catch connection problems. The mysqli object provides methods for executing SQL queries.

Performing basic CRUD operations #

CRUD stands for Create, Read, Update, and Delete – the four fundamental operations in database management.

Create (INSERT):

<?php
  // ... (connection code from above) ...

  $sql = "INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com')";
  if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
  } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
  }

  // ... $conn->close(); ...
?>

This inserts a new row into the users table.

Read (SELECT):

<?php
  // ... (connection code) ...

  $sql = "SELECT name, email FROM users";
  $result = $conn->query($sql);

  if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
      echo "Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
    }
  } else {
    echo "0 results";
  }

  // ... $conn->close(); ...
?>

This selects data from the users table.

Update (UPDATE):

<?php
  // ... (connection code) ...

  $sql = "UPDATE users SET email='john.updated@example.com' WHERE name='John Doe'";
  if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
  } else {
    echo "Error updating record: " . $conn->error;
  }

  // ... $conn->close(); ...
?>

This updates an existing row in the users table.

Delete (DELETE):

<?php
  // ... (connection code) ...

  $sql = "DELETE FROM users WHERE name='John Doe'";
  if ($conn->query($sql) === TRUE) {
    echo "Record deleted successfully";
  } else {
    echo "Error deleting record: " . $conn->error;
  }

  // ... $conn->close(); ...
?>

This deletes a row from the users table. Always use WHERE clauses to avoid accidentally deleting all data.

Security considerations #

Database security is paramount. Never hardcode database credentials directly in your code; use environment variables or configuration files to store sensitive information securely.

  • Prepared Statements: Use prepared statements to prevent SQL injection vulnerabilities. Prepared statements separate SQL code from user-supplied data, preventing malicious code from being executed.

  • Input Sanitization: Always sanitize user inputs before using them in SQL queries to prevent injection attacks.

  • Least Privilege: Grant database users only the necessary permissions. Avoid granting excessive privileges to prevent unauthorized access.

  • Stored Procedures: Use stored procedures to encapsulate database logic and enhance security.

  • Regular Security Audits: Conduct regular security audits to identify and address potential vulnerabilities.

  • Output Encoding: Encode output to prevent Cross-Site Scripting (XSS) vulnerabilities.

Ignoring these security best practices can lead to significant security risks and data breaches. Always prioritize secure coding practices when working with databases.

Further Learning #

This section points you towards resources and concepts to expand your PHP knowledge beyond the basics.

Advanced PHP concepts #

Once you’re comfortable with the fundamentals, explore these advanced topics:

  • Object-Oriented Programming (OOP) Design Patterns: Learn common OOP design patterns like Singleton, Factory, Observer, etc., to build more robust and maintainable applications. These patterns provide reusable solutions to recurring design problems.

  • Namespaces: Organize your code effectively using namespaces to avoid naming conflicts, especially in larger projects.

  • Error Handling and Exception Handling: Implement robust error and exception handling mechanisms to gracefully manage errors and prevent unexpected application crashes. Learn to use try-catch blocks effectively.

  • Working with Sessions and Cookies: Understand how to manage user sessions and use cookies to store user-specific data across multiple requests.

  • Working with Dates and Times: Learn to manipulate dates and times effectively using PHP’s date and time functions.

  • Regular Expressions: Master regular expressions to perform powerful pattern matching and text manipulation.

  • Advanced Array Functions: Explore PHP’s extensive array functions to efficiently process and manipulate data.

  • Working with different Databases: Learn how to connect to and interact with other database systems beyond MySQL, such as PostgreSQL, SQLite, or MongoDB.

  • Command-Line Interface (CLI) scripting: Use PHP to write command-line scripts for automating tasks and interacting with the operating system.

Frameworks (e.g., Laravel, Symfony) #

Frameworks provide a structured way to build web applications, offering pre-built components, tools, and best practices. Learning a framework significantly accelerates development and promotes code quality.

  • Laravel: A popular, elegant, and developer-friendly framework known for its ease of use and extensive ecosystem of packages.

  • Symfony: A mature and powerful framework offering a highly flexible and modular architecture suitable for large-scale projects.

  • CodeIgniter: A lightweight framework suitable for smaller projects and those who prefer a simpler approach.

  • CakePHP: A framework emphasizing convention over configuration, making it easier to learn and use.

Choosing a framework depends on project requirements, team expertise, and personal preference. Many online resources, tutorials, and documentation are available for each framework.

Resources and Community #

Numerous resources are available to help you continue learning:

  • PHP.net: The official PHP website, containing comprehensive documentation, tutorials, and news about the language.

  • Online Courses: Platforms like Udemy, Coursera, and edX offer structured PHP courses, ranging from beginner to advanced levels.

  • Books: Many excellent books cover PHP programming, from introductory guides to advanced techniques.

  • Online Communities: Engage with the PHP community on forums (e.g., Stack Overflow), online groups, and social media. Seeking help and sharing knowledge are essential parts of the learning process.

  • Contributing to Open Source Projects: Contributing to open-source PHP projects is an excellent way to gain practical experience and improve your skills. This allows you to see how real-world PHP projects are structured and developed.

Continuous learning and engagement with the PHP community are vital for staying up-to-date with new features, best practices, and solving challenging problems. Don’t be afraid to experiment, try new things, and ask for help when you need it.