php introducation

43
Introduction to PHP 1 Introduction to PHP Pankil Agrawal Developer , PersistancePlus

Upload: pankil-agrawal

Post on 01-Nov-2014

922 views

Category:

Education


5 download

DESCRIPTION

IN THIS PPT YOU WILL FIND PHP WITH EXAMPLE

TRANSCRIPT

PHP - Part IIs a server side scripting language.
Capable of generating the HTML pages
PHP is an open source software and free to download and use
*
Why PHP?
..There are no. of server side scripting available like ASP, JSP…..
PHP involves
platform independence.
Runs on different operating systems such as Linux, Unix and Windows
Low maintenance and development cost
Compatible with servers IIS and Apache
PHP code run faster
Ability to embed into HTML code
Supports different databases such as SQL, Generic ODBC, MySQL, Oracle, Sybase etc..
Is an open source. ( www.php.net )
*
WAMP Server
WAMPs are packages of independently-created programs installed on computers that use a Microsoft Windows operating system , So it is set of open source applications (WAMP stack)
WAMP" is an acronym formed from the initials of the operating system (Windows) and the package's principal components: Apache , MySQL and PHP (or Perl or Python )
The equivalent installation
Apache is a web server
MySQL is a database manager
Introduction to PHP
Syntax
A PHP scripting block always starts with <?php and ends with ?>.
Eg: <?php?>
A PHP scripting block can be placed anywhere in the document.
A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code.
Introduction to PHP
Each code line in PHP must end with a semicolon
Caution : The file must have the .php extension. If the file has a .html extension, the PHP code will not be executed.
PHP Comments :
/* and */ to make a large comment block.
Introduction to PHP
*
PHP Variables
The variables in PHP are declared by appending the $ sign to the variable name.
For e.g
$company = “NCST”;
$sum = 10.0;
variable’s data type is changed by the value that is assigned to the variable.
PHP is a Loosely Typed Language
In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.
In PHP the variable is declared automatically when you use it.
Type casting allows to change the data type explicitly.
*
Variable Naming Rules
A variable name must start with a letter or an underscore "_"
Can only contain alpha-numeric characters and underscores
Should not contain spaces
Comparison (==,!=,<,>,<=,>=)
Logical ( && , || , !)
A constant is an identifier (name) for a simple value
As the name suggests, that value cannot change during the execution of the script
A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores
Constants are defined in PHP by using the define() function.
For e.g.
define(“NCST”, “National Centre for Software Technology”)
*
Four Scalar Type
?>
Booleans
A boolean expresses a truth value. It can be either TRUE or FALSE.
Non-zero (whether negative or positive) number! Number can consider as TRUE
<?php
?>
Integers
An integer is a number of the set Z = {..., -2, -1, 0, 1, 2, ...}.
Integers can be specified in decimal (10-based), hexadecimal (16-based) or octal (8-based) notation, optionally preceded by a sign (- or +).
<?php
$a = 0123; # octal number (equivalent to 83 decimal)
$a = 0x1A; # hexadecimal number (equivalent to 26 decimal)
?>
String variables are used for values that contain character strings.
(.) Dot operator use to concatenation
Eg :<?php
Functions :
Strpos()-> echo strpos(“hello world”,”world ”); (6)
Introduction to PHP
Arrays
An array can store one or more values in a single variable name
There are different ways to declare array..
array( [key =>] value
// key may be an integer or string // value may be any value
Example
<?php
echo $arr["foo"]; // bar
echo $arr[12];    // 1
$names = array("Peter","Quagmire","Joe");
$names[0] = “hello";
$names[1] = “hi";
Example
<?php
$names[0] = "Peter";
$names[1] = “Jen";
$names[2] = "Joe";
?>
Introduction to PHP
Objects
To initialize an object, you use the new statement to instantiate the object to a variable.
<?php
Resource
A resource is a special variable, holding a reference to an external resource
Resources are created and used by special functions
Example
dba_open()
dba_open -- Open database
Introduction to PHP
NULL
The special NULL value represents that a variable has no value
A variable is considered to be NULL if
it has been assigned the constant NULL.
it has not been set to any value yet.
it has been unset() .
}
else
Introduction to PHP
elseif ($d=="Sun")
else
?>
$i++;
}while ($i<5);
{
Syntax:
</body>
</html>
On each loop, the value of the current element is assigned to $value and the internal array pointer is advanced by one
Introduction to PHP
break
break ends execution of the current for, foreach while, do..while or switch structure.
break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of
<?php
        break 1;  /* Exit only the switch. */
    case 10:
        break 2;  /* Exit the switch and the while. */
    default:
        break;
continue
continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the beginning of the next iteration.
Introduction to PHP
Switch
The switch statement is similar to a series of IF statements on the same expression
<html>
<body>
<?php
}
?>
All functions start with the word "function()"
Name the function - It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number)
Add a "{"  - The function code starts after the opening curly brace
Insert the function code
Add a "}"  - The function is finished by a closing curly brace
Example:
<?php