Friday, 14 March 2014

Php Basic

PHP 5 Tutorial

PHP is a server scripting language, and is a powerful tool for making dynamic and interactive Web pages quickly. PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.


Introduction


What You Should Already Know

Before you continue you should have a basic understanding of the following:

  • HTML
  • CSS
  • Javascript

What is PHP?

  • PHP is an acronym for "PHP Hypertext Preprocessor"
  • PHP is a widely-used, open source scripting language
  • PHP scripts are executed on the server
  • PHP costs nothing, it is free to download and use

What is a PHP File?

  • PHP files can contain text, HTML, CSS, JavaScript, and PHP code
  • PHP code are executed on the server, and the result is returned to the browser as plain HTML
  • PHP files have extension ".php"

What Can PHP Do?

  • PHP can generate dynamic page content
  • PHP can create, open, read, write, delete, and close files on the server
  • PHP can collect form data
  • PHP can send and receive cookies
  • PHP can add, delete, modify data in your database
  • PHP can restrict users to access some pages on your website
  • PHP can encrypt data

Why PHP?

  • PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
  • PHP is compatible with almost all servers used today (Apache, IIS, etc.)
  • PHP supports a wide range of databases
  • PHP is free. Download it from the official PHP resource : www.php.net
  • PHP is easy to learn and runs efficiently on the server side

PHP 5 Syntax

The PHP script is executed on the server, and the plain HTML result is sent back to the browser.


Basic PHP Syntax

A PHP script can be placed anywhere in the document.

A PHP script starts with : <?php and ends with ?>

The default file extension for PHP files is ".php". A PHP file normally contains HTML tags, and some PHP scripting code. Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function "echo" to output the text "Hello World!" on a web page:

<? echo "Hello World!"; ?>

Output : Hello World


Comments in PHP

A comment in PHP code is a line that is not read/executed as part of the program. Its only purpose is to be read by someone who is editing the code! Comments are useful for:

  • To let others understand what you are doing - Comments let other programmers understand what you were doing in each step (if you work in a group)
  • To remind yourself what you did - Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code

PHP supports three ways of commenting:

<?php // This is a single line comment
# This is also a single line comment
/* This is a multiple lines comment block that spans over more than one line */


PHP Case Sensitivity

In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.) are NOT case-sensitive. In the example below, all three echo statements below are legal (and equal):

<?php
ECHO "Hello World!"; // it display Hello World
echo "Hello World!"; // it display Hello World
EcHo "Hello World!"; // it display Hello World
?>

However; in PHP, all variables are case-sensitive. In the example below, only the first statement will display the value of the $color variable (this is because $color, $COLOR, and $coLOR are treated as three different variables):

<?php
$color="red";
echo "My car is " . $color . " // it display My car is red";
echo "My house is " . $COLOR . " // it display My house is ";
echo "My boat is " . $coLOR . " // it display My boat is ";
?>


PHP 5 Variables

Variables are "containers" for storing information:

<?php
$x=5; $y=6; $z=$x+$y; echo $z; // it display 11
?>


Much Like Algebra

x=5
y=6
z=x+y In algebra we use letters (like x) to hold values (like 5).
From the expression z=x+y above, we can calculate the value of z to be 11.
In PHP these letters are called variables.


PHP Variables

As with algebra, PHP variables can be used to hold values (x=5) or expressions (z=x+y).
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
Rules for PHP variables:

  • variable starts with the $ sign, followed by the name of the variable
  • variable name must start with a letter or the underscore character
  • variable name cannot start with a number
  • variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case sensitive ($y and $Y are two different variables)

PHP 5 echo and print Statements

In PHP there is two basic ways to get output: echo and print. In this tutorial we use echo (and print) in almost every example. So, this chapter contains a little more info about those two output statements.


PHP echo and print Statements

There are some differences between echo and print:

  • echo - can output one or more strings
  • print - can only output one string, and returns always 1

PHP Data Types

String, Integer, Floating point numbers, Boolean, Array, Object, NULL.


PHP Strings

A string is a sequence of characters, like "Hello world!". A string can be any text inside quotes. You can use single or double quotes:

<?php
$x = "Hello world!";
echo $x; // it prints Hello world!
echo "
"; $x = 'Hello world!';
echo $x;// it prints Hello world!
?>


PHP Integers

An integer is a number without decimals. Rules for integers:

  • An integer must have at least one digit (0-9)
  • An integer cannot contain comma or blanks
  • An integer must not have a decimal point
  • An integer can be either positive or negative
  • Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)
In the following example we will test different numbers. The PHP var_dump() function returns the data type and value of variables:

<?php
$x = 5985;
var_dump($x); // it display int(5985)
$x = -345; // negative number
var_dump($x); // it display int(-345)
$x = 0x8C; // hexadecimal number
var_dump($x); // it display int(140)
$x = 047; // octal number
var_dump($x); // it display int(39)
?>


PHP Floating Point Numbers

A floating point number is a number with a decimal point or a number in exponential form. In the following example we will test different numbers. The PHP var_dump() function returns the data type and value of variables:

<?php
$x = 10.365;
var_dump($x); // it display float(10.365)
$x = 2.4e3; var_dump($x); // it display float(2400)
$x = 8E-5; var_dump($x); // it display float(8.0E-5)
?>


PHP Booleans

Booleans can be either TRUE or FALSE.

$x=true;
$y=false;
Booleans are often used in conditional testing. You will learn more about conditional testing in a later chapter of this tutorial.


PHP Arrays

An array stores multiple values in one single variable. In the following example we create an array, and then use the PHP var_dump() function to return the data type and value of the array: <?php
$cars=array("Volvo","BMW","Toyota");
var_dump($cars); // it display array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(6) "Toyota" }
?>


PHP Objects

An object is a data type which stores data and information on how to process that data. In PHP, an object must be explicitly declared. First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods. We then define the data type in the object class, and then we use the data type in instances of that class:
<?php
class Car
{
var $color;
function Car($color="green")
{
$this->color = $color;
}
function what_color()
{
return $this->color;
}
}
function print_vars($obj)
{
foreach (get_object_vars($obj) as $prop => $val)
{
echo "\t$prop = $val\n";
}
}
// instantiate one object
$herbie = new Car("white");
// show herbie properties
echo "\herbie: Properties\n";
print_vars($herbie); // it display \herbie: Properties color = white
?>


PHP NULL Value

The special NULL value represents that a variable has no value. NULL is the only possible value of data type NULL. The NULL value identifies whether a variable is empty or not. Also useful to differentiate between the empty string and null values of databases. Variables can be emptied by setting the value to NULL:
<?php
$x="Hello world!";
$x=null;
var_dump($x); // it display NULL
?>


PHP String Functions

A string is a sequence of characters, like "Hello world!".

In this chapter we will look at some commonly used functions to manipulate strings.

The PHP strlen() function

The strlen() function returns the length of a string, in characters. The example below returns the length of the string "Hello world!":
<?php
echo strlen("Hello world!"); ?>

The output of the code above will be: 12


The PHP strpos() function

The strpos() function is used to search for a specified character or text within a string. If a match is found, it will return the character position of the first match. If no match is found, it will return FALSE. The example below searches for the text "world" in the string "Hello world!":
<?php
echo strpos("Hello world!","world");
?>

The output of the code above will be: 6.