PHP Fundamentals: Control Flow, Arrays, and Functions

Core PHP Concepts

Control Statements

Control statements manage the flow of execution based on conditions or repetitions.

Conditional Statements

  • if...else: Executes code if a condition is true.
  • switch: Selects one of many code blocks to be executed based on a variable’s value.

Looping Statements

  • for: Used when the number of iterations is known.
  • while: Repeats execution as long as a specified condition remains true.
  • foreach: Specifically designed to iterate through arrays.

File Handling Operations

PHP allows you to manipulate files on the server. The standard workflow is: Open → Read/Write → Close.

Opening, Reading, and Writing Files

  • fopen(): Opens a file. Requires a mode (e.g., ‘r’ for read, ‘w’ for write, ‘a’ for append).
  • fread() / fgets(): fread() reads a specific length; fgets() reads a single line.
  • fwrite(): Writes a string to the file.
  • fclose(): Closes the file to free up server resources.
  • file_exists(): Checks if a file exists before attempting to open it.

String Manipulation Techniques

Strings are sequences of characters. PHP treats them as a primary data type with powerful manipulation tools:

  • strlen(): Returns the length of a string.
  • str_replace(): Replaces specified characters with others in a string.
  • strpos(): Finds the position of the first occurrence of a substring.
  • trim(): Removes whitespace from both sides of a string.
  • strtolower() / strtoupper(): Changes the case of the string.

Mastering PHP Arrays

Arrays are one of the most important data structures in PHP. They allow you to store multiple values in a single variable. PHP arrays can hold values of different types, such as strings, numbers, or even other arrays. Understanding how to use arrays in PHP is crucial for working with data efficiently.

PHP offers many built-in array functions for sorting, merging, searching, and more.

Array Characteristics

  • PHP Arrays can store values of different types (e.g., strings, integers, objects, or even other arrays) in the same array.
  • They are dynamically sized.
  • They allow you to store multiple values in a single variable, making it easier to manage related data.

Types of PHP Arrays

  1. Indexed Arrays

    Indexed arrays use numeric indexes starting from 0. These arrays are ideal when you need to store a list of items where the order matters.

  2. Associative Arrays

    Associative arrays use named keys, which are useful when you want to store data with meaningful identifiers instead of numeric indexes.

  3. Multidimensional Arrays

    Multidimensional arrays are arrays that contain other arrays as elements. These are used to represent more complex data structures, such as matrices or tables.

Essential PHP Library Functions

PHP comes with a massive built-in standard library that allows you to handle everything from simple math to complex database interactions without needing external tools.

Here is a brief breakdown of the most commonly used library functions categorized by their purpose:

String Functions

These functions are used to manipulate and analyze text.

  • strlen(): Returns the length of a string.
  • str_replace(): Replaces specific characters or words within a string.
  • strtolower() / strtoupper(): Changes the case of the string.
  • trim(): Removes whitespace from the beginning and end of a string.
  • explode(): Breaks a string into an array based on a delimiter (e.g., splitting a sentence into words).

Array Functions

PHP arrays are very powerful, and these functions help manage them efficiently.

  • count(): Returns the number of elements in an array.
  • array_push(): Adds one or more elements to the end of an array.
  • array_merge(): Combines two or more arrays into one.
  • in_array(): Checks if a specific value exists in an array.
  • sort(): Sorts an array in ascending order.

Math Functions

Used for numerical calculations.

  • abs(): Returns the absolute (positive) value of a number.
  • round(): Rounds a float to the nearest integer.
  • rand(): Generates a random number.
  • max() / min(): Finds the highest or lowest value in a list or array.

Date and Time Functions

Essential for timestamps and scheduling.

  • date(): Formats a local date and time.
  • time(): Returns the current Unix timestamp.
  • strtotime(): Converts a human-readable string (like “next Monday”) into a Unix timestamp.

File System Functions

Used to interact with files on the server.

  • file_exists(): Checks whether a file or directory exists.
  • fopen() / fclose(): Opens and closes file streams.
  • file_get_contents(): Reads an entire file into a string.
  • move_uploaded_file(): Moves an uploaded file to a new location.