PHP Tutorial

Basic PHP Syntax

A PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>
The default file extension for PHP files is ".php".

PHP Data Types

PHP supports the following data types:
  • String
  • Integer
  • Float (floating point numbers - also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

Creating (Declaring) PHP Variables

In PHP, a variable starts with the $ sign, followed by the name of the variable:
Example
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>

echo and print Statements

echo and print are more or less the same. They are both used to output data to the screen.


O/P



Conditional Statements

In PHP we have the following conditional statements:

  • if statement - executes some code if one condition is true
  • if...else statement - executes some code if a condition is true and another code if that condition is false
  • if...elseif....else statement - executes different codes for more than two conditions
  • switch statement - selects one of many blocks of code to be executed
The if Statement

Syntax
if (condition) {
    code to be executed if condition is true;
}


O/P


 The if...else Statement

Syntax
if (condition) {
    code to be executed if condition is true;
} else {
    code to be executed if condition is false;
}



O/P



The if...elseif....else Statement

Syntax
if (condition) {
    code to be executed if this condition is true;
} elseif (condition) {
    code to be executed if this condition is true;
} else {
    code to be executed if all conditions are false;
}


O/P



The switch Statement

Syntax
switch (n) {
    case label1:
        code to be executed if n=label1;
        break;
    case label2:
        code to be executed if n=label2;
        break;
    case label3:
        code to be executed if n=label3;
        break;
    ...
    default:
        code to be executed if n is different from all labels;
}




Loops

In PHP, we have the following looping statements:
  • while - loops through a block of code as long as the specified condition is true
  • do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
  • for - loops through a block of code a specified number of times
  • foreach - loops through a block of code for each element in an array

The while Loop

Syntax
while (condition is true) {
    code to be executed;
}




O/P


The PHP do...while Loop

Syntax
do {
    code to be executed;
} while (condition is true);



O/P



The for Loop

Syntax
for (init counter; test counter; increment counter) {
    code to be executed;
}

Parameters:

  • init counter: Initialize the loop counter value
  • test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
  • increment counter: Increases the loop counter value



O/P



The PHP foreach Loop

The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.
Syntax
foreach ($array as $value) {
    code to be executed;
}



O/P



User Defined Functions

A user defined function declaration starts with the word "function":
Syntax
function functionName() {
    code to be executed;
}


O/P



Array

In PHP, the array() function is used to create an array:

array();



O/P



File Handling

File handling is an important part of any web application. You often need to open and process a file for different tasks.

PHP has several functions for creating, reading, uploading, and editing files.

readfile() Function

The readfile() function reads a file and writes it to the output buffer.

<?php
echo readfile("webdictionary.txt");
?>

fopen()

A better method to open files is with the fopen() function. This function gives you more options than the readfile() function.

$myfile = fopen("webdictionary.txt", "r") ;

read()

The fread() function reads from an open file.

The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.

fread($myfile,filesize("webdictionary.txt"));

fclose()

The fclose() function is used to close an open file.

The fclose() requires the name of the file (or a variable that holds the filename) we want to close:
<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>

fgets()

The fgets() function is used to read a single line from a file.

feof()

The feof() function checks if the "end-of-file" (EOF) has been reached.

fgetc()

The fgetc() function is used to read a single character from a file.

Note.txt






O/P



Defining a Class

The general form for defining a new class in PHP is as follows

  • Declare a class using the class keyword, followed by the name of the class and a set of curly braces ({})
  • A set of braces enclosing any number of variable declarations and function definitions.
  • Variable declarations start with the special form var, which is followed by a conventional $ variable name; they may also have an initial assignment to a constant value.
  • Function definitions look much like standalone PHP functions but are local to the class and will be used to set and access object data.
Syntax

<? php

   class phpClass {
      var $var1;
      var $var2 = "constant string";
      function myfunc ($arg1, $arg2) {
         [..]
      }
      [..]
   }

?>

Creating Objects 

Once you defined your class, then you can create as many objects as you like of that class type by using class name and new operator.

$objectName = new nameOfClass();

Calling Member Functions

After creating your objects, you will be able to call member functions related to that object. One member function will be able to process member variable of related object only.

Syntax

$object->functionName();


O/P



Constructor

All objects can have a special built-in method called a ‘constructor’. Constructors allow you to initialise your object’s properties(variables) when you instantiate (create) an object.

PHP provides a special function called __construct() to define a constructor. You can pass as many as arguments you like into the constructor function.

Syntax

function __construct($param1, $param2, etc.)

$this -> propertyName = $value;

 }

Example

class Book{

$title;
$numPages;

function __construct(){
$this->$numPages = 200;
}

}

The keyword this is a special keyword used when creating classes. The purpose of it is to refer to the current class.

Destructor

PHP also allows you to define class destructors - a function to be called when an object is deleted. 

Like a constructor function you can define a destructor function using function __destruct().

Syntax

public function __destruct()

 {
    print "{$this->Name} is no more...\n";
}

O/P




PHP Tutorial PHP Tutorial Reviewed by Unknown on 00:10:00 Rating: 5

No comments: