php

SDSLabs

What does it stand for?

PHP: HYPERTEXT PREPROCESSOR

What is it?

PHP IS AN HTML-EMBEDDED
SCRIPTING LANGUAGE.

INTERPRETED RATHER THAN COMPILED.

Why use it?

WRITE DYNAMICALLY GENERATED PAGES QUICKLY.

GENERATED AT THE TIME OF ACCESS BY A USER OR
CHANGE AS A RESULT OF INTERACTION WITH THE USER.

How does it work?

PHP IS A SERVER-SIDE LANGUAGE.

PROCESSING TAKES PLACE ON THE SERVER & HTML IS RETURNED TO THE BROWSER.

Installation

Refer to Resource Wiki

Write

All PHP code must be contained within <?php and ?>

Every statement has to be terminated with a semicolon.

<?php
  // code goes here
?>

Save the file with php extension.

How to see a php file in action?

Demonstration time.

What happens if the URL points to a directory?

If the URL points to a directory rather than a file, the server will serve a default page index.php within that directory.

For eg: By default http://localhost/bourbon/ points to http://localhost/bourbon/index.php

variables

Store values.

Can be used throughout your code.

$variableName = value;

Numbers

<?php
      $a = 10; //Assigns 10 to $a.
      $b = 5; //Assigns 5 to $b.
      $c = $a + $b;
      echo $c; //Prints value of $c.
  ?>

Strings

<?php
      $a = "Hello World"; //String Assignment
      echo $a; //Prints value of $a.
  ?>

Naming Conventions

  • Must start with a letter or underscore "_"
  • May only be comprised of alpha-numeric characters and underscores ie. a-z, A-Z, 0-9, or _
  • More than one word should be separated with underscores, e.g. $my_variable
  • More than one word can also be distinguished by using camelCase, $myVariable

echo

outputs text to screen

Sample Code

<?php
  $a = 5;
  echo $a;
  $b = "hello world";
  echo $b;
?>
<?php
  echo 5;
  echo "hello world";
?>
<?php
  echo 5+15;
  echo 25*5;
?>

Unique to PHP

  • String Operators
  • Foreach Loop
  • Associative Arrays

String Operators

Concatenation means connecting/joining things

Concatenation Operator
.

<?php
    $a = 'hello';
    $b = 'world';
    $c = $a . $b;
    
    // prints helloworld to screen
    echo $c;
    
    // prints 12 to screen
    echo 1 . 2;
?>

Concatenating Assignment Operator
.=

<?php
    $a = 'hello';
    $a .= ' new recruits';
    
    // prints hello new recruits to screen
    echo $a;
?>

Loops

for, while, do-while & foreach

for

<?php
  $factorial = 1;
  
  for($i = 1; $i <= 10; $i++) {
    $factorial = $factorial * $i;
  }

  echo $factorial;
?>

while

<?php
  $factorial = 1;
  $i = 1;
  
  while($i <= 10) {
    $factorial = $factorial * $i;
    $i++;  // increment
  }

  // prints the value of 10!
  echo $factorial;
?>

do-while

<?php
  $factorial = 1;
  $i = 1;
  
  do {
    $factorial = $factorial * $i;
    $i++;  // increment
  } while($i <= 10);

  // prints the value of 10!
  echo $factorial;
?>

foreach

<?php
  //$applications = array("Bourbon", "Muzi", "CodeVillage", "Erdős");
  $applications = ["Bourbon", "Muzi", "CodeVillage", "Erdős"];

  // Muzi is a music player hosted on the intranet.
  echo $applications[1] . " is a music player hosted on the intranet.";

  echo "Applications developed by SDSLabs: ";

  foreach($applications as $application) {
    echo "{$application} ";
  }
?>
// Output
  Applications developed by SDSLabs: Bourbon Muzi CodeVillage Erdős 

Associative Arrays

Associates values to keys

$ages["Kandoi"]

Example

<?php
  $ages = array(
    "Kandoi" => 19,
    "Ravi" => 20
  );

  // Ravi is 20 years old
  echo "Ravi is {$ages['Ravi']} years old.";

  foreach($ages as $name => $age) {
    echo "{$name} is {$age} years old.";
  }    
?>

Functions

Blocks of code which are used multiple times

eg: pow function computes a b

Syntax

<?php
  function FUNCTION_NAME(arguments) {
    // code
  }
?>

Example

<?php
  function sayHello($name) {
    echo "Hello {$name}";
  }

  sayHello("new recruits");
?>
<?php
  function sayHello($name) {
    return "Hello {$name}";
  }

  echo sayHello("new recruits");
?>

Forms

  • Important application of basic PHP.
  • Remember the HTML assignment ? It had a form !
<form>
  First: <input type="text" name="Firstname" maxlength="3">
  Last: <input type="text" name="Lastname" maxlength="5">
  <input type="submit" value="SUBMIT">
</form>

First:
Last:

form has 2 important attributes

Action: destination of form data

Method: method used to send data

HTTP request methods: GET & POST

GET

  • In  GET method , form data is sent explicitly and is visible in the URL .
  • Limit on the amount of data sent in single GET request.

POST

This is more secure than a GET request
  • Not visible in the URL
  • Used to send sensitive data
  • File uploading
  • No size limit


Login data of a website is sent by POST method.
Imagine if Gmail login was based upon GET !

FORM revisited

So appending action and method attributes 
to our form ,


Keep an eye on 'name' attribute.

The client side work is done.
Now , the server side script handling form data.

FORM.PHP

Data sent to PHP script through GET/POST is
accessed through  associative arrays,
$_GET and $_POST respectively . 

Name attribute used as index of array element

$_REQUEST used for both GET and POST


FORM.php 



Classes

Example

<?php
  class Member {
    public $name = "Name";
    public $age = 0;

    function __construct($name, $age) {
       $this->name = $name;
       $this->age = $age;
    }

    public function description() {
      echo "{$this->name} is a member of SDSLabs.";
      echo "He is {$this->age} years old.";
    }
  }

  $parag = new Member("Parag Nandi", 19);

  $parag->description();
?>

Visibility of functions can be public, protected and private.

MySQL

  • An open source relational database system
  • Stores data in form of tables
  • Use the data stored for functionality of your webpage

Using MySQL in PHP file

  • Connect to the database
  • Select any data from any table or insert into any table
  • Close the connection

Connecting to database

mysqli class

Parameters:

  • Server
  • Username
  • Password
  • Database
<?php
    // connecting to database
    $link = new mysqli('server', 'user', 'password', 'database');
    
    if($link->connect_errno) {
      die ("Connection attempt unsuccessful!");
    } else {
      echo "Connection made!";
    }
?>

Retrieving data from database

query() method

Parameter:

  • Query
<?php
    $link = new mysqli('server', 'user', 'password', 'database');

    if($link->connect_errno) {
      die ("Connection attempt unsuccessful!");
    } else {
      echo "Connection made!";
    }
    
    $result = $link->query("SELECT * FROM users");

    // do some processing on $result
?>

Closing database connection

close() method
<?php
    $link = new mysqli('server', 'user', 'password', 'database');

    if($link->connect_errno) {
      die ("Connection attempt unsuccessful!");
    } else {
      echo "Connection made!";
    }
    
    $result = $link->query("SELECT * FROM users");

    $link->close();
?>

links

php documentation

Tizag - PHP

mysqli class