PHP Reference



 

PHP Reference Guide

This PHP reference guide provides an overview of essential PHP functions, concepts, and syntax. It serves as a quick reference for common tasks and functionalities in PHP development.


1. PHP Syntax

  • Opening and Closing Tags:

    <?php
    // PHP code goes here
    ?>
    
  • Comments:

    // Single-line comment
    # Another single-line comment
    /*
     Multi-line comment
    */
    

2. Variables

  • Declaration:

    $variable_name = value;
    
  • Types:

    • String: $name = "John";
    • Integer: $age = 30;
    • Float: $height = 5.9;
    • Boolean: $is_student = true;

3. Data Types

  • Array:

    $fruits = array("Apple", "Banana", "Cherry");
    
  • Associative Array:

    $person = array("name" => "John", "age" => 30);
    

4. Control Structures

  • If...Else:

    if ($condition) {
        // code to execute if condition is true
    } elseif ($another_condition) {
        // code to execute if another_condition is true
    } else {
        // code to execute if no conditions are true
    }
    
  • Switch:

    switch ($variable) {
        case 'value1':
            // code to execute
            break;
        default:
            // code to execute if no case matches
    }
    
  • Loops:

    • For Loop:

      for ($i = 0; $i < 10; $i++) {
          // code to execute
      }
      
    • While Loop:

      while ($condition) {
          // code to execute
      }
      

5. Functions

  • Function Definition:

    function functionName($parameter) {
        // code to execute
    }
    
  • Returning Values:

    function add($a, $b) {
        return $a + $b;
    }
    

6. Arrays

  • Accessing Elements:

    $fruits[0]; // "Apple"
    
  • Looping Through Arrays:

    foreach ($fruits as $fruit) {
        echo $fruit;
    }
    

7. File Handling

  • Opening a File:

    $file = fopen("filename.txt", "r");
    
  • Reading from a File:

    $content = fread($file, filesize("filename.txt"));
    
  • Writing to a File:

    $file = fopen("filename.txt", "w");
    fwrite($file, "Hello, World!");
    

8. Sessions and Cookies

  • Starting a Session:

    session_start();
    $_SESSION['username'] = 'JohnDoe';
    
  • Setting a Cookie:

    setcookie("user", "John", time() + (86400 * 30), "/");
    

9. Database Interaction

  • Connecting to MySQL:

    $conn = new mysqli($servername, $username, $password, $dbname);
    
  • Executing a Query:

    $result = $conn->query("SELECT * FROM users");
    

10. Common Functions

  • String Functions:

    • strlen($string): Returns the length of a string.
    • str_replace($search, $replace, $subject): Replaces all occurrences of a search string with a replacement string.
  • Array Functions:

    • count($array): Returns the number of elements in an array.
    • array_push($array, $value): Adds one or more elements to the end of an array.
  • Mathematical Functions:

    • abs($number): Returns the absolute value of a number.
    • round($number, $precision): Rounds a number to a specified precision.

Conclusion

This reference guide covers fundamental aspects of PHP, including syntax, control structures, functions, file handling, and database interactions. Use this guide as a quick reference while developing PHP applications. For more in-depth information, you can always refer to the official PHP documentation.