SlideShare una empresa de Scribd logo
1 de 63
Introduction to PHP
M.S. Chahar
• Introduction
• PHP is one of the most prevalent Web
programming languages for creating dynamic
Web content. Its similarity to C’s syntax and
open-source nature make PHP relatively easy to
learn. This Topic provides a brief overview of
PHP, several reasons for using PHP as a Web
programming language, and a list of minimum
requirements to begin working with PHP.
• PHP is a recursive acronym for "PHP: Hypertext
Preprocessor".
M.S. Chahar
• PHP is a server side scripting language that is
embedded in HTML. It is used to manage dynamic
content, databases, session tracking, even build entire
e-commerce sites.
• It is integrated with a number of popular databases,
including MySQL, PostgreSQL, Oracle, Sybase, Informix,
and Microsoft SQL Server.
• PHP is pleasingly zippy in its execution, especially when
compiled as an Apache module on the Unix side. The
MySQL server, once started, executes even very
complex queries with huge result sets in record-setting
time.
• PHP supports a large number of major protocols such
as POP3, IMAP, and LDAP. PHP4 added support for
Java and distributed object architectures (COM and
CORBA), making n-tier development a possibility for
the first time. M.S. Chahar
Characteristics of PHP
• Five important characteristics make PHP's
practical nature possible:
• Simplicity
• Efficiency
• Security
• Flexibility
• Familiarity
M.S. Chahar
What is PHP?
• PHP is a scripting language, created in 1994 by
Rasmus Lerdorf, that is designed for producing
dynamic Web content.
• PHP originally stood for “Personal Home
Page,” but as its functionality expanded, it
became “PHP: Hypertext Processor” because
it takes PHP code as input and produces HTML
as output.
M.S. Chahar
• PHP is typically used for Web-based
applications, it can also be used for command-
line scripting – a task commonly handled by
Perl – as well as client-side applications
(possibly with a graphical user interface) – a
task commonly handled by Python or Java.
• NOTE: This course only addresses PHP in the
context of dynamic Web content creation.
M.S. Chahar
PHP is…
• Interpreted rather than compiled like Java or C.
• An embedded scripting language, meaning that
it can exist within HTML code.
• A server-side technology; everything happens on
the server as opposed to the Web browser’s
computer, the client.
• Cross-platform, meaning it can be used on Linux,
Windows, Macintosh, etc., making PHP very
portable.
M.S. Chahar
PHP does not…
• Handle client-side tasks such as creating a new
browser window, adding mouseovers,
determining the screen size of the user’s
machine, etc. Such tasks are handled by
JavaScript or Ajax (Asynchronous JavaScript and
XML).
• Do anything within the Web browser until a
request from the client has been made (e.g.,
submitting a form,
• clicking a link, etc.).
M.S. Chahar
Why use PHP?
• PHP has been described as being “better, faster, and
easier to learn than the alternatives.” (Ullman, 2004)
The alternatives being…
• HTML – static, no database/file interaction, cannot
handle email, etc.
• CGI (Common Gateway Interface) scripts – Perl or C.
• Microsoft ASP (Active Server Pages), or ASP.NET –
need to learn VBScript, C#, or another language and is
limited to servers running Microsoft Windows Server.
• JSP (JavaServer Pages) – Java-based, so there is a
higher learning curve plus you deal with the overhead
of a
• compiled language: JavaServer sites are slower than
other sites.
M.S. Chahar
• PHP is free, works on multiple platforms, and
is open source (i.e., user-driven development,
not part of a corporation). These attributes
combined with a large development
community and the wealth of available PHP
libraries make PHP an attractive choice for
Web developers.
M.S. Chahar
Cont….
• The Web content delivery process has the same
beginning and end: A user requests a URL from
the Web server, and HTML markup is sent to the
user’s browser. With the static HTML delivery
process, the Web server sends the exact content
of the requested page to the client without any
additional processing. With dynamic Web
content creation, PHP acts as a filter by
processing PHP code – possibly embedded within
HTML markup – to produce HTML markup that is
ultimately sent to the user’s browser.
M.S. Chahar
PHP requirements
• The following elements represent the minimum
requirements to begin working with PHP. Concepts such as
PHP installation and configuration are beyond the scope of
this course.
• PHP-enabled Web server. The EECS/Claxton Web server is
running PHP 5.2.
• If you are working on your own machine, you will need an
SFTP client to transfer your PHP scripts to the Webserver.
Otherwise, you can edit your files directly on the file
system via an SSH session with a text editor (e.g., vi,
emacs).
• Web browser (e.g., Mozilla Firefox, Apple Safari, Microsoft
Windows Internet Explorer).
M.S. Chahar
Benefits of Using PHP for Custom
Web Development
• PHP is the ruling language in the world of web design
and development industry. This server-side scripting
language has been especially designed for web
development and can be embedded into HTML.
• The usage of PHP web development service, the
language has prevailed in the industry since its advent.
Most of the web designing and development
companies put their efforts, time and resources in
mastering this language, and the trained PHP
developers helped the end users to get user friendly,
cost-effective and impressive websites.
M.S. Chahar
• The flexibility, usability and scalability provided
by PHP development won the hearts of
developers, site owners, as well as of the users.
• The dynamic PHP websites took less time to load
and were extremely easy to operate. Apart from
these benefits bestowed upon the end users, PHP
holds several benefits for the site owners too.
Out of the several ones, here are the four
exclusive benefits of using this language for
custom website development.
M.S. Chahar
• Cost Savings
• Freedom of Choice
• Support for Databases
• Easy Availability of Resources
M.S. Chahar
Hello print PHP Example
• <?php //start php tags
• echo "hello m s chahar"; //print statement
• ?> //end php tags
M.S. Chahar
Commenting PHP Code:
• A comment is the portion of a program that
exists only for the human reader and stripped
out before displaying the programs result.
There are two commenting formats in PHP:
• Single-line comments: They are generally
used for short explanations or notes relevant
to the local code. Here are the examples of
single line comments.
M.S. Chahar
Single-line comments
<?
# This is a comment, and
# This is the second line of the comment
// This is a comment too. Each style comments
onlyprint "An example with single line
comments";
?>
M.S. Chahar
Multi-lines printing
• Multi-lines printing: Here are the examples to
print multiple lines in a single print statement:
M.S. Chahar
<?
# First Exampleprint <<<END
This uses the "here document" syntax to outputmultiple
lines with $variable interpolation. Notethat the here
document terminator must appear on aline with just a
semicolon no extra whitespace!
END;
# Second Exampleprint "This spansmultiple lines. The
newlines will beoutput as well";
?>
M.S. Chahar
Multi-lines comments
• Multi-lines comments: They are generally
used to provide pseudocode algorithms and
more detailed explanations when necessary.
The multiline style of commenting is the same
as in C. Here are the example of multi lines
comments.
M.S. Chahar
<?
/* This is a comment with multiline Author :
Mohammad Mohtashim Purpose: Multiline
Comments Demo Subject: PHP */
print "An example with multi line comments";
?>
M.S. Chahar
PHP Variable Types
• The main way to store information in the
middle of a PHP program is by using a
variable.
– The most important things to know about
variables in PHP.
M.S. Chahar
Roles in PHP variable• All variables in PHP are denoted with a leading dollar sign ($).
• The value of a variable is the value of its most recent
assignment.
• Variables are assigned with the = operator, with the variable
on the left-hand side and the expression to be evaluated on
the right.
• Variables can, but do not need, to be declared before
assignment.
• Variables in PHP do not have intrinsic types - a variable does
not know in advance whether it will be used to store a
number or a string of characters.
• Variables used before they are assigned have default values.
• PHP does a good job of automatically converting types from
one to another when necessary.
• PHP variables are Perl-like.
M.S. Chahar
Data Type in PHP
• PHP has a total of Eight data types :
• Integers: are whole numbers, without a decimal point, like
4195.
• Doubles: are floating-point numbers, like 3.14159 or 49.1.
• Booleans: have only two possible values either true or false.
• NULL: is a special type that only has one value: NULL.
• Strings: are sequences of characters, like 'PHP supports string
operations.'
• Arrays: are named and indexed collections of other values.
• Objects: are instances of programmer-defined classes, which
can package up both other kinds of values and functions that
are specific to the class.
• Resources: are special variables that hold references to
resources external to PHP (such as database connections).M.S. Chahar
• PHP Constants
• A constant is a name or an identifier for a
simple value. A constant value cannot change
during the execution of the script.
• A constant you have to use define() function.
M.S. Chahar
constant() example:
• <?php
•
• define("MINSIZE", 50);
•
• echo MINSIZE;
• echo constant("MINSIZE"); // same thing as the
previous line
•
• ?>
M.S. Chahar
Differences between constants and
variables are:
• There is no need to write a dollar sign ($) before
a constant, where as in Variable one has to write
a dollar sign.
• Constants cannot be defined by simple
assignment, they may only be defined using the
define() function.
• Constants may be defined and accessed
anywhere without regard to variable scoping
rules.
• Once the Constants have been set, may not be
redefined or undefined.
M.S. Chahar
PHP Operator Types
• Arithmetic Operators
• Comparision Operators
• Logical (or Relational) Operators
• Assignment Operators
• Conditional (or ternary) Operators
M.S. Chahar
PHP Decision Making
• The if, elseif ...else and switch statements are
used to take decision based on the different
condition.
• if...else statement - use this statement if you
want to execute a set of code when a
condition is true and another if the condition
is not true
M.S. Chahar
Syntax
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
Ex….
• <html>
• <body>
• <?php
• $d=date("D");
• if ($d=="Fri")
• echo "Have a nice weekend!";
• else echo "Have a nice day!"; ?>
• </body>
• </html>
M.S. Chahar
• elseif statement - is used with the if...else
statement to execute a set of code if one of
several condition are true
M.S. Chahar
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;
M.S. Chahar
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo"Haveaniceweekend!";
elseif ($d=="Sun")
echo"HaveaniceSunday!";
else
echo"Haveaniceday!";
?>
</body>
</html>
• switch statement - is used 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.
M.S. Chahar
Syntax
switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different
from both label1 and label2;
}
• <html> <body> <?php $d=date("D");
switch ($d) { case "Mon": echo "Today is
Monday"; break; case "Tue": echo
"Today is Tuesday"; break; case "Wed":
echo "Today is Wednesday"; break; case
"Thu": echo "Today is Thursday"; break;
case "Fri": echo "Today is Friday"; break;
case "Sat": echo "Today is Saturday";
break; case "Sun": echo "Today is
Sunday"; break; default: echo "Wonder
which day is this ?"; } ?> </body>
</html> M.S. Chahar
PHP Loop Types
• PHP are used to execute the same block of
code a specified number of times. PHP
supports following four loop types.
• for -
• while -
• do...while -
• foreach - loops through a block of code for
each element in an array.
M.S. Chahar
• The foreach loop statement
• The foreach statement is used to loop through
arrays. For each pass the value of the current
array element is assigned to $value and the
array pointer is moved by one and in the next
pass next element will be processed.
M.S. Chahar
M.S. Chahar
Syntax
foreach(arrayasvalue)
{
codetobeexecuted;
}
Example foreach loop
M.S. Chahar
<html>
<body>
<?php
$array=array(1,2,3,4,5);
foreach($arrayas$value)
{
echo"Valueis$value<br/>";
}
?>
</body>
</html>
PHP Arrays
• An array is a data structure that stores one or
more similar type of values in a single value.
• For example if you want to store 100 numbers
then instead of defining 100 variables its easy
to define an array of 100 length.
M.S. Chahar
• There are three different kind of arrays and
each array value is accessed using an ID c
which is called array index.
– Numeric array - An array with a numeric index.
Values are stored and accessed in linear fashion
– Associative array - An array with strings as index.
This stores element values in association with key
values rather than in a strict linear index order.
– Multidimensional array - An array containing one
or more arrays and values are accessed using
multiple indices
M.S. Chahar
Numeric Array
• These arrays can store numbers, strings and
any object but their index will be
prepresented by numbers. By default array
index starts from zero.
M.S. Chahar
• <html>
• <body>
• <?php
• /* First method to create array. */
• $numbers = array( 1, 2, 3, 4, 5);
• foreach( $numbers as $value )
• {
• echo "Value is $value <br />";
• }
• /* Second method to create array. */
• $numbers[0] = "one";
• $numbers[1] = "two";
• $numbers[2] = "three";
• $numbers[3] = "four";
• $numbers[4] = "five";
• foreach( $numbers as $value )
• {
• echo "Value is $value <br />";
• }
• ?>
• </body>
• </html>
M.S. Chahar
Associative Arrays
• The associative arrays are very similar to
numeric arrays in term of functionality but
they are different in terms of their index.
Associative array will have their index as string
so that you can establish a strong association
between key and values.
• Ex ass.php
M.S. Chahar
Multidimensional Arrays
• A multi-dimensional array each element in the
main array can also be an array. And each
element in the sub-array can be an array, and
so on. Values in the multi-dimensional array
are accessed using multiple index.
M.S. Chahar
PHP Strings
• They are sequences of characters, like "PHP
supports string operations".
M.S. Chahar
There are valid examples of string
• $string_1 = "This is a string in double quotes";
• $string_2 = "This is a somewhat longer, singly
quoted string";
• $string_39 = "This string has thirty-nine
characters";
• $string_0 = ""; // a string with zero characters
M.S. Chahar
• V.php
• St.php
M.S. Chahar
The escape-sequence
replacements are:
• n is replaced by the newline character
• r is replaced by the carriage-return character
• t is replaced by the tab character
• $ is replaced by the dollar sign itself ($)
• " is replaced by a single double-quote (")
•  is replaced by a single backslash ()
M.S. Chahar
PHP File Inclusion
• You can include the content of a PHP file into
another PHP file before the server executes it.
• There are two PHP functions which can be
used to included one PHP file into another
PHP file.
• The include() Function
• The require() Function
M.S. Chahar
• This is a strong point of PHP which helps in
creating functions, headers, footers, or
elements that can be reused on multiple
pages. This will help developers to make it
easy to change the layout of complete website
with minimal effort.
M.S. Chahar
The include() Function
• The include() function takes all the text in a
specified file and copies it into the file that
uses the include function. If there is any
problem in loading a file then the include()
function generates a warning but the script
will continue execution.
• you want to create a common menu for your
website. Then create a file ex.php.
M.S. Chahar
The require() Function
• The require() function takes all the text in a
specified file and copies it into the file that
uses the include function. If there is any
problem in loading a file then the require()
function generates a fatal error and halt the
execution of the script.
M.S. Chahar
• So there is no difference in require() and
include() except they handle error conditions.
It is recommended to use the require()
function instead of include(), because scripts
should not continue executing if files are
missing or misnamed.
M.S. Chahar
PHP Files & I/O
• Following functions related to files:
• Opening a file
• Reading a file
• Writing a file
• Closing a file
M.S. Chahar
• Opening and Closing Files
• PHP fopen() function is used to open a file. It
requires two arguments stating first the file
name and then mode in which to operate.
M.S. Chahar
Files modes can be specified as one of the
six options in this table.
M.S. Chahar
Mode Purpose
r
Opens the file for reading only.
Places the file pointer at the beginning of the file.
r+
Opens the file for reading and writing.
Places the file pointer at the beginning of the file.
w
Opens the file for writing only.
Places the file pointer at the beginning of the file.
and truncates the file to zero length. If files does not
exist then it attemts to create a file.
w+
Opens the file for reading and writing only.
Places the file pointer at the beginning of the file.
and truncates the file to zero length. If files does not
exist then it attemts to create a file.
a
Opens the file for writing only.
Places the file pointer at the end of the file.
If files does not exist then it attemts to create a file.
a+
Opens the file for reading and writing only.
Places the file pointer at the end of the file.
If files does not exist then it attemts to create a file.
• If an attempt to open a file fails then fopen
returns a value of false otherwise it returns a
file pointer which is used for further reading
or writing to that file.
• After making a changes to the opened file it is
important to close it with the fclose()
function. The fclose() function requires a file
pointer as its argument and then returns true
when the closure succeeds or false if it fails.
M.S. Chahar
Reading a file
• Once a file is opened using fopen() function it
can be read with a function called fread(). This
function requires two arguments. These must
be the file pointer and the length of the file
expressed in bytes.
• The files's length can be found using the
filesize() function which takes the file name as
its argument and returns the size of the file
expressed in bytes.
M.S. Chahar
• So here are the steps required to read a file
with PHP.
• Open a file using fopen() function.
• Get the file's length using filesize() function.
• Read the file's content using fread() function.
• Close the file with fclose() function.
• Example:File.php
M.S. Chahar
Writing a file
• A new file can be written or text can be
appended to an existing file using the PHP
fwrite() function. This function requires two
arguments specifying a file pointer and the
string of data that is to be written. Optionally
a third integer argument can be included to
specify the length of the data to write. If the
third argument is included, writing would will
stop after the specified length has been
reached. M.S. Chahar
PHP Cookies
• Cookies are text files stored on the client
computer and they are kept of use tracking
purpose.
M.S. Chahar
• There are three steps involved in identifying
returning users:
• Server script sends a set of cookies to the
browser. For example name, age, or
identification number etc.
• Browser stores this information on local
machine for future use.
• When next time browser sends any request to
web server then it sends those cookies
information to the server and server uses that
information to identify the user.
M.S. Chahar

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Php introduction
Php introductionPhp introduction
Php introduction
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
An Introduction to the DOM
An Introduction to the DOMAn Introduction to the DOM
An Introduction to the DOM
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and Sessions
 
Php
PhpPhp
Php
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The Basics
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Javascript
JavascriptJavascript
Javascript
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
 
Scripting languages
Scripting languagesScripting languages
Scripting languages
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
HTML frames and HTML forms
HTML frames and HTML formsHTML frames and HTML forms
HTML frames and HTML forms
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
 

Similar a Introduction to php

Php training in bhubaneswar
Php training in bhubaneswar Php training in bhubaneswar
Php training in bhubaneswar
litbbsr
 
CSS Adnaved with HTML abd complete Stylesheet
CSS Adnaved with HTML abd complete StylesheetCSS Adnaved with HTML abd complete Stylesheet
CSS Adnaved with HTML abd complete Stylesheet
PraveenHegde20
 
Learn PHP Lacture1
Learn PHP Lacture1Learn PHP Lacture1
Learn PHP Lacture1
ADARSH BHATT
 

Similar a Introduction to php (20)

Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1
 
Introduction to PHP.pptx
Introduction to PHP.pptxIntroduction to PHP.pptx
Introduction to PHP.pptx
 
Php intro
Php introPhp intro
Php intro
 
Php
PhpPhp
Php
 
1. introduction to php and variable
1. introduction to php and variable1. introduction to php and variable
1. introduction to php and variable
 
Php unit i
Php unit i Php unit i
Php unit i
 
PhP Training Institute In Delhi
PhP Training Institute In DelhiPhP Training Institute In Delhi
PhP Training Institute In Delhi
 
Php training in bhubaneswar
Php training in bhubaneswar Php training in bhubaneswar
Php training in bhubaneswar
 
Php training in bhubaneswar
Php training in bhubaneswar Php training in bhubaneswar
Php training in bhubaneswar
 
Advanced PHP Web Development Tools in 2015
Advanced PHP Web Development Tools in 2015Advanced PHP Web Development Tools in 2015
Advanced PHP Web Development Tools in 2015
 
web Based Application Devlopment using PHP
web Based Application Devlopment using PHPweb Based Application Devlopment using PHP
web Based Application Devlopment using PHP
 
Php1
Php1Php1
Php1
 
Introduction to webprogramming using PHP and MySQL
Introduction to webprogramming using PHP and MySQLIntroduction to webprogramming using PHP and MySQL
Introduction to webprogramming using PHP and MySQL
 
Php reports sumit
Php reports sumitPhp reports sumit
Php reports sumit
 
Web programming
Web programmingWeb programming
Web programming
 
PHP for web development
PHP for web development PHP for web development
PHP for web development
 
Web Technology Fundamentals
Web Technology FundamentalsWeb Technology Fundamentals
Web Technology Fundamentals
 
Php unit i
Php unit iPhp unit i
Php unit i
 
CSS Adnaved with HTML abd complete Stylesheet
CSS Adnaved with HTML abd complete StylesheetCSS Adnaved with HTML abd complete Stylesheet
CSS Adnaved with HTML abd complete Stylesheet
 
Learn PHP Lacture1
Learn PHP Lacture1Learn PHP Lacture1
Learn PHP Lacture1
 

Más de Meetendra Singh (6)

matlab_prog.pdf
matlab_prog.pdfmatlab_prog.pdf
matlab_prog.pdf
 
561 1530-1-pb (1)
561 1530-1-pb (1)561 1530-1-pb (1)
561 1530-1-pb (1)
 
Even syllabus All
Even syllabus All Even syllabus All
Even syllabus All
 
Dot Net csharp Language
Dot Net csharp LanguageDot Net csharp Language
Dot Net csharp Language
 
Orientationprogram2013
Orientationprogram2013Orientationprogram2013
Orientationprogram2013
 
Eee assignment
Eee assignmentEee assignment
Eee assignment
 

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
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
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
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
 

Último (20)

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
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
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
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"
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
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
 
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.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
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
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
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
 
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
 

Introduction to php

  • 2. • Introduction • PHP is one of the most prevalent Web programming languages for creating dynamic Web content. Its similarity to C’s syntax and open-source nature make PHP relatively easy to learn. This Topic provides a brief overview of PHP, several reasons for using PHP as a Web programming language, and a list of minimum requirements to begin working with PHP. • PHP is a recursive acronym for "PHP: Hypertext Preprocessor". M.S. Chahar
  • 3. • PHP is a server side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites. • It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server. • PHP is pleasingly zippy in its execution, especially when compiled as an Apache module on the Unix side. The MySQL server, once started, executes even very complex queries with huge result sets in record-setting time. • PHP supports a large number of major protocols such as POP3, IMAP, and LDAP. PHP4 added support for Java and distributed object architectures (COM and CORBA), making n-tier development a possibility for the first time. M.S. Chahar
  • 4. Characteristics of PHP • Five important characteristics make PHP's practical nature possible: • Simplicity • Efficiency • Security • Flexibility • Familiarity M.S. Chahar
  • 5. What is PHP? • PHP is a scripting language, created in 1994 by Rasmus Lerdorf, that is designed for producing dynamic Web content. • PHP originally stood for “Personal Home Page,” but as its functionality expanded, it became “PHP: Hypertext Processor” because it takes PHP code as input and produces HTML as output. M.S. Chahar
  • 6. • PHP is typically used for Web-based applications, it can also be used for command- line scripting – a task commonly handled by Perl – as well as client-side applications (possibly with a graphical user interface) – a task commonly handled by Python or Java. • NOTE: This course only addresses PHP in the context of dynamic Web content creation. M.S. Chahar
  • 7. PHP is… • Interpreted rather than compiled like Java or C. • An embedded scripting language, meaning that it can exist within HTML code. • A server-side technology; everything happens on the server as opposed to the Web browser’s computer, the client. • Cross-platform, meaning it can be used on Linux, Windows, Macintosh, etc., making PHP very portable. M.S. Chahar
  • 8. PHP does not… • Handle client-side tasks such as creating a new browser window, adding mouseovers, determining the screen size of the user’s machine, etc. Such tasks are handled by JavaScript or Ajax (Asynchronous JavaScript and XML). • Do anything within the Web browser until a request from the client has been made (e.g., submitting a form, • clicking a link, etc.). M.S. Chahar
  • 9. Why use PHP? • PHP has been described as being “better, faster, and easier to learn than the alternatives.” (Ullman, 2004) The alternatives being… • HTML – static, no database/file interaction, cannot handle email, etc. • CGI (Common Gateway Interface) scripts – Perl or C. • Microsoft ASP (Active Server Pages), or ASP.NET – need to learn VBScript, C#, or another language and is limited to servers running Microsoft Windows Server. • JSP (JavaServer Pages) – Java-based, so there is a higher learning curve plus you deal with the overhead of a • compiled language: JavaServer sites are slower than other sites. M.S. Chahar
  • 10. • PHP is free, works on multiple platforms, and is open source (i.e., user-driven development, not part of a corporation). These attributes combined with a large development community and the wealth of available PHP libraries make PHP an attractive choice for Web developers. M.S. Chahar
  • 11. Cont…. • The Web content delivery process has the same beginning and end: A user requests a URL from the Web server, and HTML markup is sent to the user’s browser. With the static HTML delivery process, the Web server sends the exact content of the requested page to the client without any additional processing. With dynamic Web content creation, PHP acts as a filter by processing PHP code – possibly embedded within HTML markup – to produce HTML markup that is ultimately sent to the user’s browser. M.S. Chahar
  • 12. PHP requirements • The following elements represent the minimum requirements to begin working with PHP. Concepts such as PHP installation and configuration are beyond the scope of this course. • PHP-enabled Web server. The EECS/Claxton Web server is running PHP 5.2. • If you are working on your own machine, you will need an SFTP client to transfer your PHP scripts to the Webserver. Otherwise, you can edit your files directly on the file system via an SSH session with a text editor (e.g., vi, emacs). • Web browser (e.g., Mozilla Firefox, Apple Safari, Microsoft Windows Internet Explorer). M.S. Chahar
  • 13. Benefits of Using PHP for Custom Web Development • PHP is the ruling language in the world of web design and development industry. This server-side scripting language has been especially designed for web development and can be embedded into HTML. • The usage of PHP web development service, the language has prevailed in the industry since its advent. Most of the web designing and development companies put their efforts, time and resources in mastering this language, and the trained PHP developers helped the end users to get user friendly, cost-effective and impressive websites. M.S. Chahar
  • 14. • The flexibility, usability and scalability provided by PHP development won the hearts of developers, site owners, as well as of the users. • The dynamic PHP websites took less time to load and were extremely easy to operate. Apart from these benefits bestowed upon the end users, PHP holds several benefits for the site owners too. Out of the several ones, here are the four exclusive benefits of using this language for custom website development. M.S. Chahar
  • 15. • Cost Savings • Freedom of Choice • Support for Databases • Easy Availability of Resources M.S. Chahar
  • 16. Hello print PHP Example • <?php //start php tags • echo "hello m s chahar"; //print statement • ?> //end php tags M.S. Chahar
  • 17. Commenting PHP Code: • A comment is the portion of a program that exists only for the human reader and stripped out before displaying the programs result. There are two commenting formats in PHP: • Single-line comments: They are generally used for short explanations or notes relevant to the local code. Here are the examples of single line comments. M.S. Chahar
  • 18. Single-line comments <? # This is a comment, and # This is the second line of the comment // This is a comment too. Each style comments onlyprint "An example with single line comments"; ?> M.S. Chahar
  • 19. Multi-lines printing • Multi-lines printing: Here are the examples to print multiple lines in a single print statement: M.S. Chahar
  • 20. <? # First Exampleprint <<<END This uses the "here document" syntax to outputmultiple lines with $variable interpolation. Notethat the here document terminator must appear on aline with just a semicolon no extra whitespace! END; # Second Exampleprint "This spansmultiple lines. The newlines will beoutput as well"; ?> M.S. Chahar
  • 21. Multi-lines comments • Multi-lines comments: They are generally used to provide pseudocode algorithms and more detailed explanations when necessary. The multiline style of commenting is the same as in C. Here are the example of multi lines comments. M.S. Chahar
  • 22. <? /* This is a comment with multiline Author : Mohammad Mohtashim Purpose: Multiline Comments Demo Subject: PHP */ print "An example with multi line comments"; ?> M.S. Chahar
  • 23. PHP Variable Types • The main way to store information in the middle of a PHP program is by using a variable. – The most important things to know about variables in PHP. M.S. Chahar
  • 24. Roles in PHP variable• All variables in PHP are denoted with a leading dollar sign ($). • The value of a variable is the value of its most recent assignment. • Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right. • Variables can, but do not need, to be declared before assignment. • Variables in PHP do not have intrinsic types - a variable does not know in advance whether it will be used to store a number or a string of characters. • Variables used before they are assigned have default values. • PHP does a good job of automatically converting types from one to another when necessary. • PHP variables are Perl-like. M.S. Chahar
  • 25. Data Type in PHP • PHP has a total of Eight data types : • Integers: are whole numbers, without a decimal point, like 4195. • Doubles: are floating-point numbers, like 3.14159 or 49.1. • Booleans: have only two possible values either true or false. • NULL: is a special type that only has one value: NULL. • Strings: are sequences of characters, like 'PHP supports string operations.' • Arrays: are named and indexed collections of other values. • Objects: are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class. • Resources: are special variables that hold references to resources external to PHP (such as database connections).M.S. Chahar
  • 26. • PHP Constants • A constant is a name or an identifier for a simple value. A constant value cannot change during the execution of the script. • A constant you have to use define() function. M.S. Chahar
  • 27. constant() example: • <?php • • define("MINSIZE", 50); • • echo MINSIZE; • echo constant("MINSIZE"); // same thing as the previous line • • ?> M.S. Chahar
  • 28. Differences between constants and variables are: • There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write a dollar sign. • Constants cannot be defined by simple assignment, they may only be defined using the define() function. • Constants may be defined and accessed anywhere without regard to variable scoping rules. • Once the Constants have been set, may not be redefined or undefined. M.S. Chahar
  • 29. PHP Operator Types • Arithmetic Operators • Comparision Operators • Logical (or Relational) Operators • Assignment Operators • Conditional (or ternary) Operators M.S. Chahar
  • 30. PHP Decision Making • The if, elseif ...else and switch statements are used to take decision based on the different condition. • if...else statement - use this statement if you want to execute a set of code when a condition is true and another if the condition is not true M.S. Chahar Syntax if (condition) code to be executed if condition is true; else code to be executed if condition is false;
  • 31. Ex…. • <html> • <body> • <?php • $d=date("D"); • if ($d=="Fri") • echo "Have a nice weekend!"; • else echo "Have a nice day!"; ?> • </body> • </html> M.S. Chahar
  • 32. • elseif statement - is used with the if...else statement to execute a set of code if one of several condition are true M.S. Chahar 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;
  • 33. M.S. Chahar <html> <body> <?php $d=date("D"); if ($d=="Fri") echo"Haveaniceweekend!"; elseif ($d=="Sun") echo"HaveaniceSunday!"; else echo"Haveaniceday!"; ?> </body> </html>
  • 34. • switch statement - is used 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. M.S. Chahar Syntax switch (expression) { case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }
  • 35. • <html> <body> <?php $d=date("D"); switch ($d) { case "Mon": echo "Today is Monday"; break; case "Tue": echo "Today is Tuesday"; break; case "Wed": echo "Today is Wednesday"; break; case "Thu": echo "Today is Thursday"; break; case "Fri": echo "Today is Friday"; break; case "Sat": echo "Today is Saturday"; break; case "Sun": echo "Today is Sunday"; break; default: echo "Wonder which day is this ?"; } ?> </body> </html> M.S. Chahar
  • 36. PHP Loop Types • PHP are used to execute the same block of code a specified number of times. PHP supports following four loop types. • for - • while - • do...while - • foreach - loops through a block of code for each element in an array. M.S. Chahar
  • 37. • The foreach loop statement • The foreach statement is used to loop through arrays. For each pass the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass next element will be processed. M.S. Chahar
  • 39. Example foreach loop M.S. Chahar <html> <body> <?php $array=array(1,2,3,4,5); foreach($arrayas$value) { echo"Valueis$value<br/>"; } ?> </body> </html>
  • 40. PHP Arrays • An array is a data structure that stores one or more similar type of values in a single value. • For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length. M.S. Chahar
  • 41. • There are three different kind of arrays and each array value is accessed using an ID c which is called array index. – Numeric array - An array with a numeric index. Values are stored and accessed in linear fashion – Associative array - An array with strings as index. This stores element values in association with key values rather than in a strict linear index order. – Multidimensional array - An array containing one or more arrays and values are accessed using multiple indices M.S. Chahar
  • 42. Numeric Array • These arrays can store numbers, strings and any object but their index will be prepresented by numbers. By default array index starts from zero. M.S. Chahar
  • 43. • <html> • <body> • <?php • /* First method to create array. */ • $numbers = array( 1, 2, 3, 4, 5); • foreach( $numbers as $value ) • { • echo "Value is $value <br />"; • } • /* Second method to create array. */ • $numbers[0] = "one"; • $numbers[1] = "two"; • $numbers[2] = "three"; • $numbers[3] = "four"; • $numbers[4] = "five"; • foreach( $numbers as $value ) • { • echo "Value is $value <br />"; • } • ?> • </body> • </html> M.S. Chahar
  • 44. Associative Arrays • The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values. • Ex ass.php M.S. Chahar
  • 45. Multidimensional Arrays • A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index. M.S. Chahar
  • 46. PHP Strings • They are sequences of characters, like "PHP supports string operations". M.S. Chahar
  • 47. There are valid examples of string • $string_1 = "This is a string in double quotes"; • $string_2 = "This is a somewhat longer, singly quoted string"; • $string_39 = "This string has thirty-nine characters"; • $string_0 = ""; // a string with zero characters M.S. Chahar
  • 49. The escape-sequence replacements are: • n is replaced by the newline character • r is replaced by the carriage-return character • t is replaced by the tab character • $ is replaced by the dollar sign itself ($) • " is replaced by a single double-quote (") • is replaced by a single backslash () M.S. Chahar
  • 50. PHP File Inclusion • You can include the content of a PHP file into another PHP file before the server executes it. • There are two PHP functions which can be used to included one PHP file into another PHP file. • The include() Function • The require() Function M.S. Chahar
  • 51. • This is a strong point of PHP which helps in creating functions, headers, footers, or elements that can be reused on multiple pages. This will help developers to make it easy to change the layout of complete website with minimal effort. M.S. Chahar
  • 52. The include() Function • The include() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the include() function generates a warning but the script will continue execution. • you want to create a common menu for your website. Then create a file ex.php. M.S. Chahar
  • 53. The require() Function • The require() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the require() function generates a fatal error and halt the execution of the script. M.S. Chahar
  • 54. • So there is no difference in require() and include() except they handle error conditions. It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed. M.S. Chahar
  • 55. PHP Files & I/O • Following functions related to files: • Opening a file • Reading a file • Writing a file • Closing a file M.S. Chahar
  • 56. • Opening and Closing Files • PHP fopen() function is used to open a file. It requires two arguments stating first the file name and then mode in which to operate. M.S. Chahar
  • 57. Files modes can be specified as one of the six options in this table. M.S. Chahar Mode Purpose r Opens the file for reading only. Places the file pointer at the beginning of the file. r+ Opens the file for reading and writing. Places the file pointer at the beginning of the file. w Opens the file for writing only. Places the file pointer at the beginning of the file. and truncates the file to zero length. If files does not exist then it attemts to create a file. w+ Opens the file for reading and writing only. Places the file pointer at the beginning of the file. and truncates the file to zero length. If files does not exist then it attemts to create a file. a Opens the file for writing only. Places the file pointer at the end of the file. If files does not exist then it attemts to create a file. a+ Opens the file for reading and writing only. Places the file pointer at the end of the file. If files does not exist then it attemts to create a file.
  • 58. • If an attempt to open a file fails then fopen returns a value of false otherwise it returns a file pointer which is used for further reading or writing to that file. • After making a changes to the opened file it is important to close it with the fclose() function. The fclose() function requires a file pointer as its argument and then returns true when the closure succeeds or false if it fails. M.S. Chahar
  • 59. Reading a file • Once a file is opened using fopen() function it can be read with a function called fread(). This function requires two arguments. These must be the file pointer and the length of the file expressed in bytes. • The files's length can be found using the filesize() function which takes the file name as its argument and returns the size of the file expressed in bytes. M.S. Chahar
  • 60. • So here are the steps required to read a file with PHP. • Open a file using fopen() function. • Get the file's length using filesize() function. • Read the file's content using fread() function. • Close the file with fclose() function. • Example:File.php M.S. Chahar
  • 61. Writing a file • A new file can be written or text can be appended to an existing file using the PHP fwrite() function. This function requires two arguments specifying a file pointer and the string of data that is to be written. Optionally a third integer argument can be included to specify the length of the data to write. If the third argument is included, writing would will stop after the specified length has been reached. M.S. Chahar
  • 62. PHP Cookies • Cookies are text files stored on the client computer and they are kept of use tracking purpose. M.S. Chahar
  • 63. • There are three steps involved in identifying returning users: • Server script sends a set of cookies to the browser. For example name, age, or identification number etc. • Browser stores this information on local machine for future use. • When next time browser sends any request to web server then it sends those cookies information to the server and server uses that information to identify the user. M.S. Chahar