PHP (PHP Hypertext Preprocessor)

PHP(PHP Hypertext Preprocessor)
This article assumes you have basic to intermediate knowledge of PHP

File handling functions:-

  • fopen => $hf = fopen($html_file,"w");
  • fwrite => fwrite($hf, $htmlcontent);
  • fclose => fclose($hf);
  • unlink => unlink($html_file_path);
  • filesize => filesize($file_path);
  • chmod => chmod($file_path, 0777);

Arrays

  • To create an empty array, use the array function $empty_array = array();
  • To create an array of a single element, pass the name of the variable prefixed by the $ symbol as an argument to the array function $params = array($id);

JSON

  • To create an array of multiple elements, just pass the multiple variables arguments to the array function
  1. PHP supports two functions for dealing with JSON data
  2. To encode a variable either primitive or object, we use json_encode($obj) function
  3. To convert the data in JSON format into a object or primitive value, we use the json_decode function which has one argument denoting the JSON string and converting that into a primitive value or object
  4. It has an optional second parameter which is of type Boolean. if set to true, it converts the JSON into an associative array containing both keys and values
  5. The default value of the second argument is false

foreach loop

  • Since PHP supports associative arrays, there needs a technique to parse both keys and their respective classes
  • The syntax of the foreach loop is as follows:
  • foreach($associative_array as $key => $value) {
      	echo “Key: “ . $key;
    	echo “Value:” . $value;
    }
    

  • Include and Require

    • Include and require are two techniques to include a php file in another php file
  • Four commands are as follows:
    • include 'test.php'
    • Inserts the script written in test.php in the file in which it is written
    • The inclusion is optional meaning no syntax error will be thrown if the file is not included
    • A warning message will be displayed if test.php is not existing
    • include_once 'test.php'
    • Inserts the script only once in test.php in the file in which it is written
    • The inclusion is optional meaning no syntax error will be thrown if the file is not included
    • A warning message will be displayed if test.php is not existing
    • The file can be included only once
    • require 'test.php'
    • Inserts the script written in test.php in the file in which it is written
    • The inclusion is mandatory
    • A error message will be thrown if the file does not exist and the script will stop executing immediately
    • require_once 'test.php'
    • Inserts the script written in test.php in the file in which it is written
    • The inclusion is mandatory
    • A error message will be thrown if the file does not exist and the script will stop executing immediately
    • The file can be included only once
    Super Globals => PHP supports variables that are accessible in the entire application
      Some of them are as follows:
    • $_GET => It contains all the query parameters as keys and their values as values
    • $_POST => The file can be included only once
    • It contains all the form parameters with HTTP request parameters as keys and their values as values
    • $_FILES => It contains the list of one or more files uploaded through <input type="file" name="test" />
    • $_SESSION => It contains the list of parameters for the current session
    • $_SERVER => It contains the information about the server
    • $_REQUEST => It contains the request parameters and their for GET or POST requests

      Sessions in PHP

    • To start a new session, use the session_start(); statement
    • The session_start() should be the first statement in any PHP script
    • The session_start() creates a new session with a 32 character session id
    • The session_id is available for the entire session
    • If the session is already created, session_id is not updated
    • To destroy the session, use the session_destroy();
    • The session_destroy() unsets the $_SESSION["id"] variable

      PDO

    1. PDO stands for PHP Data Object
    2. PDOException is a class which stores the error message with the stack trace of the error while working with the PDO class
    3. The constructor of the PDO class takes three arguments in the respective order as a follows: hostname prefixed by the SQL RDMS name
    4. username
    5. password

      Basic functions – Part 1

    • To check if a string is empty, use the empty() function
    • To check if a variable is not null, use the isset() function
    • To reset a variable to null, use the unset() function
    • To start a new HTTP session, use the session_start() function
    • To invalidate an existing HTTP session, use the session_destroy() function
    • To get the current session id, use the session_id() function

      Basic functions – Part 2

    • echo() is used to to print the string specified in the the web page
    • print_r() prints all the keys and values of the objects or variables in the web page
    • exit() function is used to abort the execution of the currently running PHP script
    • die($exit_message) is same as the exit function except it gives the string message after aborting
    • header("$key: $value") function is used to send request headers and their values to the server

      File uploading

    • Steps:
    • Store the file object sent by the client in a variable
    • $file = $_FILES["index"];
    • Store the name of the file in a variable
    • $fn = $file["name"];
    • Store the file in the server
    • move_uploaded_file($file["tmp_name"],"destination_directory" . $fn);

      Handling Database Transactions

    • If $dbh is your database connection object, • To start a database transaction use the following code: - $dbh->beginTransaction();
    • To rollback a transaction to its previous state: - $dbh->rollBack();
    • To permanently execute a transaction: - $dbh->commit();
    • To execute a "prepared statement":
    • $sth = $dbh->prepare($query);
    • $sth->execute($params);
    • To fetch the result in associative array format: - $result = $sth->fetch(PDO::FETCH_ASSOC);
    • To fetch all the rows of the result set:
    • $result = $sth->fetchAll();

      Dev ops

    • Amazon Web Services (AWS) has two credentials:
    • An AWS key which is an alphanumeric string with 20 characters in length
    • An AWS secret which is a string with 40 characters in length

Comments

Popular posts from this blog

Parallel Database design, query processing

Apache Hadoop Prerequisites and Installation

Starting with Apache Hadoop