[go: up one dir, main page]

0% found this document useful (0 votes)
38 views24 pages

Lecture Silde 1-24

PHP

Uploaded by

suppershort135
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views24 pages

Lecture Silde 1-24

PHP

Uploaded by

suppershort135
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

PHP

Introduction to Server-Side Programming


Request to a Static Site

Server:
1. Homepage
lookup
2. Send as
HTTP
Response
HTTP Request: GETwww.xkcd.com

You (client) Web server

HTTP Response: web content (HTMLfile) IP: 72.26.203.99


Client-side code: HTML, CSS, JavaScript
Request to a Dynamic Site
 The server must respond dynamically if it needs to provide
different client-side code depending on the situation
 Date and time
 Specifics of the user’s request
 Database contents – forms and authentication
Server:
HTTP Request: GET www.facebook.com
1. Look up things that go on
user’s profile, such as
wall posts and friends 
caches, database
lookups
You (client) Web server
2. Generate client-side
HTTP Response: web content (HTML file) code containing these
things
Client-side code: HTML, CSS, JavaScript
3. Send as HTTP response
(dynamicallygeneratedbyserver)
PHP
Introduction and Basic Syntax
What is PHP?

 PHP = PHP: Hypertext Preprocessor


 Server-side scripting language that may be
embedded into HTML
 Allows web developers to create dynamic
content that interacts with databases.
 Ultimate goal is to get PHP files to generate client- side
code
 Must end up with HTML, CSS, JavaScript, other client-
side code!
Advantages of PHP
 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, ,
Oracle, Sybase, Informix, and Microsoft SQL Server.
 PHP is pleasingly fast 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, making n-tier development a possibility for the first time.
 PHP is forgiving: PHP language tries to be as forgiving as possible.
Characteristics of PHP
There are many features given by PHP. All Features discussed below one
by one.
 Familiarity
 Simplicity
 Efficiency
 Security
 Flexibility
 Open source
 Object Oriented
Characteristics of PHP
 Familiarity: If you are in programming background then you can easily
understand the PHP syntax. And you can write PHP script because of most of
PHP syntax inherited from other languages like C or Pascal.
 Simplicity: PHP provides a lot of pre-define functions to secure your data. It is
also compatible with many third-party applications, and PHP can easily
integrate with other.
 In PHP script there is no need to include libraries like c, special compilation
directives like Java, PHP engine starts execution from (<?) escape
sequence and end with a closing escape sequence (?>). In PHP script,
there is no need to write main function. And also you can work with PHP
without creating a class.
 Efficiency: PHP 4.0 introduced resource allocation mechanisms and more
pronounced support for object-oriented programming, in addition to session
management features. Eliminating unnecessary memory allocation.
Characteristics of PHP
 Security: Several trusted data encryption options are supported in
PHP’s predefined function set. You can use a lot of third-party
applications to secure our data, allowing for securing our application.
 Flexibility: You can say that PHP is a very flexible language because
of PHP is an embedded language you can embed PHP scripts with
HTML, JAVA SCRIPT, WML, XML, and many others. You can run
your PHP script any device like mobile Phone, tabs, laptops, PC.
 Open Source/ Free: PHP is an open source programming language
so you can download freely there is no need to buy a licence or
anything.
 Object Oriented: PHP has added some object-oriented
programming features, and Object Oriented programming became
possible with PHP 4.0.
Applications of PHP

 PHP performs system functions, i.e. from files on a system it


can create, open, read, write, and close them.
 PHP can handle forms, i.e. gather data from files, save data
to a file, through email you can send data, return data to the
user.
 You add, delete, modify elements within your database
through PHP.
 Access cookies variables and set cookies.
 Using PHP, you can restrict users to access some pages of
your website.
 It can encrypt data.
Side-by-side

PHP File: Output: resulting HTML


<html> <html>
<head>
<title> PHP
<head>
Introduction </title> <title> PHP
</head> Introduction </title>
<body> </head>
This is HTML! <br /> <body>
<?php
echo 'This is PHP!
This is HTML! <br />
<br />'; This is PHP! <br />
?> </body>
</body> </html>
</html>
A Simple Example
<html>
<head>
<title> PHP Introduction </title>
</head>
<body>
This is HTML! <br />
<?php
echo 'This is PHP! <br />'; // prints to screen
/*
Here's a longer
comment
that spans multiple
lines.
*/
?>
</body>  PHP tags: <?php and ?>
</html>
 The echo command
 Single line comment ( // )
 Multiple line comment (/* and */)
Viewing PHP files
 PHP files executed on the web server
 Therefore we cannot save them anywhere and
view them, as with HTML files
 Must save .php files in subdirectory of web server
 /var/www/ on many Linux configurations
 www directory of your user directory on Athena
 Make call to web server via domain name
(google.com), IP address (72.26.203.99), or localhost
if on your own computer
PHP
Syntax: Functions, Variables, Operators, and Strings
PHP Functions
 PHP Built-in Functions
 PHP has over 1000 built-in functions that can be called directly, from
within a script, to perform a specific task. Ex: abs() Returns positive value
 PHP User Defined Functions
 Besides the built-in PHP functions, it is possible to create your own
functions:
I. A function is a block of statements that can be used repeatedly in a
program.
II. A function will not execute automatically when a page loads.
III. A function will be executed by a call to the function.
Syntax: function functionName() Example: <?php
{ function writeMsg() {
code to be executed; echo "Hello world!";
} } writeMsg(); // call the function ?>
Variables
 Store values for future reference, use variable name
to refer to the value stored in it
$x = 42; // store the value 42 in $x
echo $x; // prints 42
echo $x+1; // prints 43, value of $x is still 42
$x = ‘hello!’ // type of $x can change

 PHP is a loosely-typed language


 Do not need to declare the type of a variable
 Type can change throughout the program
PHP Global Variables - Superglobals
 Some predefined variables in PHP are "superglobals", which means that
they are always accessible and you can access them from any function,
class or file without having to do anything special.
 The PHP superglobal variables are:

 $GLOBALS
 $_SERVER
 $_REQUEST
 $_POST
 $_GET
 $_FILES
 $_COOKIE
 $_SESSION
PHP Global Variables - Superglobals
 $GLOBALS: $GLOBALS is a PHP super global variable which is used
to access global variables from anywhere in the PHP script (also from
within functions or methods).
 PHP stores all global variables in an array called $GLOBALS[index]. The
index holds the name of the variable.
 Example:
<?php
$x = 75;
$y = 25; Output: 100
function addition() {
$GLOBALS['z'] = $GLOBALS['x']
+ $GLOBALS['y']; }
addition();
echo $z;
?>
PHP Global Variables - Superglobals
 $_SERVER: $_SERVER is a PHP super global variable which holds
information about headers, paths, and script locations.
 Some Example:
<?php
echo $_SERVER['PHP_SELF'];
Output:
echo "<br>"; /Practice/practice.php
echo $_SERVER['SERVER_NAME']; localhost
echo "<br>"; C:/xampp/htdocs/Practice/practice.php
echo $_SERVER['SCRIPT_FILENAME']; GET
echo "<br>";
echo $_SERVER['REQUEST_METHOD']; ?>
 $_SERVER['PHP_SELF'] : Returns the filename of the currently executing script.
 $_SERVER['SERVER_NAME'] : Returns the name of the host server.
 $_SERVER['SCRIPT_FILENAME']: Returns the absolute pathname of the currently executing
script.
 $_SERVER['REQUEST_METHOD']: Returns the request method used to access the page.
PHP Global Variables - Superglobals
 $_REQUEST: $_REQUEST is a PHP super global variable which is
used to collect data after submitting an HTML form. Example
<html> <body>
<form method="post" action= " " >
Name: <input type="text" name="fname">
Form:
<input type="Submit"> Name: ICE Submit
</form> Output: ICE
<?php
if ($_SERVER["REQUEST_METHOD"] == When a user submits the data by
"POST") { clicking on "Submit", the form data is
// collect value of input field sent to the file specified in the action
$name = $_REQUEST['fname']; attribute of the <form> tag. If you
if (empty($name)) { wish to use another PHP file to
echo "Name is empty"; } process form data, replace that with
else { echo $name; } } ?> the filename of your choice.
</body> </html>
Operators
 Arithmetic operators
 +, -, *, /, % (modulus – remainder after division)
 Logical AND (&&), OR (||), NOT (!)
 Assignment operators
 Shorthand for assignment operators:
 $x += $y equivalent to $x = $x + $y
 Also works with subtraction, multiplication, division, modulus, and string
concatenation
 String operators:
 Dot( .) operator, $x . $y Concatenation of $x and $y
 Dot equal( .=) operator, $x . =$y Appends of $x and $y
Example: $a = ‘hello’; $b = ‘world !’;
// prints hello world !
 Use of dot (.) Operator, echo $a.$b;
 Use of dot equal(.=) Operator, $a.=$b echo $a;
== versus ===
 Two “equality” operators
 == tests for “equality” in value but not
necessarily type
 === tests for “identity” in value and type
 == ignores the distinction between:
 Integers, floating point numbers, and strings
containing the same numerical value
 Nonzero numbers and boolean TRUE
 Zero and boolean FALSE
 Empty string, the string ‘0’ and boolean FALSE
 Any other non-empty string and boolean TRUE
Strings
 A sequence of characters
 Single and double quotes:
 Suppose $str = 42;
 echo ‘With single quotes, str is $str’;
 output: With single quotes, str is $str
 echo “With double quotes, str is $str”;
 output: With double quotes, str is 42
 String Function
 Length: strlen()
 Position of substring: strpos()
 Count Words in a String: str_word_count()
 Reverse a String: strrev()
 Replace Text Within a String: str_replace()
StringFunction Example

 strlen() - Return the Length of a String


 echo strlen("Hello world!"); // outputs 12
 strrev() - Reverse a String
 echo strrev("Hello world!"); / / outputs !dlrow olleH
 str_word_count() - Count Words in a String
 echo str_word_count("Hello world!"); // outputs 2
 strpos() - Search For a Text Within a String
 echo strpos("Hello world!", "world"); // outputs 6
(If a match is found, the function returns the character position of the first match. If no match is found, it will return
FALSE.)

 str_replace() - Replace Text Within a String


 echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello Dolly!

You might also like