SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorial
alexjones89
 

Was ist angesagt? (20)

Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorial
 
Php tutorial
Php  tutorialPhp  tutorial
Php tutorial
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
PHP
PHPPHP
PHP
 
Php
PhpPhp
Php
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Php
PhpPhp
Php
 
01 Php Introduction
01 Php Introduction01 Php Introduction
01 Php Introduction
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop Notes
 
Introduction To PHP
Introduction To PHPIntroduction To PHP
Introduction To PHP
 

Andere mochten auch

pcb making in hindi pdf( पीसीबी बनाने की विधि हिंदी में )
pcb making in hindi pdf( पीसीबी बनाने की विधि हिंदी में )pcb making in hindi pdf( पीसीबी बनाने की विधि हिंदी में )
pcb making in hindi pdf( पीसीबी बनाने की विधि हिंदी में )
Chand Rook
 
Computer netwoking notes & qustionspart 2
Computer netwoking notes & qustionspart 2Computer netwoking notes & qustionspart 2
Computer netwoking notes & qustionspart 2
SirajRock
 
Photoshop hindi-notes
Photoshop hindi-notesPhotoshop hindi-notes
Photoshop hindi-notes
SirajRock
 
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
SirajRock
 
Internet notes hindi
Internet notes hindiInternet notes hindi
Internet notes hindi
SirajRock
 
Introduction of Internet Hindi Notes
Introduction of Internet Hindi NotesIntroduction of Internet Hindi Notes
Introduction of Internet Hindi Notes
SirajRock
 
Microsoft office hindi notes
Microsoft office hindi notesMicrosoft office hindi notes
Microsoft office hindi notes
SirajRock
 
Corel draw 14 hindi notes
Corel draw 14 hindi notesCorel draw 14 hindi notes
Corel draw 14 hindi notes
SirajRock
 

Andere mochten auch (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लेग्वेज इन हिंदी )
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
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
 

Ähnlich wie Basics PHP

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)
Muhamad Al Imran
 
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)
Muhamad Al Imran
 
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)
Muhamad Al Imran
 

Ähnlich wie 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
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
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 unit i
Php unit iPhp unit i
Php unit i
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
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
 
WT_PHP_PART1.pdf
WT_PHP_PART1.pdfWT_PHP_PART1.pdf
WT_PHP_PART1.pdf
 

Kürzlich hochgeladen

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Kürzlich hochgeladen (20)

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 

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