SlideShare une entreprise Scribd logo
1  sur  42
PHP Basics
Presented By
Sajeer.K.P
Server-Side Scripting
 What is a script?
 Collection of program or sequence of instructions
 Processed/interpreted by another program
 Rather than by a processor
 Client-side
 Server-side
 In server-side scripting, PHP ASP.net - 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.
 Important fot dynamic HTML
Introduction to PHP
PHP stands for: Hypertext PreProcessor
Developed by Rasmus Lerdorf in 1994(Personal Home Page)
– Originally a set of Perl scripts known as the “Personal Home
Page” tools
• Source code released in 1995
• PHP 3 in 1997-98 by Andi Gutmans and Zeev Suraski
• Latest version 5.5.1
• It is a powerful server-side scripting language for creating
dynamic and interactive websites.
• It is an open source software, which is widely used and free to
download and use (php.net).
• It is an efficient alternative to competitors such as Microsoft's
ASP.
Introduction to PHP
• PHP is perfectly suited for Web development and can be
embedded directly into the HTML code.
• The PHP syntax is very similar to JavaScript, Perl and C.
• PHP is often used together with Apache (web server) on
various operating systems. It also supports ISAPI and
can be used with Microsoft's IIS on Windows.
• PHP supports many databases (MySQL, Informix,
Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
Introduction to PHP
• What is a PHPFile?
• PHP files have a file extension of .php, .phtml, .php4
.php3, .php5, .phps
• PHP files can contain text, HTML tags and scripts
• PHP files are returned to the browser as plain HTML 
Introduction to PHP
What you need to develop PHPApplication:
• Install Apache (or IIS) on your own server,
install PHP, and MySQL
• OR
• Install Wampserver2 (a bundle of PHP, Apache,
and MySql server) on your own server/machine
PHP Installation Downloads
Free Download
 PHP: http://www.php.net/downloads.php
 MySQL Database: http://www.mysql.com/downloads/index.html
 Apache Server: http://httpd.apache.org/download.cgi
• How to install and configure apache
• Here is a link to a good tutorial from PHP.net on how to install PHP5:
http://www.php.net/manual/en/install.php
How PHP is Processed
• When a PHP document is requested of a
server, the server will send the document first
to a PHP processor
• Two modes of operation
– Copy mode in which plain HTML is copied to the
output
– Interpret mode in which PHP code is interpreted
and the output from that code sent to output
– The client never sees PHP code, only the output
produced by the code
Basic PHP Syntax
• starts with <?php and ends with ?>
<?php ……………. ?>
– Other options are:
1. <? ……………… ?> or<?= ?>(shortened forms)
2. <script language=”php”> ... </script>
• There are three basic statements to output text with PHP:
echo, print, and printf. Example:
echo 'This is a <b>test</b>!';
• Comments:
– #
– //
– /* . . . * /
Basic PHP Syntax
• PHP statements are terminated with semicolons ;
• Curly braces, { } are used to create compound
statements
• PHP has typical scripting language characteristics
– Dynamic typing, un-typed variables
– Associative arrays
– Pattern matching
– Extensive libraries
• Primitives, Operations, Expressions
– Four scalar types: boolean, integer, double, string
– Two compound types: array, object
– Two special types: resource and NULL
Basic PHP Syntax
Example 1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> <title>Simple PHP Example</title>
<body>
<?php
echo "Hello Class of 2011. This is my first PHP Script";
echo "<br />";
print "<b><i>What have you learnt and how many friends have you
made?</i></b>";
echo "<br /><a href='PHP I-BSIC.ppt'>PHP BASIC</a>";
?>
</body>
</html>
PHP Language Basics
• Constants, Data Types and
Variables
• Constants define a string or numeric value
• Constants do not begin with a dollar sign
• Examples:
• define(“COMPANY”, “Acme Enterprises”);
• define(“YELLOW”, “#FFFF00”);
• define(“YELLOW”, “#FFFF00”);
• define(“PI”, 3.14);
• define(“NL”, “<br>n”);
Using a constant
PHP Language Basics
• Constants, Data Types and
Variables
Data types
• Integers, doubles and strings
– isValid = true; // Boolean
– 25 // Integer
– 3.14 // Double
– ‘Four’ // String
– “Total value” // Another string
PHP Language Basics
• Constants, Data Types and
Variables
Data types
• Strings and type conversion
– $street = 123;
– $street = $street . “ Main Street”;
– $city = ‘Naperville’;
$state = ‘IL’;
– $address = $street;
– $address = $address . NL . “$city, $state”;
– $number = $address + 1; // $number equals
124
PHP Language Basics
• Constants, Data Types and
Variables
Data types
• Arrays
– Perl-like syntax
» $arr = array("foo" => "bar", 12 => true);
– same as
» $arr[“foo”] = “bar”;
» $arr[12] = true;
PHP Language Basics
• Constants, Data Types and
Variables
• Arrays (cont.)
– <?php
$arr = array("somearray" => array(6 => 5, 13 => 9,
"a" => 42));
echo $arr["somearray"][6]; // 5
echo $arr["somearray"][13]; // 9
echo $arr["somearray"]["a"]; // 42
?>
PHP Language Basics
• Constants, Data Types and
Variables
Operators
– Contains all of the operators like in C and Perl (even
the ternary)
Statements
– if, if/elseif
– Switch/case
– for, while, and do/while loops
– Include and require statements for code reuse
PHP Variables
• Variables are used for storing values, such as numbers, strings or function
results, so that they can be used many times in a script.
• All variables in PHP start with a $ sign symbol.
• Variables are assigned using the assignment operator "="
• Variable names are case sensitive in PHP: $name is not the same as
$NAME or $Name.
• Variable naming rules similar to variable naming rules in other programming
languages
• In PHP a variable does not need to be declared before being set.
PHP is a Loosely Typed Language.
Strings in PHP
• a string is a sequence of letters, symbols, characters and arithmetic values or
combination of all tied together in single or double quotes.
• String literals are enclosed in single or double quotes
• Example:
<?php
$sum = 20;
echo 'the sum is: $sum';
echo "<br />";
echo "the sum is: $sum";
echo "<br />";
echo '<input type="text" name="first_name" id="first_name">';
?>
– Double quoted strings have escape sequences (such as /n or /r) interpreted and
variables interpolated (substituted)
– Single quoted strings have neither escape sequence interpretation nor variable
interpolation
– A literal $ sign in a double quoted string must be escaped with a backslash, 
– Double-quoted strings can cover multiple lines
Escaping quotes with in quotes
Example 1:
<?php
$str = ""This is a PHP string examples quotes"";
echo $str;
?>
Example 2
<?php
$str = 'It's a nice day today.';
echo $str;
?>
The Concatenation Operator
• The concatenation operator (.)  is used to put two string
values together.
• Example:
<?php
$txt1="Hello Everyone,";
$txt2="1234 is Dan’s home address";
echo $txt1.$txt2;
?>
PHP Operators
 Operators are used to operate on values.
 List of PHP Operators:
 Similar to Other programming language
 Arithamatic
 Assignment
 Bitwise
 Comparison
 Incrementing/decrementing
 Logical
 Array
PHP Function
 In php a function is a predefined set of
commands that are carried out when the
function is called.
 The real power of PHP comes from its
functions.
 PHP has more than 700 built-in or predefine
functions for you to use.
 Complete php string reference
 You can write your own functions
Using Built-in Functions
• Useful PHPString Functions
<?php
echo strlen("Hello world!");//prints string length
echo "<br />";
echo strpos("Hello world!","world"); //Prints //position of a
word
?>
</body>
</html>
Basic PHP Syntax
 Inserting external files:
 PHP provides four functions that enable you to insert
code from external files: include() or require()
include_once() or require_once() functions.
• E.g.
 include("table2.php");
– Includedfiles start incopymode
Using Built-in Function
 Examples: Inserting external files:
PHP provides four functions that enable you to insert code
from external files: include() or require() include_once() or
require_once() functions.
A sample include file called add.php
<html> <body>
<?php
function add( $x, $y ) {
return $x + $y; }
?>
<h1>Welcome to my home
page</h1>
<p>Some text</p>
</body> </html>
Using the include function
<?php
include('add.php');
echo add(2, 2); ?>
Using Built-in Function
 Inserting external files - continued:
 The functions are identical in every way, except how they
handle errors.
 The include() and include_once() functions generates a warning (but
the script will continue execution)
 The require() and require_once() functions generates a fatal error
(and the script execution will stop after the error).
 These functions are used to create functions, headers,
footers, or elements that can be reused on multiple pages.
 This can save the developer a considerable amount of time for
updating/editing.
Defining and Referencing a Function
Syntax
function functionname () { your code }
Example:
<html> <body>
<?php
Function Name()
{
echo "Ben John";
}
Name();
?>
</body> </html>
Conditional Statements
1. The If...Else Statement
Syntax
if (co nditio n) co de to be
e xe cute d if co nditio n is true ;
else co de to be e xe cute d if
co nditio n is false ;
<?php
$d=date("D");
if ($d=="Fri") echo "Have a nice
weekend!";
else echo "Have a nice day!";
?>
If more than one line should
be executed if a
condition is true/false,
the lines should be
enclosed within curly
braces:
Conditional Statements
2. The ElseIf Statement
• If you want to execute some code if one of several conditions
is true use the elseif statement
Syntax
if (co nditio n) co de to be e xe cute d if co nditio n is true ;
elseif (co nditio n) co de to be e xe cute d if co nditio n is true ;
else co de to be e xe cute d if co nditio n is false ;
PHP Switch Statement
• If you want to select one of many blocks of code to be executed, use
the Switch statement.
• The switch statement is used to avoid long blocks of if..elseif..else
code.
Syntax
switch (e xpre ssio n)
{
case labe l1 : co de to be e xe cute d if e xpre ssio n = labe l1 ;
break;
case labe l2: co de to be e xe cute d if e xpre ssio n = labe l2;
break;
default: co de to be e xe cute d if e xpre ssio n is diffe re nt fro m bo th labe l1
and labe l2;
}
PHP Looping
• Looping statements in PHP are used to execute the same
block of code a specified number of times.
• In PHP we have the following looping statements:
– while - loops through a block of code if and as long as a
specified condition is true
– do...while - loops through a block of code once, and then
repeats the loop as long as a special 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
PHP Arrays
 An array can store one or more values in a
single variable name.
 There are three different kind of arrays:
 Numeric array - An array with a numeric ID key
 Associative array - An array where each ID key is
associated with a value
 Multidimensional array - An array containing one
or more arrays
Tricks and Tips
• Coding
Prototype your web pages first
• Separate the design of the site from the coding
Turn repetitive code into functions
• Makes for more maintainable and reusable code
Turn grunt code into functions
• Database access, configuration file access
Tricks and Tips
• Debugging
Feature: PHP is not a strongly typed language
• Variables can be created anywhere in your code
Undocumented Feature: PHP is not a strongly
typed language
• Typos in variable names will cause stuff to
happen
Tricks and Tips
• Debugging
Use scripts to dump form and session variables
• Write scripts to dump data to discover bad or
missing data
Tricks and Tips
• Development Tools
Color coding editors
• vim, Emacs, Visual SlickEdit
IDEs
• Windows
– Macromedia Dreamweaver
– Allaire Homesite
– Zend’s PHPEdit
– netbeans
• Linux
– ???
PHP and the Web

www.intellibitz.com Is typed in firefox

Firefox sends a message over the internet to
the computer named www.intellibitz.com

Apache, a program running on
www.intellibitz.com, gets the message and
asks the PHP interpreter, another program
running on the www.intellibitz.com computer,
“what does /index.php look like?”
PHP and the Web

The PHP interpreter reads the file
/var/www/index.php from disk drive

The PHP interpreter runs the commands in
index.php, possibly exchanging data with a
database program such as MySQL

The PHP interpreter takes the index.php
program output and sends it back to Apache
as answer
PHP and the Web

Apache sends the page contents it got from
the PHP interpreter back to your computer
over the Internet in response to Firefox

Firefox displays the page on the screen,
following the instructions of the HTML tags in
the page
Security
•About 30% of all vulnerabilities listed on the National Vulnerability
Database are linked to PHP.
•These vulnerabilities are caused mostly by not following best practice
programming rules; technical security flaws of the language itself or of
its core libraries are not frequent
•programmers make mistakes, some languages include taint
checking to automatically detect the lack of input validation which
induces many issues.
•There are advanced protection patches such as Suhosin and
Hardening- Patch, especially designed for web hosting environments.
Questions?
– Any Questions
• www.php.net
– Community
• www.phpbuilder.com: articles on PHP, discussion
forums
– Newsgroups
• comp.lang.php

Contenu connexe

Tendances (20)

Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
php
phpphp
php
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Php
PhpPhp
Php
 
01 Php Introduction
01 Php Introduction01 Php Introduction
01 Php Introduction
 
Php introduction
Php introductionPhp introduction
Php introduction
 
Javascript essentials
Javascript essentialsJavascript essentials
Javascript essentials
 
PHP slides
PHP slidesPHP slides
PHP slides
 
Php ppt
Php pptPhp ppt
Php ppt
 
Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorial
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
 
Java script ppt
Java script pptJava script ppt
Java script ppt
 
Php variables
Php variablesPhp variables
Php variables
 
Php
PhpPhp
Php
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node js
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
Php tutorial
Php  tutorialPhp  tutorial
Php tutorial
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 

En vedette

Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting languageElmer Concepcion Jr.
 
Pgdca final syllabus_2007_revised_31st_july_2007
Pgdca final syllabus_2007_revised_31st_july_2007Pgdca final syllabus_2007_revised_31st_july_2007
Pgdca final syllabus_2007_revised_31st_july_2007litu9439
 
pcb making in hindi pdf( पीसीबी बनाने की विधि हिंदी में )
pcb making in hindi pdf( पीसीबी बनाने की विधि हिंदी में )pcb making in hindi pdf( पीसीबी बनाने की विधि हिंदी में )
pcb making in hindi pdf( पीसीबी बनाने की विधि हिंदी में )Chand Rook
 
Router components in hindi
Router components in hindiRouter components in hindi
Router components in hindiVipin sharma
 
Pagemaker hindi notes
Pagemaker hindi notesPagemaker hindi notes
Pagemaker hindi notesBadal Rajput
 
C language in hindi (cलेग्वेज इन हिंदी )
C language  in hindi (cलेग्वेज इन हिंदी )C language  in hindi (cलेग्वेज इन हिंदी )
C language in hindi (cलेग्वेज इन हिंदी )Chand Rook
 
Introduction To PHP
Introduction To PHPIntroduction To PHP
Introduction To PHPShweta A
 
Computer netwoking notes & qustionspart 2
Computer netwoking notes & qustionspart 2Computer netwoking notes & qustionspart 2
Computer netwoking notes & qustionspart 2SirajRock
 
Photoshop hindi-notes
Photoshop hindi-notesPhotoshop hindi-notes
Photoshop hindi-notesSirajRock
 
Networking in hindi notes
Networking in hindi notesNetworking in hindi notes
Networking in hindi notesSirajRock
 
Excel shortcut and function keys hindi notes
Excel shortcut and function keys hindi notesExcel shortcut and function keys hindi notes
Excel shortcut and function keys hindi notesSirajRock
 
Internet notes hindi
Internet notes hindiInternet notes hindi
Internet notes hindiSirajRock
 
Introduction of Internet Hindi Notes
Introduction of Internet Hindi NotesIntroduction of Internet Hindi Notes
Introduction of Internet Hindi NotesSirajRock
 
Microsoft office hindi notes
Microsoft office hindi notesMicrosoft office hindi notes
Microsoft office hindi notesSirajRock
 
Corel draw 14 hindi notes
Corel draw 14 hindi notesCorel draw 14 hindi notes
Corel draw 14 hindi notesSirajRock
 

En vedette (16)

Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting language
 
Pgdca final syllabus_2007_revised_31st_july_2007
Pgdca final syllabus_2007_revised_31st_july_2007Pgdca final syllabus_2007_revised_31st_july_2007
Pgdca final syllabus_2007_revised_31st_july_2007
 
Computer basic course
Computer basic courseComputer basic course
Computer basic course
 
pcb making in hindi pdf( पीसीबी बनाने की विधि हिंदी में )
pcb making in hindi pdf( पीसीबी बनाने की विधि हिंदी में )pcb making in hindi pdf( पीसीबी बनाने की विधि हिंदी में )
pcb making in hindi pdf( पीसीबी बनाने की विधि हिंदी में )
 
Router components in hindi
Router components in hindiRouter components in hindi
Router components in hindi
 
Pagemaker hindi notes
Pagemaker hindi notesPagemaker hindi notes
Pagemaker hindi notes
 
C language in hindi (cलेग्वेज इन हिंदी )
C language  in hindi (cलेग्वेज इन हिंदी )C language  in hindi (cलेग्वेज इन हिंदी )
C language in hindi (cलेग्वेज इन हिंदी )
 
Introduction To PHP
Introduction To PHPIntroduction To PHP
Introduction To PHP
 
Computer netwoking notes & qustionspart 2
Computer netwoking notes & qustionspart 2Computer netwoking notes & qustionspart 2
Computer netwoking notes & qustionspart 2
 
Photoshop hindi-notes
Photoshop hindi-notesPhotoshop hindi-notes
Photoshop hindi-notes
 
Networking in hindi notes
Networking in hindi notesNetworking in hindi notes
Networking in hindi notes
 
Excel shortcut and function keys hindi notes
Excel shortcut and function keys hindi notesExcel shortcut and function keys hindi notes
Excel shortcut and function keys hindi notes
 
Internet notes hindi
Internet notes hindiInternet notes hindi
Internet notes hindi
 
Introduction of Internet Hindi Notes
Introduction of Internet Hindi NotesIntroduction of Internet Hindi Notes
Introduction of Internet Hindi Notes
 
Microsoft office hindi notes
Microsoft office hindi notesMicrosoft office hindi notes
Microsoft office hindi notes
 
Corel draw 14 hindi notes
Corel draw 14 hindi notesCorel draw 14 hindi notes
Corel draw 14 hindi notes
 

Similaire à Basics PHP

Similaire à Basics PHP (20)

Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
 
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
 
PHP ITCS 323
PHP ITCS 323PHP ITCS 323
PHP ITCS 323
 
Prersentation
PrersentationPrersentation
Prersentation
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
Php unit i
Php unit iPhp unit i
Php unit i
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHP
 
Materi Dasar PHP
Materi Dasar PHPMateri Dasar PHP
Materi Dasar PHP
 
PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course Deck
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
php Chapter 1.pptx
php Chapter 1.pptxphp Chapter 1.pptx
php Chapter 1.pptx
 
Introduction to-php
Introduction to-phpIntroduction to-php
Introduction to-php
 
PHP MySQL Workshop - facehook
PHP MySQL Workshop - facehookPHP MySQL Workshop - facehook
PHP MySQL Workshop - facehook
 
Php intro
Php introPhp intro
Php intro
 
Php intro
Php introPhp intro
Php intro
 
Php intro
Php introPhp intro
Php intro
 
Php Basics
Php BasicsPhp Basics
Php Basics
 

Dernier

Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 

Dernier (20)

Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 

Basics PHP

  • 2. Server-Side Scripting  What is a script?  Collection of program or sequence of instructions  Processed/interpreted by another program  Rather than by a processor  Client-side  Server-side  In server-side scripting, PHP ASP.net - 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.  Important fot dynamic HTML
  • 3. Introduction to PHP PHP stands for: Hypertext PreProcessor Developed by Rasmus Lerdorf in 1994(Personal Home Page) – Originally a set of Perl scripts known as the “Personal Home Page” tools • Source code released in 1995 • PHP 3 in 1997-98 by Andi Gutmans and Zeev Suraski • Latest version 5.5.1 • It is a powerful server-side scripting language for creating dynamic and interactive websites. • It is an open source software, which is widely used and free to download and use (php.net). • It is an efficient alternative to competitors such as Microsoft's ASP.
  • 4. Introduction to PHP • PHP is perfectly suited for Web development and can be embedded directly into the HTML code. • The PHP syntax is very similar to JavaScript, Perl and C. • PHP is often used together with Apache (web server) on various operating systems. It also supports ISAPI and can be used with Microsoft's IIS on Windows. • PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
  • 5. Introduction to PHP • What is a PHPFile? • PHP files have a file extension of .php, .phtml, .php4 .php3, .php5, .phps • PHP files can contain text, HTML tags and scripts • PHP files are returned to the browser as plain HTML 
  • 6. Introduction to PHP What you need to develop PHPApplication: • Install Apache (or IIS) on your own server, install PHP, and MySQL • OR • Install Wampserver2 (a bundle of PHP, Apache, and MySql server) on your own server/machine
  • 7. PHP Installation Downloads Free Download  PHP: http://www.php.net/downloads.php  MySQL Database: http://www.mysql.com/downloads/index.html  Apache Server: http://httpd.apache.org/download.cgi • How to install and configure apache • Here is a link to a good tutorial from PHP.net on how to install PHP5: http://www.php.net/manual/en/install.php
  • 8. How PHP is Processed • When a PHP document is requested of a server, the server will send the document first to a PHP processor • Two modes of operation – Copy mode in which plain HTML is copied to the output – Interpret mode in which PHP code is interpreted and the output from that code sent to output – The client never sees PHP code, only the output produced by the code
  • 9. Basic PHP Syntax • starts with <?php and ends with ?> <?php ……………. ?> – Other options are: 1. <? ……………… ?> or<?= ?>(shortened forms) 2. <script language=”php”> ... </script> • There are three basic statements to output text with PHP: echo, print, and printf. Example: echo 'This is a <b>test</b>!'; • Comments: – # – // – /* . . . * /
  • 10. Basic PHP Syntax • PHP statements are terminated with semicolons ; • Curly braces, { } are used to create compound statements • PHP has typical scripting language characteristics – Dynamic typing, un-typed variables – Associative arrays – Pattern matching – Extensive libraries • Primitives, Operations, Expressions – Four scalar types: boolean, integer, double, string – Two compound types: array, object – Two special types: resource and NULL
  • 11. Basic PHP Syntax Example 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Simple PHP Example</title> <body> <?php echo "Hello Class of 2011. This is my first PHP Script"; echo "<br />"; print "<b><i>What have you learnt and how many friends have you made?</i></b>"; echo "<br /><a href='PHP I-BSIC.ppt'>PHP BASIC</a>"; ?> </body> </html>
  • 12. PHP Language Basics • Constants, Data Types and Variables • Constants define a string or numeric value • Constants do not begin with a dollar sign • Examples: • define(“COMPANY”, “Acme Enterprises”); • define(“YELLOW”, “#FFFF00”); • define(“YELLOW”, “#FFFF00”); • define(“PI”, 3.14); • define(“NL”, “<br>n”); Using a constant
  • 13. PHP Language Basics • Constants, Data Types and Variables Data types • Integers, doubles and strings – isValid = true; // Boolean – 25 // Integer – 3.14 // Double – ‘Four’ // String – “Total value” // Another string
  • 14. PHP Language Basics • Constants, Data Types and Variables Data types • Strings and type conversion – $street = 123; – $street = $street . “ Main Street”; – $city = ‘Naperville’; $state = ‘IL’; – $address = $street; – $address = $address . NL . “$city, $state”; – $number = $address + 1; // $number equals 124
  • 15. PHP Language Basics • Constants, Data Types and Variables Data types • Arrays – Perl-like syntax » $arr = array("foo" => "bar", 12 => true); – same as » $arr[“foo”] = “bar”; » $arr[12] = true;
  • 16. PHP Language Basics • Constants, Data Types and Variables • Arrays (cont.) – <?php $arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42)); echo $arr["somearray"][6]; // 5 echo $arr["somearray"][13]; // 9 echo $arr["somearray"]["a"]; // 42 ?>
  • 17. PHP Language Basics • Constants, Data Types and Variables Operators – Contains all of the operators like in C and Perl (even the ternary) Statements – if, if/elseif – Switch/case – for, while, and do/while loops – Include and require statements for code reuse
  • 18. PHP Variables • Variables are used for storing values, such as numbers, strings or function results, so that they can be used many times in a script. • All variables in PHP start with a $ sign symbol. • Variables are assigned using the assignment operator "=" • Variable names are case sensitive in PHP: $name is not the same as $NAME or $Name. • Variable naming rules similar to variable naming rules in other programming languages • In PHP a variable does not need to be declared before being set. PHP is a Loosely Typed Language.
  • 19. Strings in PHP • a string is a sequence of letters, symbols, characters and arithmetic values or combination of all tied together in single or double quotes. • String literals are enclosed in single or double quotes • Example: <?php $sum = 20; echo 'the sum is: $sum'; echo "<br />"; echo "the sum is: $sum"; echo "<br />"; echo '<input type="text" name="first_name" id="first_name">'; ?> – Double quoted strings have escape sequences (such as /n or /r) interpreted and variables interpolated (substituted) – Single quoted strings have neither escape sequence interpretation nor variable interpolation – A literal $ sign in a double quoted string must be escaped with a backslash, – Double-quoted strings can cover multiple lines
  • 20. Escaping quotes with in quotes Example 1: <?php $str = ""This is a PHP string examples quotes""; echo $str; ?> Example 2 <?php $str = 'It's a nice day today.'; echo $str; ?>
  • 21. The Concatenation Operator • The concatenation operator (.)  is used to put two string values together. • Example: <?php $txt1="Hello Everyone,"; $txt2="1234 is Dan’s home address"; echo $txt1.$txt2; ?>
  • 22. PHP Operators  Operators are used to operate on values.  List of PHP Operators:  Similar to Other programming language  Arithamatic  Assignment  Bitwise  Comparison  Incrementing/decrementing  Logical  Array
  • 23. PHP Function  In php a function is a predefined set of commands that are carried out when the function is called.  The real power of PHP comes from its functions.  PHP has more than 700 built-in or predefine functions for you to use.  Complete php string reference  You can write your own functions
  • 24. Using Built-in Functions • Useful PHPString Functions <?php echo strlen("Hello world!");//prints string length echo "<br />"; echo strpos("Hello world!","world"); //Prints //position of a word ?> </body> </html>
  • 25. Basic PHP Syntax  Inserting external files:  PHP provides four functions that enable you to insert code from external files: include() or require() include_once() or require_once() functions. • E.g.  include("table2.php"); – Includedfiles start incopymode
  • 26. Using Built-in Function  Examples: Inserting external files: PHP provides four functions that enable you to insert code from external files: include() or require() include_once() or require_once() functions. A sample include file called add.php <html> <body> <?php function add( $x, $y ) { return $x + $y; } ?> <h1>Welcome to my home page</h1> <p>Some text</p> </body> </html> Using the include function <?php include('add.php'); echo add(2, 2); ?>
  • 27. Using Built-in Function  Inserting external files - continued:  The functions are identical in every way, except how they handle errors.  The include() and include_once() functions generates a warning (but the script will continue execution)  The require() and require_once() functions generates a fatal error (and the script execution will stop after the error).  These functions are used to create functions, headers, footers, or elements that can be reused on multiple pages.  This can save the developer a considerable amount of time for updating/editing.
  • 28. Defining and Referencing a Function Syntax function functionname () { your code } Example: <html> <body> <?php Function Name() { echo "Ben John"; } Name(); ?> </body> </html>
  • 29. Conditional Statements 1. The If...Else Statement Syntax if (co nditio n) co de to be e xe cute d if co nditio n is true ; else co de to be e xe cute d if co nditio n is false ; <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?> If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces:
  • 30. Conditional Statements 2. The ElseIf Statement • If you want to execute some code if one of several conditions is true use the elseif statement Syntax if (co nditio n) co de to be e xe cute d if co nditio n is true ; elseif (co nditio n) co de to be e xe cute d if co nditio n is true ; else co de to be e xe cute d if co nditio n is false ;
  • 31. PHP Switch Statement • If you want to select one of many blocks of code to be executed, use the Switch statement. • The switch statement is used to avoid long blocks of if..elseif..else code. Syntax switch (e xpre ssio n) { case labe l1 : co de to be e xe cute d if e xpre ssio n = labe l1 ; break; case labe l2: co de to be e xe cute d if e xpre ssio n = labe l2; break; default: co de to be e xe cute d if e xpre ssio n is diffe re nt fro m bo th labe l1 and labe l2; }
  • 32. PHP Looping • Looping statements in PHP are used to execute the same block of code a specified number of times. • In PHP we have the following looping statements: – while - loops through a block of code if and as long as a specified condition is true – do...while - loops through a block of code once, and then repeats the loop as long as a special 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
  • 33. PHP Arrays  An array can store one or more values in a single variable name.  There are three different kind of arrays:  Numeric array - An array with a numeric ID key  Associative array - An array where each ID key is associated with a value  Multidimensional array - An array containing one or more arrays
  • 34. Tricks and Tips • Coding Prototype your web pages first • Separate the design of the site from the coding Turn repetitive code into functions • Makes for more maintainable and reusable code Turn grunt code into functions • Database access, configuration file access
  • 35. Tricks and Tips • Debugging Feature: PHP is not a strongly typed language • Variables can be created anywhere in your code Undocumented Feature: PHP is not a strongly typed language • Typos in variable names will cause stuff to happen
  • 36. Tricks and Tips • Debugging Use scripts to dump form and session variables • Write scripts to dump data to discover bad or missing data
  • 37. Tricks and Tips • Development Tools Color coding editors • vim, Emacs, Visual SlickEdit IDEs • Windows – Macromedia Dreamweaver – Allaire Homesite – Zend’s PHPEdit – netbeans • Linux – ???
  • 38. PHP and the Web  www.intellibitz.com Is typed in firefox  Firefox sends a message over the internet to the computer named www.intellibitz.com  Apache, a program running on www.intellibitz.com, gets the message and asks the PHP interpreter, another program running on the www.intellibitz.com computer, “what does /index.php look like?”
  • 39. PHP and the Web  The PHP interpreter reads the file /var/www/index.php from disk drive  The PHP interpreter runs the commands in index.php, possibly exchanging data with a database program such as MySQL  The PHP interpreter takes the index.php program output and sends it back to Apache as answer
  • 40. PHP and the Web  Apache sends the page contents it got from the PHP interpreter back to your computer over the Internet in response to Firefox  Firefox displays the page on the screen, following the instructions of the HTML tags in the page
  • 41. Security •About 30% of all vulnerabilities listed on the National Vulnerability Database are linked to PHP. •These vulnerabilities are caused mostly by not following best practice programming rules; technical security flaws of the language itself or of its core libraries are not frequent •programmers make mistakes, some languages include taint checking to automatically detect the lack of input validation which induces many issues. •There are advanced protection patches such as Suhosin and Hardening- Patch, especially designed for web hosting environments.
  • 42. Questions? – Any Questions • www.php.net – Community • www.phpbuilder.com: articles on PHP, discussion forums – Newsgroups • comp.lang.php