SlideShare a Scribd company logo
Chapter One:
Server Side
Scripting Basics
11/27/2019
BantamlakDejene,Information
Technology
1
Introduction to Server-Side Scripting
Today’s Web users expect exciting pages that are updated
frequently and provide a customized experience. For them, Web
sites are more like communities, to which they’ll return time
and again. At the same time, Web-site administrators want sites
that are easier to update and maintain, understanding that’s the
only reasonable way to keep up with visitors’ expectations. For
these reasons and more, PHP and MySQL have become the de
facto standards for creating dynamic, database driven Web sites.
11/27/2019
BantamlakDejene,Information
Technology
2
Cont.….
Dynamic Web sites are flexible and potent creatures, more accurately
described as applications than merely sites. Dynamic Web sites
Respond to different parameters (for example, the time of day or the
version of the visitor’s Web browser)
Have a “memory,” allowing for user registration and login, e-commerce,
and similar processes
Almost always integrate HTML forms, allowing visitors to perform
searches, provide feedback, and so forth
Often have interfaces where administrators can manage the site’s content
Are easier to maintain, upgrade, and build upon than statically made sites.
11/27/2019
BantamlakDejene,Information
Technology
3
Cont.….
There are many technologies available for creating dynamic
Web sites. The most common are ASP.NET (Active Server
Pages, a Microsoft construct), JSP (Java Server Pages),
ColdFusion, Ruby on Rails (a Web development framework
for the Ruby programming language), and PHP. Dynamic
Web sites don’t always rely on a database, but more and
more of them do, particularly as excellent database
applications like MySQL are available at little to no cost.
11/27/2019
BantamlakDejene,Information
Technology
4
Server-Side Scripting Languages
PHP originally stood for “Personal Home Page” as it was created in 1994 by Rasmus
Lerdorf to track the visitors to his online résumé. As its usefulness and capabilities
grew, it came to mean “PHP: Hypertext Preprocessor. According to the official PHP
Web site, found at www.php.net A, PHP is a “widely used general-purpose scripting
language that is especially suited for Web development and can be embedded into
HTML.” It’s a long but descriptive definition.
Server side refers to the fact that everything PHP does occurs on the server. A Web
server application, like Apache or Microsoft’s IIS, is required and all PHP scripts
must be accessed through a URL. Its cross-platform nature means that PHP runs on
most operating systems, including Windows, UNIX, and Macintosh. More important,
the PHP scripts written on one server will normally work on another with little or no
modification.
11/27/2019
BantamlakDejene,Information
Technology
5
Cont.….
MySQL (www.mysql.com) is the world’s most popular open-source database. In
fact, today MySQL is a viable competitor to the pricey goliaths such as Oracle and
Microsoft’s SQL Server. Like PHP, MySQL offers excellent performance,
portability, and reliability, with a moderate learning curve and little to no cost.
MySQL is an RDBMS. A database, in the simplest terms, is a collection of data, be
it text, numbers, or binary files, stored and kept organized by the DBMS.
MySQL is an open-source application, like PHP, meaning that it is free to use or
even modify. There are occasions in which you should pay for a MySQL license,
especially if you are making money from the sales or incorporation of the MySQL
product. The MySQL software consists of several pieces, including the MySQL
server, the MySQL client, and numerous utilities for maintenance and other
purposes. PHP always had good support for MySQL, and that is even truer in the
most recent versions of the language.
11/27/2019
BantamlakDejene,Information
Technology
6
Use Basic Syntax
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page Title</title></head>
<body><!-- Example 1 - template.html -->
</body></html>
11/27/2019
BantamlakDejene,Information
Technology
7
Cont.….
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Basic PHP Page</title></head>
<body><!—Example 2 - first.php -->
<p>This is standard HTML.</p><?php?></body></html>
11/27/2019
BantamlakDejene,Information
Technology
8
Send Data to Web Browser
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"
/>
<title>Using Echo</title></head>
<body><!-- Example 3 - second.php -->
<p>This is standard HTML.</p>
<?php
echo <p>This is generated by using PHP!</p>
?></body></html>
11/27/2019
BantamlakDejene,Information
Technology
9
Write Comments
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Comments</title></head>
<body><?php
# Example 4 - comments.php
# Created March 16, 2011
# Created by Larry E. Ullman
# This script does nothing much.
echo '<p>This is a line of text.<br />
This is another line of text.</p>';
/*
echo 'This line will not be executed.';
*/
echo "<p>Now I'm done.</p>";
// End of PHP code.
?></body></html>
11/27/2019
BantamlakDejene,Information
Technology
10
Utilize Variables
Regardless of what type you are creating, all variable names in
PHP follow certain syntactical rules:
A variable’s name must start with a dollar sign ($), for example, $name.
The variable’s name can contain a combination of letters, numbers, and
the underscore, for example, $my_report1.
The first character after the dollar sign must be either a letter or an
underscore (it cannot be a number).
Variable names in PHP are case sensitive! This is a very important rule. It
means that $name and $Name are entirely different variables.
11/27/2019
BantamlakDejene,Information
Technology
11
Cont.….
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN“
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Predefined Variables</title></head>
<body><?php # Example 5 - predefined.php
// Create a shorthand version of the variable names:
$file = $_SERVER['SCRIPT_FILENAME'];
$user = $_SERVER['HTTP_USER_AGENT'];
$server = $_SERVER['SERVER_SOFTWARE'];
// Print the name of this script:
echo "<p>You are running the file:<br /><b>$file</b>.</p>n";
// Print the user's information:
echo "<p>You are viewing this page using:<br /><b>$user</b></p>n";
// Print the server's information:
echo "<p>This server is running: <br /><b>$server</b>.</p>n";
?></body></html>
11/27/2019
BantamlakDejene,Information
Technology
12
Manipulate Strings
A string is merely a quoted chunk of characters: letters, numbers, spaces, punctuation, and so
forth. These are all strings:
-> ‘Tobias’ -> “In watermelon sugar” -> ‘100’ -> ‘August 2, 2011
Make a string variable, assign a string value to a valid variable name:
$first_name = 'Tobias'; $today = 'August 2, 2011';
When creating strings, you can use either single or double quotation marks to encapsulate the characters,
just as you would when printing text. Likewise, you must use the same type of quotation mark for the
beginning and the end of the string. If that same mark appears within the string, it must be escaped: $var =
"Define "platitude", please."; Or you can also use the other quotation mark type: $var = 'Define
"platitude", please.';
To print out the value of a string, use either
echo or print:
echo $first_name
To print the value of string within a context, you must use double quotation marks:
echo "Hello, $first_name";
11/27/2019
BantamlakDejene,Information
Technology
13
Cont.….
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Strings</title></head>
<body><?php # Example6 - strings.php
// Create the variables:
$first_name = 'ሐዲስ';
$last_name = 'አለማሁ';
$book = 'ፍቅር እስከ መቃብር';
// Print the values:
echo "<p>The book <em>$book</em> was written by $first_name $last_name.</p>";
?></body></html>
11/27/2019
BantamlakDejene,Information
Technology
14
Cont.….
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Strings</title></head>
<body><?php # string concatenation, length and word counting =>
string_concat.php
// Create the variables:
$favoriteAnimal = 'cat';
$myArray ['age']= '28';
$about = "My age";
$len = strlen($about);
$word = str_word_count("My favorite animals are {$favoriteAnimal}s");
// Print the values:
echo "My favorite animals are {$favoriteAnimal}s => There are {$word} words in
this sentence</br>"; // using curly bracket
echo $about . " is " . $myArray["age"] . "</br>"; // using . operator
echo "The length of " . $about . " is " . $len;
?></body></html>
11/27/2019
BantamlakDejene,Information
Technology
15
Cont.….
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Strings</title></head>
<body><?php # string search and count => string_search.php
// Create the variables:
$su = 'Welcome to Samara University. Welcome to Department of Information Technology <br>
This is the Department of Information Technology from College of Engineering and Technology in Samara University';
$search = strstr($su, "Engineering");// searches the string from the sentence
$searchF = strpos($su, "University");// locates the string starting from beginning
$searchE = strrpos($su, "Samara");// locates the string starting from end
$count = substr_count($su, "Technology");//counts the repetition of the string in the text
$countS = substr_count($su, "to", 9);//counts the repetition of the string in the text starting form the given index
$countF = substr_count($su, "of", 45, 100);//counts the repetition of the string in the text between the indexes
// Print the values:
echo $su . "</br>";
echo "</br>Engineering is found at => ". $search . "</br>";
echo "University is found at => " . $searchF . " => from the beginning </br>";
echo "Samara is found at => " . $searchE . " => from the end </br>";
echo "Technology is repeated => " . $count . " => times in the text </br>";
echo "to is repeated => " . $countS . " => times in the text starting from index 9 </br>";
echo "of is repeated => " . $countF . " => times in the text between index 45 and 100 </br>";
?></body></html>
11/27/2019
BantamlakDejene,Information
Technology
16
Cont.….
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Strings</title></head>
<body><?php # string manipulation => string_manipulation.php
// Create the variables:
$myString = "It was the best of times, it was the worst of times";
$string = "Here's a little string";
$str = "Information Technology";
echo $myString . "</br></br>";
echo str_replace("times", "bananas", $myString, $num ). "</br>";//replaces all and count it
echo "The text was replaced $num times. </br></br>";
echo substr_replace($myString, "bananas", 11 ). "</br></br>";//replaces from the given index
echo substr_replace($myString, "bananas", 19, 5). "</br></br>";//replaces from the given index to the given number of characters
echo substr_replace($myString, "really", 3, 0) . "</br></br>";
echo strtr($myString, " '", "+-"). "</br></br>";
echo strtolower($str) . "</br></br>";
echo ucfirst($str) . "</br></br>";
echo lcfirst($str) . "</br></br>";
?></body></html>
11/27/2019
BantamlakDejene,Information
Technology
17
Cont.….
<!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" xml:lang="en" lang="en">
<head><title>Type Specifiers in Action</title >
<link rel="stylesheet" type="text/css" href="common.css"></head>
<body><h1>Type Specifiers in Action</h1>
<?php
$myNumber = 123.45;
printf("Binary: %b </br>", $myNumber);
printf("Character: %c </br>", $myNumber);
printf("Decimal: %d </br>", $myNumber);
printf("Scientific: %e </br>", $myNumber);
printf("Float: %f </br>", $myNumber );
printf("Octal: %o </br>", $myNumber );
printf("String: %s </br>", $myNumber );
printf("Hex (lower case): %x </br>", $myNumber );
printf("Hex (upper case): %X </br>", $myNumber );
?></body></html>
11/27/2019
BantamlakDejene,Information
Technology
18
Manipulate Numbers Work with Constants
PHP has both integer and floating-point (decimal) number types. These two types can be
classified under the generic title numbers without losing any valuable distinction (for the most
part). Valid number-type variables in PHP can be anything like
-> 8 -> 3.14 -> 10980843985 -> -4.2398508 -> 4.4e2
Notice that these values are never quoted—quoted numbers are strings with neither numeric
values— nor do they include commas to indicate thousands. Also, a number is assumed to be
positive unless it is preceded by the minus sign (-). Along with the standard arithmetic
operators
you can use on numbers (Table 1.1), there are dozens of functions built into PHP. Two
common ones are round( ) and number_format( ). The former rounds a decimal to the
nearest integer:
$n = 3.14;
$n = round ($n); // 3
11/27/2019
BantamlakDejene,Information
Technology
19
Cont.….
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Numbers</title></head><body>
<?php # Script 1.8 - numbers.php
// Set the variables:
$quantity = 30; // Buying 30 widgets.
$price = 119.95;
$taxrate = .05; // 5% sales tax.
// Calculate the total:
$total = $quantity * $price;
$total = $total + ($total * $taxrate);
// Calculate and add the tax.
// Format the total:
$total = number_format ($total, 2);
// Print the results:
echo "<p/>You are purchasing </br>" . $quantity . "</br> widget(s) at a cost of <br>$" . $price .
"</br> each. With tax, the total comes to <b>$" . $total . ".</br></p>";
?></body></html>
11/27/2019
BantamlakDejene,Information
Technology
20
Cont.….
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Constants</title></head><body>
<?php # Script 1.9 - constants.php
// Set today's date as a constant:
define ('TODAY', 'March 16, 2011');
// Print a message, using predefined constants and the TODAY constant:
echo '<p>Today is ' . TODAY . '.</br> This server is running version <b>' . PHP_VERSION .
'</b>
of PHP on the <b>' . PHP_OS . '</b> operating system.</p>';
?></body></html>
11/27/2019
BantamlakDejene,Information
Technology
21
THANK YOU
11/27/2019
BantamlakDejene,Information
Technology
22

More Related Content

PPTX
Php ch-2_html_forms_and_server_side_scripting
PDF
Intro to Dynamic Web Pages
PDF
POX to HATEOAS: Our Company's Journey Building a Hypermedia API
PPT
Tech talk php_cms
PPTX
What should or not be programmed on the web
PPT
Xhtml 2010
PDF
Unit 1 php_basics
PPTX
Php intro
Php ch-2_html_forms_and_server_side_scripting
Intro to Dynamic Web Pages
POX to HATEOAS: Our Company's Journey Building a Hypermedia API
Tech talk php_cms
What should or not be programmed on the web
Xhtml 2010
Unit 1 php_basics
Php intro

What's hot (19)

PPTX
Web page concept final ppt
PPTX
PPTX
Drupalcamp Atlanta 2010 Internationalization Presentation
PPT
PHP Project PPT
PPTX
Dynamic Web Programming
PPTX
Presentation php
PPTX
Web Designing Training Institute and Coaching center In Ambala Cantt
PDF
Basic php
PDF
PPT
4 internet programming
PDF
The Same-Origin Policy
PPT
Web Development From the Ground Up, a Series for Novice ...
PPTX
0 csc 3311 slide internet programming
PPTX
PHP Summer Training Presentation
PPTX
Php Unit 1
PDF
Php tutorial from_beginner_to_master
PDF
Understanding and Developing Web Services - For DBAs and Developers (whitepaper)
PPTX
Php presentation
Web page concept final ppt
Drupalcamp Atlanta 2010 Internationalization Presentation
PHP Project PPT
Dynamic Web Programming
Presentation php
Web Designing Training Institute and Coaching center In Ambala Cantt
Basic php
4 internet programming
The Same-Origin Policy
Web Development From the Ground Up, a Series for Novice ...
0 csc 3311 slide internet programming
PHP Summer Training Presentation
Php Unit 1
Php tutorial from_beginner_to_master
Understanding and Developing Web Services - For DBAs and Developers (whitepaper)
Php presentation
Ad

Similar to Php ch-1_server_side_scripting_basics (20)

PDF
PHP Basics
PPT
PHP and MySQL
PPT
Php Tutorial
PPT
Osp ii presentation
PDF
phptutorial
PDF
phptutorial
PDF
PHP Basics Ebook
PPT
Fundamentals of web_design_v2
PPTX
1 Introduction to Drupal Web Development
PDF
Leksion 1 hyrje ne xhtml
PPT
Php intro
PPT
Php intro
PPT
Php intro
PDF
Making Of PHP Based Web Application
PPT
Introduction to web and php mysql
PPS
Web technology html5 php_mysql
PDF
Web Programming introduction
PPT
PPT
Introduction To Website Development
PPTX
Introduction to php
PHP Basics
PHP and MySQL
Php Tutorial
Osp ii presentation
phptutorial
phptutorial
PHP Basics Ebook
Fundamentals of web_design_v2
1 Introduction to Drupal Web Development
Leksion 1 hyrje ne xhtml
Php intro
Php intro
Php intro
Making Of PHP Based Web Application
Introduction to web and php mysql
Web technology html5 php_mysql
Web Programming introduction
Introduction To Website Development
Introduction to php
Ad

More from bantamlak dejene (9)

PPTX
object oriented fundamentals in vb.net
PPTX
introduction to .net
PPTX
introduction to vb.net
PPTX
html forms and server side scripting
PPTX
server side scripting basics
PPTX
server side scripting basics by Bantamlak Dejene
PPTX
Vb ch 3-object-oriented_fundamentals_in_vb.net
PPTX
Vb ch 2-introduction_to_.net
PPTX
Vb ch 1-introduction
object oriented fundamentals in vb.net
introduction to .net
introduction to vb.net
html forms and server side scripting
server side scripting basics
server side scripting basics by Bantamlak Dejene
Vb ch 3-object-oriented_fundamentals_in_vb.net
Vb ch 2-introduction_to_.net
Vb ch 1-introduction

Recently uploaded (20)

PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Empathic Computing: Creating Shared Understanding
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Cloud computing and distributed systems.
PPTX
sap open course for s4hana steps from ECC to s4
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Approach and Philosophy of On baking technology
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Machine learning based COVID-19 study performance prediction
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Encapsulation theory and applications.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Dropbox Q2 2025 Financial Results & Investor Presentation
NewMind AI Weekly Chronicles - August'25-Week II
Network Security Unit 5.pdf for BCA BBA.
Empathic Computing: Creating Shared Understanding
Spectral efficient network and resource selection model in 5G networks
MIND Revenue Release Quarter 2 2025 Press Release
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Cloud computing and distributed systems.
sap open course for s4hana steps from ECC to s4
A comparative analysis of optical character recognition models for extracting...
Approach and Philosophy of On baking technology
The AUB Centre for AI in Media Proposal.docx
Programs and apps: productivity, graphics, security and other tools
Per capita expenditure prediction using model stacking based on satellite ima...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Machine learning based COVID-19 study performance prediction
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Encapsulation theory and applications.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Php ch-1_server_side_scripting_basics

  • 1. Chapter One: Server Side Scripting Basics 11/27/2019 BantamlakDejene,Information Technology 1
  • 2. Introduction to Server-Side Scripting Today’s Web users expect exciting pages that are updated frequently and provide a customized experience. For them, Web sites are more like communities, to which they’ll return time and again. At the same time, Web-site administrators want sites that are easier to update and maintain, understanding that’s the only reasonable way to keep up with visitors’ expectations. For these reasons and more, PHP and MySQL have become the de facto standards for creating dynamic, database driven Web sites. 11/27/2019 BantamlakDejene,Information Technology 2
  • 3. Cont.…. Dynamic Web sites are flexible and potent creatures, more accurately described as applications than merely sites. Dynamic Web sites Respond to different parameters (for example, the time of day or the version of the visitor’s Web browser) Have a “memory,” allowing for user registration and login, e-commerce, and similar processes Almost always integrate HTML forms, allowing visitors to perform searches, provide feedback, and so forth Often have interfaces where administrators can manage the site’s content Are easier to maintain, upgrade, and build upon than statically made sites. 11/27/2019 BantamlakDejene,Information Technology 3
  • 4. Cont.…. There are many technologies available for creating dynamic Web sites. The most common are ASP.NET (Active Server Pages, a Microsoft construct), JSP (Java Server Pages), ColdFusion, Ruby on Rails (a Web development framework for the Ruby programming language), and PHP. Dynamic Web sites don’t always rely on a database, but more and more of them do, particularly as excellent database applications like MySQL are available at little to no cost. 11/27/2019 BantamlakDejene,Information Technology 4
  • 5. Server-Side Scripting Languages PHP originally stood for “Personal Home Page” as it was created in 1994 by Rasmus Lerdorf to track the visitors to his online résumé. As its usefulness and capabilities grew, it came to mean “PHP: Hypertext Preprocessor. According to the official PHP Web site, found at www.php.net A, PHP is a “widely used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.” It’s a long but descriptive definition. Server side refers to the fact that everything PHP does occurs on the server. A Web server application, like Apache or Microsoft’s IIS, is required and all PHP scripts must be accessed through a URL. Its cross-platform nature means that PHP runs on most operating systems, including Windows, UNIX, and Macintosh. More important, the PHP scripts written on one server will normally work on another with little or no modification. 11/27/2019 BantamlakDejene,Information Technology 5
  • 6. Cont.…. MySQL (www.mysql.com) is the world’s most popular open-source database. In fact, today MySQL is a viable competitor to the pricey goliaths such as Oracle and Microsoft’s SQL Server. Like PHP, MySQL offers excellent performance, portability, and reliability, with a moderate learning curve and little to no cost. MySQL is an RDBMS. A database, in the simplest terms, is a collection of data, be it text, numbers, or binary files, stored and kept organized by the DBMS. MySQL is an open-source application, like PHP, meaning that it is free to use or even modify. There are occasions in which you should pay for a MySQL license, especially if you are making money from the sales or incorporation of the MySQL product. The MySQL software consists of several pieces, including the MySQL server, the MySQL client, and numerous utilities for maintenance and other purposes. PHP always had good support for MySQL, and that is even truer in the most recent versions of the language. 11/27/2019 BantamlakDejene,Information Technology 6
  • 7. Use Basic Syntax <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Page Title</title></head> <body><!-- Example 1 - template.html --> </body></html> 11/27/2019 BantamlakDejene,Information Technology 7
  • 8. Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Basic PHP Page</title></head> <body><!—Example 2 - first.php --> <p>This is standard HTML.</p><?php?></body></html> 11/27/2019 BantamlakDejene,Information Technology 8
  • 9. Send Data to Web Browser <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Using Echo</title></head> <body><!-- Example 3 - second.php --> <p>This is standard HTML.</p> <?php echo <p>This is generated by using PHP!</p> ?></body></html> 11/27/2019 BantamlakDejene,Information Technology 9
  • 10. Write Comments <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Comments</title></head> <body><?php # Example 4 - comments.php # Created March 16, 2011 # Created by Larry E. Ullman # This script does nothing much. echo '<p>This is a line of text.<br /> This is another line of text.</p>'; /* echo 'This line will not be executed.'; */ echo "<p>Now I'm done.</p>"; // End of PHP code. ?></body></html> 11/27/2019 BantamlakDejene,Information Technology 10
  • 11. Utilize Variables Regardless of what type you are creating, all variable names in PHP follow certain syntactical rules: A variable’s name must start with a dollar sign ($), for example, $name. The variable’s name can contain a combination of letters, numbers, and the underscore, for example, $my_report1. The first character after the dollar sign must be either a letter or an underscore (it cannot be a number). Variable names in PHP are case sensitive! This is a very important rule. It means that $name and $Name are entirely different variables. 11/27/2019 BantamlakDejene,Information Technology 11
  • 12. Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN“ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Predefined Variables</title></head> <body><?php # Example 5 - predefined.php // Create a shorthand version of the variable names: $file = $_SERVER['SCRIPT_FILENAME']; $user = $_SERVER['HTTP_USER_AGENT']; $server = $_SERVER['SERVER_SOFTWARE']; // Print the name of this script: echo "<p>You are running the file:<br /><b>$file</b>.</p>n"; // Print the user's information: echo "<p>You are viewing this page using:<br /><b>$user</b></p>n"; // Print the server's information: echo "<p>This server is running: <br /><b>$server</b>.</p>n"; ?></body></html> 11/27/2019 BantamlakDejene,Information Technology 12
  • 13. Manipulate Strings A string is merely a quoted chunk of characters: letters, numbers, spaces, punctuation, and so forth. These are all strings: -> ‘Tobias’ -> “In watermelon sugar” -> ‘100’ -> ‘August 2, 2011 Make a string variable, assign a string value to a valid variable name: $first_name = 'Tobias'; $today = 'August 2, 2011'; When creating strings, you can use either single or double quotation marks to encapsulate the characters, just as you would when printing text. Likewise, you must use the same type of quotation mark for the beginning and the end of the string. If that same mark appears within the string, it must be escaped: $var = "Define "platitude", please."; Or you can also use the other quotation mark type: $var = 'Define "platitude", please.'; To print out the value of a string, use either echo or print: echo $first_name To print the value of string within a context, you must use double quotation marks: echo "Hello, $first_name"; 11/27/2019 BantamlakDejene,Information Technology 13
  • 14. Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Strings</title></head> <body><?php # Example6 - strings.php // Create the variables: $first_name = 'ሐዲስ'; $last_name = 'አለማሁ'; $book = 'ፍቅር እስከ መቃብር'; // Print the values: echo "<p>The book <em>$book</em> was written by $first_name $last_name.</p>"; ?></body></html> 11/27/2019 BantamlakDejene,Information Technology 14
  • 15. Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Strings</title></head> <body><?php # string concatenation, length and word counting => string_concat.php // Create the variables: $favoriteAnimal = 'cat'; $myArray ['age']= '28'; $about = "My age"; $len = strlen($about); $word = str_word_count("My favorite animals are {$favoriteAnimal}s"); // Print the values: echo "My favorite animals are {$favoriteAnimal}s => There are {$word} words in this sentence</br>"; // using curly bracket echo $about . " is " . $myArray["age"] . "</br>"; // using . operator echo "The length of " . $about . " is " . $len; ?></body></html> 11/27/2019 BantamlakDejene,Information Technology 15
  • 16. Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Strings</title></head> <body><?php # string search and count => string_search.php // Create the variables: $su = 'Welcome to Samara University. Welcome to Department of Information Technology <br> This is the Department of Information Technology from College of Engineering and Technology in Samara University'; $search = strstr($su, "Engineering");// searches the string from the sentence $searchF = strpos($su, "University");// locates the string starting from beginning $searchE = strrpos($su, "Samara");// locates the string starting from end $count = substr_count($su, "Technology");//counts the repetition of the string in the text $countS = substr_count($su, "to", 9);//counts the repetition of the string in the text starting form the given index $countF = substr_count($su, "of", 45, 100);//counts the repetition of the string in the text between the indexes // Print the values: echo $su . "</br>"; echo "</br>Engineering is found at => ". $search . "</br>"; echo "University is found at => " . $searchF . " => from the beginning </br>"; echo "Samara is found at => " . $searchE . " => from the end </br>"; echo "Technology is repeated => " . $count . " => times in the text </br>"; echo "to is repeated => " . $countS . " => times in the text starting from index 9 </br>"; echo "of is repeated => " . $countF . " => times in the text between index 45 and 100 </br>"; ?></body></html> 11/27/2019 BantamlakDejene,Information Technology 16
  • 17. Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Strings</title></head> <body><?php # string manipulation => string_manipulation.php // Create the variables: $myString = "It was the best of times, it was the worst of times"; $string = "Here's a little string"; $str = "Information Technology"; echo $myString . "</br></br>"; echo str_replace("times", "bananas", $myString, $num ). "</br>";//replaces all and count it echo "The text was replaced $num times. </br></br>"; echo substr_replace($myString, "bananas", 11 ). "</br></br>";//replaces from the given index echo substr_replace($myString, "bananas", 19, 5). "</br></br>";//replaces from the given index to the given number of characters echo substr_replace($myString, "really", 3, 0) . "</br></br>"; echo strtr($myString, " '", "+-"). "</br></br>"; echo strtolower($str) . "</br></br>"; echo ucfirst($str) . "</br></br>"; echo lcfirst($str) . "</br></br>"; ?></body></html> 11/27/2019 BantamlakDejene,Information Technology 17
  • 18. Cont.…. <!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" xml:lang="en" lang="en"> <head><title>Type Specifiers in Action</title > <link rel="stylesheet" type="text/css" href="common.css"></head> <body><h1>Type Specifiers in Action</h1> <?php $myNumber = 123.45; printf("Binary: %b </br>", $myNumber); printf("Character: %c </br>", $myNumber); printf("Decimal: %d </br>", $myNumber); printf("Scientific: %e </br>", $myNumber); printf("Float: %f </br>", $myNumber ); printf("Octal: %o </br>", $myNumber ); printf("String: %s </br>", $myNumber ); printf("Hex (lower case): %x </br>", $myNumber ); printf("Hex (upper case): %X </br>", $myNumber ); ?></body></html> 11/27/2019 BantamlakDejene,Information Technology 18
  • 19. Manipulate Numbers Work with Constants PHP has both integer and floating-point (decimal) number types. These two types can be classified under the generic title numbers without losing any valuable distinction (for the most part). Valid number-type variables in PHP can be anything like -> 8 -> 3.14 -> 10980843985 -> -4.2398508 -> 4.4e2 Notice that these values are never quoted—quoted numbers are strings with neither numeric values— nor do they include commas to indicate thousands. Also, a number is assumed to be positive unless it is preceded by the minus sign (-). Along with the standard arithmetic operators you can use on numbers (Table 1.1), there are dozens of functions built into PHP. Two common ones are round( ) and number_format( ). The former rounds a decimal to the nearest integer: $n = 3.14; $n = round ($n); // 3 11/27/2019 BantamlakDejene,Information Technology 19
  • 20. Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Numbers</title></head><body> <?php # Script 1.8 - numbers.php // Set the variables: $quantity = 30; // Buying 30 widgets. $price = 119.95; $taxrate = .05; // 5% sales tax. // Calculate the total: $total = $quantity * $price; $total = $total + ($total * $taxrate); // Calculate and add the tax. // Format the total: $total = number_format ($total, 2); // Print the results: echo "<p/>You are purchasing </br>" . $quantity . "</br> widget(s) at a cost of <br>$" . $price . "</br> each. With tax, the total comes to <b>$" . $total . ".</br></p>"; ?></body></html> 11/27/2019 BantamlakDejene,Information Technology 20
  • 21. Cont.…. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Constants</title></head><body> <?php # Script 1.9 - constants.php // Set today's date as a constant: define ('TODAY', 'March 16, 2011'); // Print a message, using predefined constants and the TODAY constant: echo '<p>Today is ' . TODAY . '.</br> This server is running version <b>' . PHP_VERSION . '</b> of PHP on the <b>' . PHP_OS . '</b> operating system.</p>'; ?></body></html> 11/27/2019 BantamlakDejene,Information Technology 21