Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

05_PHP_Lecture

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

PHP

iFour Consultancy
HYPERTEXT PREPROCESSOR

web Engineering ||

winter 2017
wahidullah Mudaser  Lecture 05
assignment.cs2017@gmail.com
 Introduction To PHP
INDEX

 Three-tiered website
 Introduction to PHP
 Server side scripting langauge

http://www.ifourtechnolab.com/ ASP.NET Software Development Companies India


Three-tiered Web Site
Client example request Server
User-agent: Firefox GET / HTTP/1.1 Apache HTTP Server
Host: www.tamk.fi
User-Agent: Mozilla/5.0 (Mac..)
...

response PHP

Database
MySQL
Server Side Scripting Language

 A “script” is a collection of program or sequence of instructions that is interpreted or carried out


by another program rather than by the computer processor.
 Client-side
 Server-side

 In server-side scripting, (such as PHP, ASP) the script is processed by the server Like: Apache,
ColdFusion, ISAPI and Microsoft's IIS on Windows.

 Client-side scripting such as JavaScript runs on the web browser.


Advantages of Server side Scripting Language
 Dynamic content.
 Computational capability.
 Database and file system access.
 Network access (from the server only).
 Built-in libraries and functions.
 Known platform for execution (as opposed to client-side,
 where the platform is uncontrolled.)
 Security improvements
What Is PHP?

 PHP is a server scripting language, and a powerful tool for making dynamic and interactive
Web pages.
 PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's
ASP.
 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 is free to download and use
Cont…

 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
Brief History of PHP

PHP (PHP: Hypertext Preprocessor) was created by Rasmus Lerdorf in 1994. It was initially developed for HTTP usage logging and server-
side form generation in Unix.

PHP 2 (1995) transformed the language into a Server-side embedded scripting language. Added database support, file uploads, variables,
arrays, recursive functions, conditionals, iteration, regular expressions, etc.

PHP 3 (1998) added support for ODBC data sources, multiple platform support, email protocols (SNMP,IMAP), and new parser written by
Zeev Suraski and Andi Gutmans .

PHP 4 (2000) became an independent component of the web server for added efficiency. The parser was renamed the Zend Engine. Many
security features were added.

PHP 5 (2004) adds Zend Engine II with object oriented programming, robust XML support using the libxml2 library, SOAP extension for
interoperability with Web Services, SQLite has been bundled with PHP
What is PHP File

PHP files have a file extension of ".php", ".php3", or ".phtml“

 PHP files can contain text, HTML tags and scripts

 PHP files are returned to the browser as plain HTML


Basic PHP Syntax

 A PHP script can be placed anywhere in the document.

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

 <!DOCTYPE html>
<html>
<body>
<h1>PHP TUTORIAL</h1>
<?php
echo “YCCE NAGPUR”;
?>
</body>
</html>
PHP Variables

 A variable starts with the $ sign, followed by the name of


the variable
 A variable name must start with a letter or the
underscore character
 A variable name cannot start with a number
 A variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
 Variable names are case-sensitive ($age and $AGE are
two different variables)
Basic PHP Syntax
<!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
</body>
</html>
PHP is Loosely Typed language

 In the example above, notice that we did not have to tell


PHP which data type the variable is.
 PHP automatically converts the variable to the correct
data type, depending on its value.
 In other languages such as C, C++, and Java, the
programmer must declare the name and type of the
variable before using it.
PHP Variables Scope

 In PHP, variables can be declared anywhere in the script.


 The scope of a variable is the part of the script where the
variable can be referenced/used.
 PHP has three different variable scopes:
 local
 global
 static
PHP global keyword

 The global keyword is used to access a global variable from within a


function.
 To do this, use the global keyword before the variables
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y;
?>
PHP Static Keyword

 <?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}

myTest();
myTest();
myTest();
?>
PHP echo Statement

<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple
parameters.";
?>
PHP print statement

<?php
$txt1 = "Learn PHP";
$txt2 = “ycce.com"; Echo V.S Print
$x = 5;  The differences are small: echo has no return value while
$y = 4; print has a return value of 1 so it can be used in expressions.

 echo can take multiple parameters (although such usage is


print "<h2>$txt1</h2>"; rare) while print can take one argument.
print "Study PHP at $txt2<br>";
 echo is marginally faster than print.
print $x + $y;
?>
PHP Data types

 PHP supports the following data types:


 String
 Integer
 Float (floating point numbers - also called double)
 Boolean
 Array
 Object
 NULL
 Resource
Data types cont..

PHP String:
 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:
Example
<?php
$x = 5985;
var_dump($x);
?>
Cont…

PHP Array:
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
PHP Object
 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.
<?php
class Car {
function Car() {
$this->model = "VW";
}
}
// create an object
$herbie = new Car();
// show object properties
echo $herbie->model;
?>
PHP NULL value
 Null is a special data type which can have only one value:
NULL.
 A variable of data type NULL is a variable that has no
value assigned to it.
 Note: If a variable is created without a value, it is
automatically assigned a value of NULL.

Example
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
PHP String Functions
Get The Length of a String:

 The PHP strlen() function returns the length of a string


(number of characters).
 The example below returns the length of the string "Hello
world!":

Example
<?php
echo strlen("Hello world!"); // outputs 12
?>
Cont…

Count The Number of Words in a String:


Example
<?php
echo str_word_count("Hello world!"); // outputs 2
?>

Reverse a String:
Example
<?php
echo strrev("Hello world!");
?>
Cont…
Search For a Specific Text Within a String:
 If a match is found, the function returns the character
position of the first match. If no match is found, it will
return FALSE.
Example
<?php
echo strpos("Hello world!", "world"); // outputs 6
?>
Cont…

 Replace Text Within a String:


Example
<?php
echo str_replace("world", "Dolly", "Hello world!"); //
outputs Hello Dolly!
?>
PHP Constants

 A constant is an identifier (name) for a simple value. The value cannot


be changed during the script.
 A valid constant name starts with a letter or underscore (no $ sign
before the constant name).
 Note: Unlike variables, constants are automatically global across the
entire script.
PHP Constants
Syntax:
define(name, value, case-insensitive)
Parameters:
 name: Specifies the name of the constant
 value: Specifies the value of the constant
 case-insensitive: Specifies whether the constant name should be case-insensitive.
Default is false
Example - Constants

<?php
define(“NAME", "Welcome Ahmadullah!");
echo NAME;
?>
Constants are Global:
<?php
define(“NAME", "Welcome Ahmadullah!");
function myTest() {
echo NAME;
}
myTest();
?>
PHP Operators
Operators are used to perform operations on variables and
values.

PHP divides the operators in the following groups:

 Arithmetic operators
 Assignment operators
 Comparison operators
 Increment/Decrement operators
 Logical operators
 String operators
 Array operators
PHP Operators

PHP Arithmetic Operators:


PHP Operators
PHP Assignment Operators:
PHP Operators

PHP Comparison Operators:


PHP Operators

PHP Increment / Decrement Operators:


PHP Operators
PHP Logical Operators:
PHP Operators

PHP String Operators:


PHP Operators

PHP Array Operators:


PHP Conditional Statements
 In PHP we have the following conditional statements:
 if statement - executes some code only if a specified
condition is true
 if...else statement - executes some code if a condition is
true and another code if the condition is false
 if...elseif....else statement - specifies a new condition to
test, if the first condition is false
 switch statement - selects one of many blocks of code to
be executed
PHP – The IF Statement

 The if statement is used to execute some code only if a


specified condition is true.
Syntax
if (condition)
{
code to be executed if condition is true;
}
PHP - The if...else Statement
 Use the if....else statement to execute some code if a
condition is true and another code if the condition is
false.
Syntax:
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
PHP - The if...elseif....else Statement
 Use the if....elseif...else statement to specify a new
condition to test, if the first condition is false.
Syntax:
if (condition) {
code to be executed if condition is true;
} elseif (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
PHP - The switch Statement
 Use the switch statement to select one of many blocks of
code to be executed.
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;
}
PHP 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
While Loops

 The while loop executes a block of code as long as the


specified condition is true.
Syntax:
while (condition is true)
{
code to be executed;
}
Do…while loop
 The do...while loop will always execute the block of code
once, it will then check the condition, and repeat the loop
while the specified condition is true.
Syntax:
do {
code to be executed;
}
while (condition is true);
For Loop
 The for loop is used when you know in advance how
many times the script should run.
Syntax:
for (init counter; test counter; increment counter)
{
code to be executed;
}
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;
}
Example – foreach loop
 For every loop iteration, the value of the current array
element is assigned to $value and the array pointer is
moved by one, until it reaches the last array element.
Example:
<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {


echo "$value <br>";
}
?>
PHP Functions

PHP User Defined Functions


 Besides the built-in PHP functions, we can create our own
functions.
 A function is a block of statements that can be used
repeatedly in a program.
 A function will not execute immediately when a page
loads.
 A function will be executed by a call to the function.
Questions?

http://www.ifourtechnolab.com/ ASP.NET Software Development Companies India

You might also like