Why PHP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Introduction to

<?PHP?>

[email protected]

Course Structure

2 Parts
1. Introduction to PHP (3 Days)

2. Web Applications with PHP (4-6 Days)

Introduction to PHP
Day-1 : PHP installations, PHP Language Basics, Basic Control Structures
Day-2 : Advanced control Structures, Function, Arrays, Include &

Require(Modularity)
Day-3 : PHP with HTML: HTTP Get & Post, Sessions, Cookies, Strings and

String Functions,

Tools
Code

Editor : Notepad++ / Microsoft Web Matrix

Application

Server: Apache (HTTPD)

Web Browser : Google


Bundled Package

Chrome / Mozilla Firefox

: WAMP Server (PHP 5.5)

Runtime: VC++ 2012 (VC110)


Download : ftp://cc.vspsite.org/test/PHP/Tools/

Why

PHP

Programming languages @ most popular websites


Websites

Popularity
visitors per
month)

Front-end
(Client-side)

Back-end
(Server-side)

Database

Google.com 1,600,000,000 JavaScript

C, C++, Go, Java, Pyt


BigTable, MariaDB
hon

Facebook.c
1,100,000,000 JavaScript
om

MariaDB, MySQL, H
Hack, PHP
(HHVM), Python, C++, BaseCassandra

YouTube.co
1,100,000,000 JavaScript
m

C, C++, Python, Java


BigTable, MariaDB
, Go

Yahoo

JavaScript

PHP

MySQL, PostgreSQL

JavaScript

Java, C++, Perl

Oracle Database

JavaScript

PHP , Hack

MySQL, MariaDB

750,000,000

Amazon.co
500,000,000
m
Wikipedia.or
475,000,000
g

The classical case


You wanted a BANANA

but what you got


was a GORILLA holding the banana
and the entire JUNGLE.

About

PHP

About PHP

PHP is a server-side scripting language designed primarily for web


development.

Created by Rasmus Lerdorf in 1995.

PHP is now maintained The PHP Development Team.

PHP originally stood for Personal Home Page, but it now stands for the
recursive acronym PHP: Hypertext Preprocessor.

PHP code is usually processed by a PHP interpreter implemented as a


module in the web server or as a Common Gateway Interface (CGI)
executable.

PHP code may also be executed with a command-line interface (CLI).

Setting Up

Development
Environment

Intallations
WAMP Server (Windows Apache

Mysql Php) -

Apache web server - OpenSSL for SSL-TLS support,


MySQL database, &
PHP programming language

VC++ (VC110)
Notepad++

Runtime

: Code Editor

Language Basics

PHP

The Hello World example

The echo statement can be used with or without parentheses: echo or echo().
<?php

echo "Hello World!";


echo ("Hello World2!);
?>

The Hello World with Variables


<?php
$string = 'Hello World!<br>';
echo $string;
print $string;
printf('%s', $string);

?>

Variables

They are "containers" (spaces in memory) that hold data. The data can
be changed, thus it is called "variable".
PHP is dynamically typed, because the type of the variable is linked to
the value of the variable. You could define a variable for a string, store a
string, and then replace the string with a number.
To do the same thing in C#/Java, you would have to cast, or change
the type of, the variable, and store it in a different "container".
All variables in PHP follow the format of a dollar sign ($) followed by an
identifier i.e. $variable_name.
These identifiers are case-sensitive, meaning that capitalization
matters, so $wiki is different from $Wiki.

Variables

PHP has eight data types which we use to construct our variables

Integers are whole numbers, without a decimal point, like 4195.

Doubles are floating-point numbers, like 3.14159 or 49.1.

Booleans have only two possible values either true or false.

NULL is a special type that only has one value: NULL.

Strings are sequences of characters, like 'PHP supports string operations.'

Arrays are named and indexed collections of other values.

Objects are instances of programmer-defined classes, which can package up both


other kinds of values and functions that are specific to the class.

Resources are special variables that hold references to resources external to PHP
(such as database connections).

Variables

Example:
<?php

$x = 5;
$y = 4;
echo $x + $y;
?>

Global Variable

In contrast to local variables, a global variable can be accessed in any


part of the program.

However, in order to be modified, a global variable must be explicitly


declared to be global in the function in which it is to be modified.

This is accomplished, conveniently enough, by placing the


keyword GLOBAL in front of the variable that should be recognized as
global.

Placing this keyword in front of an already existing variable tells PHP to


use the variable having that name.

Global Variable

Example:
<?php
$somevar = 15;
function addit()
{
GLOBAL $somevar;
$somevar++;
print "Somevar is $somevar";
}

addit();
?>

Operators

= (assignment)

*= (multiplication assignment)

+ (addition)

/= (division assignment)

(subtraction)

%= (modulus assignment)

* (multiplication)

.= (concatenation assignment)

/ (division)
% (modulus)
. (concatenation)

= (subtraction assignment)

The seven basic PHP operators:

Assignment operators :
+= (addition assignment)

Increment and Decrement


operators
++ (increment)

(decrement)

Conditional Operators
Operator

Description

Example

==

Checks if the value of two operands are equal or not, if yes then
condition becomes true.

(A == B) is not
true.

===

Checks if the value and type of two operands are equal or not, if
yes then condition becomes true.

(A === B) is not
true.

!=

Checks if the value of two operands are equal or not, if values are (A != B) is true.
not equal then condition becomes true.

>

Checks if the value of left operand is greater than the value of


right operand, if yes then condition becomes true.

(A > B) is not
true.

<

Checks if the value of left operand is less than the value of right
operand, if yes then condition becomes true.

(A < B) is true.

>=

Checks if the value of left operand is greater than or equal to the


value of right operand, if yes then condition becomes true.

(A >= B) is not
true.

<=

Checks if the value of left operand is less than or equal to the


value of right operand, if yes then condition becomes true.

(A <= B) is true.

Logical Operators
Operator

Description

Example

and

Called Logical AND operator. If both the operands are true then
condition becomes true.

(A and B) is true.

or

Called Logical OR Operator. If any of the two operands are non


zero then condition becomes true.

(A or B) is true.

&&

Called Logical AND operator. If both the operands are non zero
then condition becomes true.

(A && B) is true.

||

Called Logical OR Operator. If any of the two operands are non


zero then condition becomes true.

(A || B) is true.

Called Logical NOT Operator. Use to reverses the logical state of


its operand. If a condition is true then Logical NOT operator will
make false.

!(A && B) is false.

Basic Control Structures

PHP

IF statement
<?php
$foo = 1;
$bar = 2;
if ($foo == $bar) {
echo "$foo is equal to $bar.";
}
elseif ($foo > $bar) {
echo "$foo is greater than $bar.";
}
else {
echo "$foo is less than $bar.";
}

?>

FOR Loop
<?php
$a = 0;
$b = 0;
for( $i = 0; $i<5; $i++ )
{
$a += 10;
$b += 5;
}
echo ("At the end of the loop a = $a and b = $b" );
?>

WHILE Loop
<?php
$i = 0;
$num = 50;
while( $i < 10)
{
$num--;
$i++;
}
echo ("Loop stopped at i = $i and num = $num" );
?>

Do While Loop
<?php
$i = 0;
$num = 0;
do
{
$i++;
}
while( $i < 10 );
echo ("Loop stopped at i = $i" );
?>

For Each Loop


<?php
$array = array( 1, 2, 3, 4, 5);
foreach( $array as $value )
{
echo "Value is $value <br />";
}
?>

Break
<?php
$i = 0;
while( $i < 10)
{
$i++;
if( $i == 3 )
break;
}
echo ("Loop stopped at i = $i" );
?>

Continue
<?php
$array = array(
foreach( $array
{
if( $value ==
continue;
echo "Value
}
?>

1, 2, 3, 4, 5);
as $value )
3 )
is $value <br />";

Thats all for the day

Thanks

You might also like