0% found this document useful (0 votes)
30 views27 pages

Advanced Internet Programming Ch1

Uploaded by

genawmigo20
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
30 views27 pages

Advanced Internet Programming Ch1

Uploaded by

genawmigo20
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 27

Advanced Internet Programming

ITec3092

Chala Simon
CHAPTER ONE:
SERVER-SIDE SCRIPTING
BASICS
Introduction to server-side scripting
Is a technique used in web development which involves employing scripts on a web server which
produce a response customized for each user's (client's) request to the website.

Is the general name for the kinds of programs


which are run on the Server

What can Server Scripts Do?

• Dynamically edit, change or add any content to a Web page

• Respond to user queries or data submitted from HTML forms

• Access any data or databases and return the results to a browser

• Customize a Web page to make it more useful for individual users

• Provide security since your server code cannot be viewed from a browser
Server-side Languages

o PHP
o ASP
o Ruby
o Python
o Java …
What is PHP?
PHP == ‘PHP: Hypertext Preprocessor’ –recursive acronym

Open-source, server-side scripting language

Used to generate dynamic web-pages

PHP is an interpreted language, i.e. there is no need for compilation.

PHP is simple and easy to learn language.

PHP scripts reside between reserved PHP tags

• This allows the programmer to embed PHP scripts within HTML

7
PHP Features

There are given many features of PHP:

• Performance: Script written in PHP executes much faster then those scripts written in other languages such
as JSP & ASP.

• Open Source Software: PHP source code is free available on the web. You can develop all the versions of
PHP according to your requirement without paying any cost. All its components are free to download and use.

• Platform Independent: PHP are available for WINDOWS, MAC, LINUX & UNIX operating system. A PHP
application developed in one OS can be easily executed in other OS also.

• Compatibility: PHP is compatible with almost all local servers used today like Apache, IIS etc.

• Embedded: PHP code can be easily embedded within HTML tags and script.
Common uses 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, and 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.


11/17/2024 10
Install PHP

• To install PHP, AMP (Apache, MySQL, PHP) software stack is suggested. It is available for
all operating systems.
• WAMP for Windows

• LAMP for Linux

• MAMP for Mac

• SAMP for Solaris

• FAMP for FreeBSD

• XAMPP (Cross, Apache, MySQL, PHP, Perl) for Cross Platform: It includes some other
components too such as FileZilla, OpenSSL, Webalizer, Mercury Mail etc.
Web servers- xampp control panel

12
What does PHP code look like?

• Structurally similar to C/C++

• Supports procedural and object-oriented paradigm (to some degree)

• All PHP statements end with a semi-colon

• Each PHP script must be enclosed in the reserved PHP tag

<?php

?>
Comments in PHP

Standard C, C++, and shell comment symbols

// C++ and Java-style comment

# Shell-style comments

/* C-style comments
These can span multiple lines */
Variables in PHP
• All variables in PHP start with a $ sign, followed by the name of the variable.

• A variable name must start with a letter or the underscore character _.

• A variable name cannot start with a number.

• A variable name in PHP can only contain alpha-numeric characters and underscores

(A-z, 0-9, and _).

• A variable name cannot contain spaces.

• Case-sensitive ($test != $Test != $tEst)

• Variables do not need to be declared before assignment.


… cont’d
• The value of a variable is the value of its most recent assignment.

• Static Variable – to hold the values of variables inside the function.

• Global and locally-scoped variables

• Global variables can be used anywhere

• Uses global keyword

• Local variables restricted to a function or class

• Certain variable names reserved by PHP

• Form variables ($_POST, $_GET)

• Server variables ($_SERVER)

• Etc.
Variable usage

<?php
$abc = 'Welcome'; //valid
$Abc = 'W3resource.com'; //valid
$9xyz = 'Hello world'; //invalid; starts with a number
$_xyz = 'Hello world'; //valid; starts with an underscore
$_9xyz = 'Hello world'; //valid

$foo = 25; // Numerical variable


$bar = “Hello”; // String variable
$foo = ($foo * 7); // Multiplies foo by 7
$bar = ($bar * 7); // Invalid expression
?>
Send Data to the Web Browser

 The PHP command ‘echo’ is used to output the parameters passed to it

 The typical usage for this is to send data to the client’s web-browser

 Syntax

 void echo (string arg1 [, string argn...])

 In practice, arguments are not passed in parentheses since echo is a


language construct rather than an actual function
Echo example
<?php  Notice how echo ‘5x5=$foo’
$foo = 25; // Numerical variable outputs $foo rather than replacing
$bar = “Hello”; // String variable it with 25
 Strings in single quotes (‘ ’) are
echo $bar; // Outputs Hello not interpreted or evaluated by
echo $foo,$bar; // Outputs 25Hello PHP
echo “5x5=”,$foo; // Outputs 5x5=25  This is true for both variables and
echo “5x5=$foo”; // Outputs 5x5=25 character escape-sequences (such
echo ‘5x5=$foo’; // Outputs 5x5=$foo as “\n” or “\\”)
?>
PHP | Static Variables

• The variables in a function which keep their value after the function has ended.

<?php
function add() {
static $number = 1;
echo $number;
$number++;
}

add();
echo "<br>";
add();
echo "<br>";
add();
?>
PHP | Constants
• Constants are either identifiers or simple names that can be assigned any fixed values.

• They are similar to a variable except that they can never be changed.

• Unlike with variables, you do not need to have a constant with a $.

• The define() function in PHP is used to create a constant as shown below:


<?php Output
• Syntax:
// This creates a case-sensitive GeeksforGeeks
define(name, value, case_insensitive) constant
GeeksforGeeks
define("WELCOME",
• The parameters are as follows: "GeeksforGeeks");
echo WELCOME, "\n";
• name: The name of the constant.
// This creates a case-insensitive
constant
• value: The value to be stored in the constant.
define("HELLO", "GeeksforGeeks",
true);
• case_insensitive: Defines whether a constant is case insensitive. By default this value is False, i.e.,
echo hello;
case sensitive.
Arithmetic Operations
 $a - $b // subtraction
 $a * $b // multiplication
 $a / $b // division
 $a += 5 // $a = $a+5
Also works for *= and /= <?php
$a=15;
$b=30;
$total=$a+$b;
Print $total;
Print “<p><h1>$total</h1>”;
// total is 45
?>
Concatenation

Use a period to join strings into


one.
<?php
$string1=“Hello”;
$string2=“PHP”;
$string3=$string1 . “ ” . $string2;
Print $string3;
?>

Hello PHP
String functions

Name Java Equivalent


strlen length
strpos indexOf
substr substring
toLowerCase,
strtolower, strtoupper
toUpperCase
trim trim
explode, implode split, join
strcmp compareTo
Escaping the Character

If the string has a set of double quotation marks that must remain visible, use the \
[backslash] before the quotation marks to ignore and display them.

<?php
$heading=“\”Computer Science\””;
Print $heading;
?>

“Computer Science”
Review exercise
What does a page’s encoding refer to?

What impact does the encoding have on the page?

What PHP functions, or language constructs, can you use to send data to the Web browser?

How does using single versus double quotation marks differ in creating or printing strings?

What does it mean to escape a character in a string?

Search the PHP manual for the $_SERVER variable to see what other information it contains.

26
How are constants defined and used?
END

You might also like